0

I made an app that has three activities, and it works like this:

Activity A --> Activity B --> Activity C

and for returning from activity C to activity A is like this :

Activity C --> Activity B --> Activity A

and I wanna to pass an object in Activity C to Activity A, but when I press back button or what ever, all of activity's objects has destroyed,and I can't receive the object on Activity A .How can I pass an object in Activity C to Activity A?

(I used DB for solving this problem already and when I am in activity C , I insert the object in a table and also use the table in activity A for getting the object . But I am looking for easier way to solve it)

please guide me...

Thanks

poursina
  • 87
  • 1
  • 1
  • 11
  • Instead of using multiple activity you can use multiple fragment in on activity. – Ajit Kumar Dubey Apr 28 '14 at 06:52
  • I don't think exists an easy way to do it, anyway you could try to pass this object between the 3 activities and when you go back from activity C you pass it to B and B pass it to A. Or maybe something could be done using activity result... add more info about what you want to do. – Marco Acierno Apr 28 '14 at 06:53
  • you can use global variable http://stackoverflow.com/questions/1944656/android-global-variable – Android Apr 28 '14 at 06:56

3 Answers3

1

You might override onActivityResult in your Activities A and B, so that B just takes the result back from the C and passes it back to A

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
  super.onActivityResult(requestCode, resultCode, data);
  switch(requestCode) {
     case (A) : {
      if (resultCode == Activity.RESULT_OK) {
        // Extract the data returned from the Activity.
      }
      break;
    } 
  }
}

while in B and C you should pass back the object to the calling activity:

Intent resultIntent = new Intent();
// add your object here to the resultIntent
setResult(Activity.RESULT_OK, resultIntent);
finish();
Sergo Pasoevi
  • 2,833
  • 3
  • 27
  • 41
1

using shared Preferences in Activity C as shown.

SharedPreferences data = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
data.edit().putString("somevalue", myvalue).commit();

and calling it in Activity A as:

SharedPreferences data = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
String myval= data.getString("somevalue",""); 
coder
  • 13,002
  • 31
  • 112
  • 214
0

If this is primitive data type, you can go with a SharedPreferences mechanism. You can also use some public static fields.

More information about your problem: http://developer.android.com/guide/faq/framework.html#3

tjurczyk
  • 111
  • 2