How to pass / pass an object from fragment to fragment?

I want to pass a realtime object from fragment

to fragment

.

I dont want to transfer data .

I haven't found a way to do this.

The only way I think is intentional, but I'm not sure.

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

I would like to see an example if share / pass object can be used from fragment

to secend fragment

. Many thanks.

+3


source to share


3 answers


Passing real objects between activities or fragments can be achieved by implementing the beans Parcelable

or model interface Serializable

.

Parcelable : Parcel is similar to Bundle, but more complex and can support more complex class serialization. Applications can implement an interfaceParcelable

to define the classes of applications that can be passed, especially when using services.

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

public class Foo implements Parcelable {
   ...
}

      

Serializable . Serializable interface is Java standard serialization. You can read about it here .

public class Foo implements Serializable {

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

      

Let's assume you have a class Foo

that correctly implements Parcelable to put it in an Intent in an Activity:

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

      



To get this from intent in another activity:

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

      

** EDIT **

As the original question asked for snippets, 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 the package in another snippet:

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

      

Hope this helps!

+6


source


  • Passing an object from one fragment to another A fragment can be accomplished with these simple steps:
  • First of all, you must make your model class Parcelable:

    class Car implements Parcelable

  • Then it implements the override functions of the Parcelable class: eg

    @Override
    public int describeContents() {
        return 0;
    }
    
    @Override
    public void writeToParcel(Parcel parcel, int i) {
    
    }
    
          

  • From the first snippet:

    // Set data to transfer

    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); 
    
          

    // Passing data to another fragment

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

  • In the second snippet

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

    Hope this helps :)



+1


source


There is another way to pass an object by simply creating a simple setter function. From your current fragment, let's make a target fragment object. Your target snippet 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();`

      

0


source







All Articles