vijay g
vijay g

Reputation: 113

How to pass an object form Tabhost activity to tab Fragments usding get/set arguments

Here i am having the tabhost activity with four fragment tab. Here i have to pass the object from this tabhost activity to all the fragment tabs.For that iam using get/set Arguments but it throws null value when i am trying to get the object in the fragment.

this is my 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);


    }
}

this is my 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;

        }

Upvotes: 1

Views: 459

Answers (2)

Rahul Chaudhary
Rahul Chaudhary

Reputation: 1061

Use this code for sending Object

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

GET VALUE ON NEXT FRAGMENT

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

    }

Upvotes: 1

kris larson
kris larson

Reputation: 30985

Look closer:

put:

        bundle.putSerializable("collegedetails", initCollegeMO);

get:

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

Your key strings don't match.

Upvotes: 0

Related Questions