SaveInstanceState vs getIntent (). getExtras ()

I came across two different types to start my activity based on some parameters. The first is savedInstanceState , and the other getIntent.getExtras ()

Q1) Therefore, I do not understand, as soon as I pass the bundle to my activity and then run it, it should have a package. But, if for some reason the activity is recreated again, it must have the same set again and again. (I'm right?)

Q2) Based on the fact that Q1 is true, and the fact that I canโ€™t just redefine the package after the activity has already started, I assume that if for some reason the Activity is already running, I want to change some params beam, I have to create activity fields and use these fields in my life. And override saveInstanseState to save the new fields if for some reason my activity is recreated. it's true?

Q3). Based on the fact that all of the above is true, in onCreate () so that every activity in the Android world should start as follows:

if (savedInstanceState != null) { mType = savedInstanceState.getInt("some_val1"); mCardId = savedInstanceState.getLong("some_val2"); mQuery = savedInstanceState.getString("some_val3"); mCategory = savedInstanceState.getLong("some_val4");; } else { mType = getIntent().getExtras().getInt("some_val1"); mCardId = getIntent().getExtras().getLong("some_val2"); mQuery = getIntent().getExtras().getString("some_val3"); mCategory = getIntent().getExtras().getString("some_val4"); } 

Q4) Suppose that onSaveInstanceState is called and stores values โ€‹โ€‹different from the original package that started the activity (getIntent.getExtras). If the activity is recreated again, does this mean that saveInstanceState is different from getIntent.getExtras (), or are they the same now? (If they match, then if / else in the code above does not have a true value, because it is one and the same!).

Q5) If I did not redefine onSaveInstanceState , but when I created the action, I passed the Bundle to it, it still means that I can get my original package if the activity is recreated again? (I think this question will be answered by myself, based on other answers)

+7
android android-activity android-lifecycle
source share
1 answer

The main difference between getIntent().getExtras() and savedInstanceState is that they have different uses. The goal is to link between actions until the saved state matches the current state of your user interface when you leave your activity, for example. by clicking the home button. Thus, the position of your ListView and the like or the value of unsaved TextEdit s will be saved.

Q1: Yes, in general, that information should not be lost, since you do not open it a second time.

Q2: True so far. Views by default retain their state, so you do not need to do this yourself. However, if your activity is started a second time with a different package, you will have new values.

Q3: No need, it depends on your use case, however it will not break anything. The fact is that when you download a date from the Internet, you have to wait only once, in the second call to onCreate() data will be restored (if you show them in user interface elements with an identifier!).

Q4: The intention is the initial value, and the saved state is the changed state of the values.

Q5: In general, this should work the same. Since the main elements of ui will keep their state by default.

The point of saving state is to save ui changes, even if your application was destroyed in the background. Thus, your application will be resumed the moment you leave it, even if it is no longer in the memory of your device.

+6
source share

All Articles