Is intent used to create activity?

A bit more about the question.

I want to know if the data that I transferred was in the intent that the Activity created, if I don't kill it.

Example: Activity A triggers activity B with optional String data (SomeStringValue). Then Activity B calls C, which calls D. Now, somewhere around this time, Activity B was destroyed (for example, to save memory), when I return to Activity B, it needs to be recreated (for example, onCreate is called again), but since I used back, instead of transmitting the intention, my previous intention will still be there, and I can get the data I need or the data will be deleted.

I tried to check it myself. But I can't get onCreate to be called again without killing the whole application.

+4
source share
2 answers

As mentioned above, I checked this by going to the "Developer Options" and turning on "Do Not Perform Actions".

Using this methodology, I see that the original intention is preserved when the action is deleted from memory.

onDestroy is called immediately when I leave the action. When I get back to the original action, onCreate is called with the same values ​​in the Intent as it was originally sent.

The following code was used as a test bench.

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

}

So, to answer your question, saving Intent data is redundant in onSavedInstanceState. You should just save everything that has changed or should be preserved, but not preserved forever.

+11
source

If the onDestroy() method has been called, there is a chance that will be blocked by all intent data ... you need to override the onSaveInstanceState() method that you intend to use there ... after that, onCreate() just check if the package has saveInstaceState null or not ...

This should make you sure that all additional features will be saved, no matter what happens ...

0
source

All Articles