Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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.)

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)

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);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top