6

Is it possible to save a reference to an object in a bundle?
Not the whole object, only the reference?

For example, Bundle class has method to get an object but I can't find the put or set method.

I need it because in an activity, I create an instance of my timer class, and then I start the timer: myTimer.start()

If I have to restart the activity, I want to restore the timer to it's previous value.

Mr. Llama
  • 20,202
  • 2
  • 62
  • 115
Radioleao
  • 394
  • 1
  • 4
  • 19

4 Answers4

6

Hopefully you cannot do this. You can only store primitives in bundle. As Marcin Orlowski mentioned "storing" the whole object is achieveable through implementing Parcelable interface. By "storing" I meant storing object state. Implementing this interface helps you persisting your object state in different code sections without putting its all attributes to Bundle object over and over again.

When activity goes to pause state sooner or later all objects used by your activity will be removed by garbage collector, so storing references to them would be silly.

bpawlowski
  • 1,025
  • 2
  • 11
  • 22
  • So, everytime they rotate the device I have recreate every object? – Radioleao May 15 '14 at 16:42
  • Actually, yes. Because your activities are recreated during device rotation. There's `onSaveInstanceState` method called on every `Activity` and other similar classes so you can save state of your objects there, and then recreate them in `onCreate` method. If you have saved your activities state in `onSaveInstance` method, the `Bundle` argument of `onCreate` method should NOT be null. However there's an method to avoid Activity recreation during orientation change - overriding `onConfigurationChanged` method and not allowing to call it's super method. Check this out:http://tinyurl.com/l3rooo7 – bpawlowski May 15 '14 at 21:36
  • My answer was too long so I couldn't attach link to appropriate thread, here it is: http://stackoverflow.com/questions/8814551/how-to-stop-activity-recreation-on-screen-orientation/8814650#8814650 – bpawlowski May 15 '14 at 21:42
  • Thanks much, it’s great! I understood that the best is make the screen refresh and handle it, but in this moment I can avoid the problem and continue studying. – Radioleao May 16 '14 at 12:38
  • 2
    @Garet, Why is it silly? The objects can also be referenced from elsewhere (Application class for instance) so don't necessarily have to be GC'ed when the activity is paused. – AtomHeartFather Feb 06 '16 at 11:49
  • Actually, from the source of `BaseBundle` class one can see that a `Serializable` / `Parcelable` objects are saved as references in the internal map like any other objects of any type. Then the whole bundle might be (but not guaranteed) be parceled into a `Parcel` upon rotation. However in my case that's not the case. On rotation the `savedInstanceState` is not parcelled and remains the same for the new fragment thus causing leaks of the complex objects that I store in bundle. Even though they are parcelables! Really nasty. – WindRider Aug 15 '16 at 12:49
2

You can't. You can only pass limited set of data types as you can see by looking at method lists in the docs and if you want more than single primitives then your class needs to implement Parcelable interface (or use helper like Parceler). Once you got that done you will be able to pass your object data via the Bundle (but still, not object itself)

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
  • 2
    That doesn't really answer the question, which was about storing a reference to an object, not the object itself. – AtomHeartFather Feb 06 '16 at 11:44
  • Parcelable is meant for storing objects, much like Serializable, but you need to implement the "marshalling" yourself. It has nothing to do with how Java manages objects. You've explained how to store an object, NOT a referance to it, which was what the question was about. – AtomHeartFather Feb 08 '16 at 13:45
  • If any of you'd spent a moment looking at the docs, then it should be clear why. – Marcin Orlowski Dec 07 '16 at 20:12
2

The official docs recommend using fragments for storing references during "configuration changes" (no, I don't think this means you need to repose your activity as a fragment, but to use a fragment as a storage medium (clarification needed)):

http://developer.android.com/guide/topics/resources/runtime-changes.html#RetainingAnObject

Retaining an Object During a Configuration Change

If restarting your activity requires that you recover large sets of data, re-establish a network connection, or perform other intensive operations, then a full restart due to a configuration change might be a slow user experience. Also, it might not be possible for you to completely restore your activity state with the Bundle that the system saves for you with the onSaveInstanceState() callback—it is not designed to carry large objects (such as bitmaps) and the data within it must be serialized then deserialized, which can consume a lot of memory and make the configuration change slow. In such a situation, you can alleviate the burden of reinitializing your activity by retaining a Fragment when your activity is restarted due to a configuration change. This fragment can contain references to stateful objects that you want to retain.

When the Android system shuts down your activity due to a configuration change, the fragments of your activity that you have marked to retain are not destroyed. You can add such fragments to your activity to preserve stateful objects.

To retain stateful objects in a fragment during a runtime configuration change:

Extend the Fragment class and declare references to your stateful objects. Call setRetainInstance(boolean) when the fragment is created. Add the fragment to your activity. Use FragmentManager to retrieve the fragment when the activity is restarted. For example, define your fragment as follows:

public class RetainedFragment extends Fragment {

    // data object we want to retain
    private MyDataObject data;

    // this method is only called once for this fragment
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // retain this fragment
        setRetainInstance(true);
    }

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

    public MyDataObject getData() {
        return data;
    }
}

Caution: While you can store any object, you should never pass an object that is tied to the Activity, such as a Drawable, an Adapter, a View or any other object that's associated with a Context. If you do, it will leak all the views and resources of the original activity instance. (Leaking resources means that your application maintains a hold on them and they cannot be garbage-collected, so lots of memory can be lost.)

Community
  • 1
  • 1
samus
  • 6,102
  • 6
  • 31
  • 69
2

One alternate approach is to convert the POJO to json and then to string the GSON library and save in bundle as string. Then retrieve it back from bundle as string and convert it back to object using the same GSON library. Example code below.

Convert JSON to String and save it in bundle

JsonElement json = new JsonElement();
String result = gson.toJson(json);
bunlde.putString("key",result);

Convert String from bundle to object again

 JsonElement json = gson.fromJson(bundle.getString("key"), JsonElement.class);
mohammed nathar
  • 190
  • 1
  • 12