3

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:

FragmentFullScreen fragment = new FragmentFullScreen();
        Bundle args = new Bundle();
        args.putParcelable(ARG_VIDEO_SELECTED, mVideoSelected);
        fragment.setArguments(args);

getFragmentManager().beginTransaction()
                .replace(R.id.container, FragmentFullScreen.newInstance(mVideoSelected))
                .addToBackStack("FragmentDetails")
                .commit();

And then I using popBackStack to go to the previous Fragment:

getFragmentManager().popBackStack();

And there is when I want to update a Variable from the Previous Fragment.

jobernas
  • 686
  • 1
  • 12
  • 24
  • 1
    If you use a singleton design pattern you could create there a function which would return your variable of choice – zwebie Nov 20 '14 at 11:54
  • 1
    After fragment is popped you can find it by tag and pass your value. – Bracadabra Nov 20 '14 at 11:54
  • 1
    exclude that in separate Java class that will hold your object within the project scope. – Anuj Sharma Nov 20 '14 at 11:55
  • 1
    You can use the fragmentmanager to find the fragment and then set a value using a method created for that. You can also use the enclosing activity to store temporarily the value. – Joao Sousa Nov 20 '14 at 11:57
  • I have in Mind that three solutions ... So To answer my question, to past like startActivityForResult is not possible? – jobernas Nov 20 '14 at 11:58
  • I suggest you use the activity hosting the fragments as a proxy to communicate between the fragments – zwebie Nov 20 '14 at 12:03

4 Answers4

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.html http://developer.android.com/reference/java/util/Observable.html

Dmitry_L
  • 1,004
  • 7
  • 8
  • I am getting a litle trouble understanding how to do it. Is the Observable that update my Object Observer correct? ... If my Observable is in my first Fragment how do I update my Observer. I have to past my Observable to the second Fragment ? Can you redirect me to an Example or give me one? tks – jobernas Nov 20 '14 at 23:48
  • 1
    Look at documentation http://developer.android.com/reference/java/util/Observable.html . Store this object in application class. Then in first fragment in create method get this observable from application and call addObserver, (add there Observer instance, you may implement Observer interface in your fragment), but don't forget to call deleteObserver when your fragment is destroying. In 2nd fragment just get Observable object from application and call notifyObservers(data), where data will be your variable values or something else. – Dmitry_L Nov 21 '14 at 11:28
  • I manage do it like you said and by the Logs it looks its is updating fine but when I bo back to the First Fragments, my Object is null and it load the previous object in the bundle. How do I preserve the object? – jobernas Nov 21 '14 at 11:32
  • My First Fragment alread get the Video from another Fragment ... So I have the Object in a Bundle ... but besides that The object is null So I can't check if the Object is Diferent from Null to avoid to load it from the Bundle... – jobernas Nov 21 '14 at 11:36
  • the first fragment instance is the same after back as before opening second fragment? Could you debug and see what happens when you assign value from second fragment? And also if you won't fix this problem or will have some difficulties, you may just get your fragment in FragmentManager by fragment tag and call setVariable method directly - ((MyFragment) fragmentManager. findFragmentByTag("fragmentTag")).setVariable(value) – Dmitry_L Nov 21 '14 at 11:43
  • The first fragment is the Same because I use addToBackStack to save in the stack and then I use the popBackStack to retrieve the previous fragment (In this case the first Fragment) ... – jobernas Nov 21 '14 at 11:48
  • just check if your value is not null then not set value from args to it, it should work. – Dmitry_L Nov 21 '14 at 13:00
  • I manage to use a Callback through the Activity and Updated the Object in the Previous Fragment in the onCreate Method. Tks anyway. The Observer will be more usefull in another situation. – jobernas Nov 21 '14 at 14:07
  • @Bernas, could you post that Callback code as answer? I have exactly the same problem. Thanks a lot! – Valeriya Jun 17 '16 at 10:16
  • @Bernas can u pls help me how u achieve ur task ? – Erum Oct 06 '16 at 08:16
  • @Bernas i want to pass setArguments null to my firstFragment but failed to do so – Erum Oct 06 '16 at 08:17
0
Bundle args = new Bundle();
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)
AndiDog
  • 68,631
  • 21
  • 159
  • 205
upperY
  • 1
  • I trying to go back in the stack to a previous created Fragment not create a new Instance of a Fragment. That I already know. – jobernas Nov 20 '14 at 12:01
0

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.

cozeJ4
  • 1,555
  • 2
  • 11
  • 20
  • I manage to use a Callback through the Activity and Updated the Object in the Previous Fragment in the onCreate Method. Tks. – jobernas Nov 21 '14 at 14:07
-2

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

public class Singleton {

private static Singleton mInstance = null;

private int mVar;

private Singleton() {

if (mInstance == null) {

    mInstance = new Singleton();
}

return mInstance;

}

public static getInstance () {
    return mInstance;
}

public int getVar () {

return mVar;
}

public void setVar (int val) {
 mVar = val;
}

}
Benoist Laforge
  • 127
  • 1
  • 6
  • It is not good method, static variable may come to null and than your data will be lost. http://stackoverflow.com/questions/9541688/static-variable-null-when-returning-to-the-app – Dmitry_L Nov 20 '14 at 12:20