Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Life cycle predicament with orientation change in another activity

I've got 2 activities in a tabhost. In Activity1, I handle orientation changes and when the user switches between activities fine.

The problem starts when the user switches from Activity1 to Activity2 (via tab select), performs an orientation change, then switches BACK to Activity1. I get a bit lost with the life cycle events that take place in my Activity1 while things are going when Activity2 is visible.

According to the debugger, here's the sequence of events that occur in my Activity1:

=== Orientation Change ===
onSaveInstanceState
onPause
onStop
onCreate
onStart
onRestoreInstanceState
onResume

=== Switch TO Activity 2 ===
onSaveInstanceState
onPause

=== Orientation Change WHILE IN Activity 2 ===
onStop
onCreate
onStart

=== Switchback BACK FROM Activity2 ===
onResume

As you can see, I have opportunity to save my Activity1 data in the call to onSaveInstanceState when it's switched out to Activity2, but I never get a call to onRestoreInstanceState to restore it.

Questions

  1. Why does android call my onSaveInstanceState upon switching to another activity if it doesn't intend to call onRestoreInstanceState when it switches back?

  2. Why don't I get onSaveInstanceState/onRestoreInstanceState in my Activity1 while Activity2 is visible? I must still save/restore data whether it's visible or not, yes?

  3. Where is the safest place to save/restore data in this case? And if it's NOT in onSaveInstanceState/onRestoreInstanceState, how do I access the bundle for restoration?

  4. Is there another solution like other callbacks I can take advantage of to alleviate this?

Thanks for any help!

Greg

like image 795
user695977 Avatar asked Apr 22 '11 18:04

user695977


People also ask

What happens to an activity when the screen orientation changes?

When you rotate your device and the screen changes orientation, Android usually destroys your application's existing Activities and Fragments and recreates them. Android does this so that your application can reload resources based on the new configuration.

Which method is called when screen changes orientation?

from portrait => landscape mode).

What is activity lifecycle What is the significant role of each state?

Activity Lifecycle: Activity is one of the building blocks of Android OS. In simple words Activity is a screen that user interact with. Every Activity in android has lifecycle like created, started, resumed, paused, stopped or destroyed. These different states are known as Activity Lifecycle.


1 Answers

This is a really good question.

Looking at the docs for onSaveInstanceState/onRestoreInstanceState their purpose (and therefore behaviour of the default implementations) is to save view state of any view which has an ID. Examples, if a checkbox is checked, which radiobutton in a radiogroup is selected and so on.

What is also stated in the docs is that the Bundle created/saved for onSaveInstanceState is passed to both onCreate and onRestoreInstanceState (more on this in a moment) when the Activity is 're-instated'.

I'm guessing that because Activity 1 is hidden at the time of rotation, onRestoreInstanceState isn't called because there is no 'view' (i.e., it can't be seen/viewed). Also, it is entirely possible to have 2 completely different layout files for portrait/landscape which potentially have different UI elements with different IDs.

As a result, I'd say if you want to use the Bundle in onSaveInstanceState to save your own data, then you need to add extra logic in your onCreate (in Activity 1) to process your own data there (as well as doing it conditionally in onRestoreInstanceState).

In particular, you could maintain a 'last known' orientation field so that onCreate knows that it needs to process your own data because orientation has changed, rather than relying on onRestoreInstanceState being called.

Does that make any sense? It's the only way I can interpret the logic.

like image 89
Squonk Avatar answered Oct 05 '22 22:10

Squonk