0

I'm building an Android App using Android Studio. Let's say I have Activity1 and Activity2. Activity2 has 3 fragments (3 tabs). I managed to pass data from Activity1 to Activity2 and then from Activity2 to it's fragments by using a Fragment Adapter.

1) I want to do the opposite: gather data from all 3 fragments and pass it to the activity they are sitting in (Activity2), and then pass the data from Activity2 back to Activity1.

I have implemented an interface that passes data from one fragment to Activity2, but how (and when) can I pass data from all 3 fragments to Activity2? The method I wrote sends an object from a fragment to Activity2. But the method in Activity2 gets only one object... Or perhaps there's a way I can send the data from the fragments to the Activity2 Fragment Adapter? (This can be the best I think...)

2) Is the best way to pass data from Activity2 back to Activity1 is by overriding "onBackPressed" and using startActivityForResult and setResult? (I don't have a button to do that besides the ActionBar "back button").

Thanks!

mac-man
  • 41
  • 1
  • 11
  • startActivityForResult gives you a chance to handle every single activity return. So that, you dont have to worry about activity return value messing. – metzelder Jun 04 '15 at 10:19

3 Answers3

1

1) You can create an interface with 3 methods like: setResult1(), setResult2() and setResult3() and make your Activity implement this interface so it can collect the results of the 3 Fragments as soon as they are available. The fragments should call their own method in this interface as soon as the result changes, and also after restoration (in case the Activity is re-created, like when you change orientation).

2) For Activity2 to send the result back to Activity1, the best way is to start Activity2 from Activity1 with startActivityForResult(). Then each time a new result is collected by Activity2 it should call setResult(int resultCode, Intent Data). You can call it multiple times to replace the previous result. If you set the result immediately you don't need to override onBackPressed().

BladeCoder
  • 12,779
  • 3
  • 59
  • 51
1

My suggestion is to create a new class for storing and passing data,and this class can be accessed by any activities or fragments.This class has to be singleton so the same data is shared by different activities and fragments.

public static DataCache instance;
private UserInfo user;    //here's the data you want to save

private DataCache(Context c){
    this.user=new UserInfo();
}

public static DataCache getInstance(Context c){
    if(null==instance){
        instance=new DataCache(c);
    }
    return instance;
}

public void setData(UserInfo data){
    this.user=user;
}

public UserInfo getData(){

    return user;
}

You can use this to save or get data from any activities or fragments: DataCache.getInstance(context).getData()/saveData(data)

In this case the data is stored in memory ,you can rewrite the set and get functions to save the data to anywhere you want,for example the SDCARD or database.

Ai Hao
  • 102
  • 5
0

You can store data from all 3 fragments into shared preferences and get that data in activity 1. You should clear the data from shared preference once you retrieved.