如何将对象形式 Tabhost 活动传递到选项卡片段 usding get/set 参数



在这里,我有四个片段选项卡的tabhost活动。在这里,我必须将对象从此选项卡host活动传递到所有片段选项卡。为此,我使用获取/设置参数,但是当我尝试在片段中获取对象时,它会抛出空值。

这是我的tabhostAtivity

 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tab_host);
        tabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
        tabHost.setup(this, getSupportFragmentManager(), android.R.id.tabcontent);
        context = getApplicationContext();
        final InitCollegeMO initCollegeMO = (InitCollegeMO) getIntent().getSerializableExtra("initCollegeMO");
        Log.e("tab", "init" + initCollegeMO);
        Bundle bundle = new Bundle();
        bundle.putSerializable("collegedetails", initCollegeMO);
        AboutCollegeFragment fragment = new AboutCollegeFragment();
        fragment.setArguments(bundle);
        tabHost.addTab(tabHost.newTabSpec(TAB_1_TAG).setIndicator("", getResources().getDrawable(R.drawable.college)), AboutCollegeFragment.class, null);
        tabHost.addTab(tabHost.newTabSpec(TAB_2_TAG).setIndicator("", getResources().getDrawable(R.drawable.course)), CourseFragment.class, null);
        tabHost.addTab(tabHost.newTabSpec(TAB_3_TAG).setIndicator("", getResources().getDrawable(R.drawable.admission)), AdmissionFragment.class, null);
        tabHost.addTab(tabHost.newTabSpec(TAB_4_TAG).setIndicator("", getResources().getDrawable(R.drawable.contact)), ContactFragment.class, null);
        tabHost.getTabWidget().setCurrentTab(0);

    }
}

这是我的aboutCollegeFragment

 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
//here i got null value
            activeInitCollegMO = (InitCollegeMO) getArguments().getSerializable("initCollegeMO");
            Log.e("tab", "init" + activeInitCollegMO);
            view = inflater.inflate(R.layout.web_view, container, false);
            webView = (WebView) view.findViewById(R.id.activity_tab_host_webview);
      return view;
        }

使用此代码发送对象

 Bundle bundle = new Bundle();
            bundle.putString("fromFragment", new Gson().toJson(myData ));
            MyFragment myFragment = new MyFragment ();
            myFragment .setArguments(bundle);
            replaceFragment(context, myFragment );

获取下一个片段的值

 bundle = this.getArguments();
    if (bundle != null) {
        String json = bundle.getString("fromFragment", "");
        Type dataType = new TypeToken<MyData >() {
        }.getType();
      MyData myData = new Gson().fromJson(json, dataType);
    }

仔细观察:

放:

        bundle.putSerializable("collegedetails", initCollegeMO);

获取:

            activeInitCollegMO = (InitCollegeMO) getArguments().getSerializable("initCollegeMO");

您的密钥字符串不匹配。

最新更新