Does the intent used to create an activity stick around?
Asked Answered
C

2

5

A bit more on the question.

I want to know if the data i passed in the intent that created an Activity stays around if I don't kill it.

Example: Activity A calls Activity B with extra data String(SomeStringValue). Then Activity B calls C which Calls D. Now somewhere during this time Activity B was destroyed (to save memory for instance), when I come back to Activity B it needs to be recreated (eg onCreate is called again) but since I have used back button instead of passing an intent will my previous intent still be there and I can get the data I need or will that data be gone.

I have tried to test it myself But i cannot get onCreate to be called again without killing the whole app.

Capella answered 2/1, 2013 at 15:34 Comment(3)
To test this, go to developer options, and check the "Do not keep activities" option. Remember to turn it off though since sooo many apps break. If nobody answers this later, I will verify for you.Combination
If you did not do something special in your activity, an easy way to recreate it is to change the orientation of the device (eg. from portrait to landscape).Unguent
@Mikedg Does the do not keep activities act like you are killing them? (because certain buttons like back and "home"(which is a button that uses onActivityResult to go back to first activity)Capella
C
12

As mentioned above, I tested this by going into Developer Options and turning on "Do Not Keep Activities."

Using this methodology, I see that the original Intent is maintained when an activity is remove from memory.

onDestroy is immediately called when I leave an activity. When I go back to the original activity onCreate is called with the same values in the Intent as was originally sent over.

The following code was used as my testbed.

public class MyActivity extends Activity {
/**
 * Called when the activity is first created.
 */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    String extra = getIntent().getStringExtra("test");

    ((TextView) findViewById(R.id.test)).setText(extra);
}

public void onClick(View view) {
    Intent i = new Intent(this, MyActivity.class);
    i.putExtra("test", ""+Math.random());
    startActivity(i);
}

@Override
protected void onDestroy() {
    Log.d("Test", "onDestroy");
    super.onDestroy();    //To change body of overridden methods use File | Settings | File Templates.
}

}

Thus to answer your question, saving Intent data is redundant in onSavedInstanceState. You should just be saving anything that changed or would need to be preserved but not persisted forever.

Combination answered 2/1, 2013 at 16:0 Comment(1)
Thanks for the info. I have also tried it with the kill activity and it kept crashing when I had savedInstanceState but no crash if I didn't use it.Capella
T
0

If the onDestroy() method was called, there is a chanse that all intent data will be losed... you need to override the onSaveInstanceState() method an pass you're intent data there... after that in onCreate() just check if the saveInstaceState bundle is null or not...

This should make you certain that all extras will be saved, no matter what happens...

Tar answered 2/1, 2013 at 15:44 Comment(4)
I'm not worrying if onDestroy was called (as that usually means I want the activity to end). My worry is that an activity gets killed to free up some memory, I also already use onSaveInstanceState() just wanted to know if using onSaveInstanceState is redundant when using intents or not.Capella
you could also try to use the onLowMemory() method, but I'm not sure if it works properly...Tar
I though onLowMemory was only for use in the emulator and not on device.Capella
This is called when the overall system is running low on memory, and would like actively running process to try to tighten their belt. this is the javadoc on this methodTar

© 2022 - 2024 — McMap. All rights reserved.