In the main activity of my app, I have three tabs which, when clicked, switch the contentView of the activity to a different layout. My problem is that when the orientation changes, the first tab is automatically selected and loads the first layout.
My first thought on how to fix this would be to just save the tab location in the savedInstanceState or a sharedPrefs file, but the problem with one of these solutions is that they will also happen when the app is first opened and OnCreate() is called.
To be clear, I want the following to happen:
- On App load (fresh activity, onCreate() called)- Load first tab
always - On App resume (after app paused, Onresume() called)-Load last opened tab
- On orientation change(after change, OnCreate() called?)- Load last opened tab
Basically I am trying to find a way to distinguish between an orientation change (witch calls OnDestroy() and onCreate()) and the onCreate() and onDestory() when the app is first launched.
If its possible, I’d rather not but the configChanges=”orientation” line in my manifest, and handle the switch myself, because I’ve heard that can cause problems when dealing with different screen sizes and densities, etc.
Is there any way to accomplish this, or do I have to settle for a different behavior?
onSaveInstanceStateis the right way to go. It will not persist after the activity has been finished, only when it’s “going to a background state” or doing an orientation change.