Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does the intent used to create an activity stick around?

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.

like image 917
Raigex Avatar asked Jan 02 '13 15:01

Raigex


People also ask

What is activity and intent?

Activity is a UI component which you see on your screen. An Intent is a message object which is used to request an action from the same/different app component.

How do you pass an activity in intent?

The easiest way to do this would be to pass the session id to the signout activity in the Intent you're using to start the activity: Intent intent = new Intent(getBaseContext(), SignoutActivity. class); intent. putExtra("EXTRA_SESSION_ID", sessionId); startActivity(intent);

How do you check which intent started the activity?

Use startActivityForResult in your calling activity to start the activity: ActivityCompat. startActivityForResult(this, new Intent(this, MyActivity. class), 0, null);


1 Answers

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.

like image 144
Mike dg Avatar answered Sep 25 '22 13:09

Mike dg