Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save object reference to Bundle in Android

Tags:

java

android

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.

like image 437
Radioleao Avatar asked May 15 '14 15:05

Radioleao


People also ask

Can we pass objects in bundle?

It is also possible to pass your custom object to other activities using the Bundle class. There are two ways: Serializable interface—for Kotlin and Android.

How do you pass bundles in intent?

Using putExtra() We can start adding data into the Intent object, we use the method defined in the Intent class putExtra() or putExtras() to store certain data as a key value pair or Bundle data object. These key-value pairs are known as Extras in the sense we are talking about Intents.

What is a bundle object in Android?

An Android App Bundle is a publishing format that includes all your app's compiled code and resources, and defers APK generation and signing to Google Play.


2 Answers

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.

like image 105
bpawlowski Avatar answered Sep 18 '22 14:09

bpawlowski


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

like image 29
samus Avatar answered Sep 19 '22 14:09

samus