4

I want to pass realtime object from fragment to fragment.

I dont want to pass data.

I dont found way to do that.

The only way that I think is by intent but I am not sure.

i read about Singleton pattern,interfaces and Bundle,Global varible.

I would like to see example if it is possible to use share/pass object from one fragment to secend fragment. many Thanks.

biraj800
  • 150
  • 1
  • 1
  • 9

3 Answers3

7

Passing real objects between activities or fragments can be achieved through implementing model beans Parcelable or Serializable interface.

Parcelable: A Parcel is similar to a Bundle, but is more sophisticated and can support more complex serialization of classes. Applications can implement the Parcelable interface to define application-specific classes that can be passed around, particularly when using Services.

You can see how to implement Parcelable interface in this article here.

public class Foo implements Parcelable {
   ...
}

Serializable: A serializable interface is java standard serialization. You can read more about it here.

public class Foo implements Serializable {

    private static final long serialVersionUID = 0L;
   ...
}

Suppose you have a class Foo implements Parcelable properly, to put it into Intent in an Activity:

Intent intent = new Intent(getBaseContext(), NextActivity.class);
Foo foo = new Foo();
intent.putExtra("foo", foo);
startActivity(intent);

To get it from intent in another activity:

Foo foo = (Foo) getIntent().getExtras().getParcelable("foo");

** EDIT **

As the original question asked for Fragments, then this is how it works:

 Fragment fragment = new Fragment();
 Foo foo = new Foo();

 Bundle bundle = new Bundle();
 bundle.putParcelable("Foo", foo);
 fragment.setArguments(bundle); 

To get it from bundle in another fragment:

Foo foo = (Foo) bundle.getParcelable("Foo");

Hope this helps!

Deminem
  • 672
  • 8
  • 19
  • I do not need to extract/disassemble the object to type varible `int,string,boolean etc..` `@Override public void writeToParcel(Parcel dest, int flags) { dest.writeString(varible1); dest.writeInt(varible2); dest.writeString(varible3); dest.writeString(varible4); }`and then to collect all the value from each varible. I need to transfer/pass/share the object as a one unit object with all the methodes together. – biraj800 Nov 10 '14 at 17:26
  • Android Dalvik (DVM) works different than JVM in many aspects which you can see in this [post](http://stackoverflow.com/questions/11374477/android-javac-vs-dalvik). Every application has given their own instance in DVM, which means multiple active applications require multiple DVM instances. By having said that, sharing the static memory with different processes are not good idea and doesn’t guarantee the lifetime of object state in memory. DVM also has an automatic garbage collector similar to Java implementation JVM. – Deminem Nov 11 '14 at 08:36
  • To address this issue, Java provides serialisation for marshalling/unmarshalling to keep the object state. However, android provides it’s own interface `Parcelable` which is more efficient than `Serializable`. I would recommend to read about marshalling/unmarshalling [here](https://coderwall.com/p/vfbing/passing-objects-between-activities-in-android). According to Android guidelines, passing the objects between activities/fragments can only be achieved through Parcelable/Serializable. All other methods have their pros and cons. – Deminem Nov 11 '14 at 08:40
  • The link is for passing Object between activities or Activity and Fragment and the question is about passing object from fragment to fragment. – Maihan Nijat Nov 30 '16 at 14:09
  • what to do if i want to pass the Parcelable Object from Fragment to Activity ? – Ravi Vaniya Nov 20 '17 at 09:30
2
  • Pass object from one Fragment to another Fragment can be acheived by using these simple steps:
  • First of all, you have to make your model class as Parcelable:

    class Car implements Parcelable

  • Then implements the override functions of Parcelable class: e-g

    @Override
    public int describeContents() {
        return 0;
    }
    
    @Override
    public void writeToParcel(Parcel parcel, int i) {
    
    }
    
  • From First Fragment:

    // Set data to pass

    MyFragment fragment = new MyFragment(); //Your Fragment
    Car car = new Car(); // Your Object 
    Bundle bundle = new Bundle();
    bundle.putParcelable("carInfo", car)  // Key, value
    fragment.setArguments(bundle); 
    

    // Pass data to other Fragment

    getFragmentManager()
           .beginTransaction()
           .replace(R.id.container, fragment)
           .commit();`
    
  • On Second Fragment

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
    
       Bundle bundle = this.getArguments();
       if (bundle != null) {
           Car receivedCar = bundle.getParcelable("carInfo"); // Key
       } 
    }
    

    Hope it will help :)

Satan Pandeya
  • 3,747
  • 4
  • 27
  • 53
Shujat Munawar
  • 1,657
  • 19
  • 23
1

There is another method you can pass object by just making a simple setter function. From your current fragment make object of target fragment. Your target fragment should have a setter function like

public void setObjectFunction(Object obj){this.mObj = obj}

YourFragment yourFragment = new YourFragment();
Object  passingObj = new Object();
yourFragment.setObjectFunction(passingObj);

Then

getFragmentManager()
   .beginTransaction()
   .replace(R.id.container, yourFragment)
   .commit();`