how to pass object between activity and fragment

12,875

No doubt: best solution is to call an activity method from the fragment:

Here an example with a Bitmap Object.

In ACTIVITY:

define your method:

public Bitmap getMyBitmap() {
    return myBitmap;
}

In FRAGMENT:

1 - Define your activity

private Activity_Main myActivity;

2 - Link your activity

@Override
public void onAttach(Activity myActivity) {
    super.onAttach(myActivity);
    this.activity= (Activity_Main) myActivity;
}

3 - Call your method!

myActivity.getMyBitmap()

Quick and easy!

Share:
12,875
Myron
Author by

Myron

Updated on June 18, 2022

Comments

  • Myron
    Myron almost 2 years

    Please give me some suggestions how to pass object between fragment and activity.

    In my project, there is one FragmentActivity to show and edit customer profile. Multiple tabs will be contained in this activity to show contact info, address... The customer info will be preload as one class in the activity. My question is how could I pass this object to each fragment or tab? Once updated, how could I pass back to activity?

    Do I must to implement the Parcelable interface in my customer class to pass by bundle?

    Each tab will be dynamic created, is possible to get the fragment instance to modify view directly? If yes, once tab switch, is fragment destroyed?

    Thanks

    Myron

  • Myron
    Myron almost 11 years
    Do I need to add "R.id.fragment_container" in my activity to get the instance of each dynamic created fragment? For the interface is ok, but how to pass objects between activity and fragment?
  • jpardogo
    jpardogo almost 11 years
    R.id.fragment_container is the id of the container of your fragment. It is used to be a FrameLayout on your activity layout. To pass custom objects is the same way but you have to Serialize or Parcelable, it is answered in here : stackoverflow.com/questions/10836525/…
  • Pontus Backlund
    Pontus Backlund almost 10 years
    This is not the best solution, it's inefficient, better to use an interface to avoid leaks and memory problems
  • Vipin Sharma
    Vipin Sharma over 9 years
    @PontusBacklund - Sorry, I didn't understand "it's inefficient". He is adding activity reference in fragment using onAttach(). If he removes that reference in onDettach() by simply myActivity=null; there will be no leaks. Also, in cases where user needs to have access other objects which will be modified by app based on current state, i feel this is best approach. Also this is what google developers also recommended.
  • user65721
    user65721 about 9 years
    @VipinSharma can you point out any example or documentation that goodle developers actually recommended said approach?
  • Vipin Sharma
    Vipin Sharma over 8 years
    @user65721 Extremely sorry for late response. I checked code example given on developer.android.com/guide/components/fragments.html Creating event callbacks to the activity
  • Simon Ninon
    Simon Ninon almost 6 years
    This is a poor design and it breaks one of the critical role of fragments: being reusable, by any kind of activities.