1

I have several Activities in a program. Lets say, activities A, B and C.

Activity A is the main activity in this context. It contains object X, that must be accessible for all other activities (Activities: B and C).

Activity A will start activity B and then B will start С. After that both activities A and B are in the background and can be killed by the OS. How should I pass object X to activities B and C in order to be sure that object X will not be killed when A&B are killed?

Dion Segijn
  • 2,625
  • 4
  • 24
  • 41
Roman
  • 65
  • 5
  • Do you need it for custom objects or primitive datatypes? If the latter is the case, SharedPreferences can be used for that. – Lennart May 17 '13 at 12:48

4 Answers4

1

Why don't you can create your X Object with SingleTon ? You can keep it alive as long as you want and you can get the same Instance from where ever you want.

public class TestObject {

private static TestObject testObjectInstance;

/* put you data here */

   private TestObject() {

   }

   public TestObject getTestObjectInstance() {

       if (testObjectInstance != null) {

           return testObjectInstance;

       } else {

           testObjectInstance = new TestObject();
           return testObjectInstance;
       }
   }

    public TestObject createNewTestObjectInstance() {

       testObjectInstance = new TestObject();

       return testObjectInstance;
    }
}
osayilgan
  • 5,873
  • 7
  • 47
  • 68
  • Thank You. I didn't know about this trick. When the testObjectInstance will be destroyed? As far as I concern, it will be killed only when the process is killed, isn't it? – Roman May 27 '13 at 15:12
  • Take a look on this StackOverFlow Question, it has been discussed before. [SOF Question](http://stackoverflow.com/questions/13891186/static-singleton-lifetime-in-android). As it has been discussed here, do not use SingleTon for your Activity instances which can cause Memory Leaks. – osayilgan May 27 '13 at 16:06
0

The best way will be to save X value in Shared Preferences.The Value of X will be retained even your A and B activities are killed.

Check this Link for How to use Shared Preferences:

How to use SharedPreferences in Android to store, fetch and edit values

Community
  • 1
  • 1
Nargis
  • 4,687
  • 1
  • 28
  • 45
0

There are three possible ways:

Two of them were mentioned here.

  1. Via SharedPreferences. But remember that the SharedPreferences retain the value when the application is closed. This is the best solution if you want the value to be "permanent" on your application.

  2. Creating a singleton object. This is the best solution if you want to manipulate the object in all activities but don't want to save it for another runs.

  3. Sending the data via extras. This is the best solution if you only want the VALUE of the object and don't want to manipulate it.

Thiago Moura
  • 358
  • 2
  • 12
0

try the putExtra() function in Intent You can only use it for primitives.

If your X is contains only primitives, you may write a function in X,public Intent fillIntentWithX(Intent intent) which take the intent object as argument, and fill the intent object with the primitives in X, and return the intent object.

similarly, write another function in X, public X getXFromIntent(Intent intent) which takes an intent as an argument, extracts the primitives residing in it to form a new object X, and return it.

  • use fillIntentWithX() to fill the intent, which launches B, with the object X's properties.

  • use getXFromIntent() to extract X out in activity B, and so on.

Vinay W
  • 9,912
  • 8
  • 41
  • 47
  • Thank You but I need to manipulate a complex object, not primitive. So to use Extras I have to implement Parcelable interface (I think so). – Roman May 27 '13 at 14:46