Skip to content Skip to sidebar Skip to footer

It Is Possible To Pass A Variable To A Previous Fragment From Another Fragment?

Im trying to pass a variable back to a previous Fragment, similar to startActivityForResult but with Fragments, Is this possible? The code I am using To call a Fragment is this: Fr

Solution 1:

You may implement Observable object in your 1st fragment and create Observer, which may be shared through Application class or Activity.

Then in your 2nd fragment you get this Observer from Application and update data. If you 1st fragment still exists your variable will be passed there.

http://developer.android.com/reference/java/util/Observer.htmlhttp://developer.android.com/reference/java/util/Observable.html

Solution 2:

Communication between fragments should be done via the activity. And to achieve this communication between fragment and activity, this is the best way http://developer.android.com/guide/topics/fundamentals/fragments.html#CommunicatingWithActivity.

Solution 3:

Bundleargs=newBundle();
args.putParcelable(ARG_VIDEO_SELECTED, mVideoSelected);
fragment.setArguments(args);

Put this variable in 'args' (just a key-value map). Just like:

args.putExtra("id", id);
args.putExtra("parcelable", p)

Solution 4:

A cool way to do this is to create a singleton class :

publicclassSingleton {

privatestatic Singleton mInstance = null;

privateint mVar;

privateSingleton() {

if (mInstance == null) {

    mInstance = new Singleton();
}

return mInstance;

}

publicstaticgetInstance () {
    return mInstance;
}

publicintgetVar () {

return mVar;
}

publicvoidsetVar (int val) {
 mVar = val;
}

}

Post a Comment for "It Is Possible To Pass A Variable To A Previous Fragment From Another Fragment?"