Issue
I have come across 2 different types to get my activity running based on some params. The first one is savedInstanceState and the other is getIntent.getExtras()
Q1) So what I don't understand is, once I pass bundle to my activity and then start it, it should have the bundle. But, if for some reason the activity is recreated again, it should have the same bundle all over again. (am I right?)
Q2) Based on the fact that Q1 is true, and the fact that I can't just override the bundle once the activity already started, I guess that if for some reason in my already started Activity, I want to change some params of the bundle, I should create some activity fields and used those fields in my activity life. And override saveInstanseState to save the new fields if from some reason my activity would be recreated. is it true?
Q3) Based on the fact that all above is true, in onCreate() should every activity in world of Android need to start like this:
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) Assume onSaveInstanceState was called and saved values that are different from the original bundle who started the activity (the getIntent.getExtras), If the activity is recreated again, does this mean that saveInstanceState is different from the getIntent.getExtras() or they are now the same? (If they are the same, then the if/else on the code above has no true meaning, cause it is the same!).
Q5) If I didn't override the onSaveInstanceState but when I created the activity I pass it a Bundle, does this still mean that I can get my original bundle if the activity recreated again? ( i guess this question will answer itself based on other answers)
Solution
The main difference between getIntent().getExtras() and the savedInstanceState is that they have different usages. The intent is for the communication between activities while the saved state is for the current state of your UI when you leave your activity e.g. by pressing the home button. So it will been saved the position of your ListViews and similar or the value of unsaved TextEdits.
Q1: Yes in general that information should not been lost, as long you don't open the activity a second time.
Q2: True so far. The default views will save their state so you don't need to do it yourself. However if your Activity is started a second time with a different bundle you will have the new values.
Q3: No not necessary, that depends on your use case, however that would not break anything. The point is when you load date from the internet you would have to wait just one time, on the second onCreate() call the data will been restored (if you show them in UI elements with an id!).
Q4: The intent would be the initial value while the saved state is the modified state of the values.
Q5: In general that should work the same. Since the basic ui elements will save their state by default.
The point of the saved state is to keep ui changes even if your app was destroyed in the background. So your app will resume at that point where left it even if it was not anymore in the memory of your device.
Answered By - rekire
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.