Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing an Object from an Activity to a Fragment

Tags:

android

I have an Activity which uses a Fragment. I simply want to pass an object from this Activity to the Fragment.

How could I do it? All the tutorials I've seen so far where retrieving data from resources.

EDIT :

Let's be a bit more precise:

My Activity has a ListView on the left part. When you click on it, the idea is to load a Fragment on the right part.

When I enter this Activity, an Object Category is given through the Intent. This Object contains a List of other Objects Questions (which contains a List of String). These Questions objects are displayed on the ListView. When I click on one item from the ListView, I want to display the List of String into the Fragment (into a ListView).

To do that, I call the setContentView() from my Activity with a layout. In this layout is defined the Fragment with the correct class to call. When I call this setContentView(), the onCreateView() of my Fragment is called but at this time, the getArguments() returns null.

How could I manage to have it filled before the call of onCreateView() ? (tell me if I'm not clear enough)

Thanks

like image 252
Nico Avatar asked Mar 29 '12 19:03

Nico


People also ask

How do you pass data from activity to fragment using intent?

This example demonstrates how do I pass a variable from activity to Fragment in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.

Can we navigate from activity to fragment?

If you want to go back from Activity to Fragment. This is very simple just override onBackPressed() in your activity and call onBackPressed where you want.


2 Answers

Create a static method in the Fragment and then get it using getArguments().

Example:

public class CommentsFragment extends Fragment {   private static final String DESCRIBABLE_KEY = "describable_key";   private Describable mDescribable;    public static CommentsFragment newInstance(Describable describable) {     CommentsFragment fragment = new CommentsFragment();     Bundle bundle = new Bundle();     bundle.putSerializable(DESCRIBABLE_KEY, describable);     fragment.setArguments(bundle);      return fragment;   }    @Override   public View onCreateView(LayoutInflater inflater,       ViewGroup container, Bundle savedInstanceState) {      mDescribable = (Describable) getArguments().getSerializable(         DESCRIBABLE_KEY);      // The rest of your code } 

You can afterwards call it from the Activity doing something like:

FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); Fragment fragment = CommentsFragment.newInstance(mDescribable); ft.replace(R.id.comments_fragment, fragment); ft.commit(); 
like image 191
Macarse Avatar answered Oct 21 '22 05:10

Macarse


In your activity class:

public class BasicActivity extends Activity {  private ComplexObject co;  @Override protected void onCreate(Bundle savedInstanceState) {     super.onCreate(savedInstanceState);     setContentView(R.layout.main_page);      co=new ComplexObject();     getIntent().putExtra("complexObject", co);      FragmentManager fragmentManager = getFragmentManager();     Fragment1 f1 = new Fragment1();     fragmentManager.beginTransaction()             .replace(R.id.frameLayout, f1).commit();  } 

Note: Your object should implement Serializable interface

Then in your fragment :

public class Fragment1 extends Fragment {  ComplexObject co;  @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,         Bundle savedInstanceState) {     Intent i = getActivity().getIntent();      co = (ComplexObject) i.getSerializableExtra("complexObject");      View view = inflater.inflate(R.layout.test_page, container, false);     TextView textView = (TextView) view.findViewById(R.id.DENEME);     textView.setText(co.getName());      return view; } } 
like image 40
Tugce Saritas Avatar answered Oct 21 '22 06:10

Tugce Saritas