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

3
  • Instead of using multiple activity you can use multiple fragment in on activity. Commented Apr 28, 2014 at 6: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. Commented Apr 28, 2014 at 6:53
  • you can use global variable stackoverflow.com/questions/1944656/android-global-variable Commented Apr 28, 2014 at 6:56

3 Answers 3

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",""); 
Sign up to request clarification or add additional context in comments.

1 Comment

how can use this method for passing an object?
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(); 

Comments

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.