I have strange behavior in the application when changing orientation.
Normal behavior:
When I open my application, my home activity begins. When I move on to the next action (gallery), it starts up normally (with animations expanding from right to left). When I get back using the rear key, the current activity (gallery) is over (with a shift-animation from left to right).
Strange behavior:
- When I launch the application in portrait mode and change the orientation to the landscape. Then there is something like a second instance of homework. Because then pressing the "Back" button in landscape mode does not close the application, as if it had not changed without changes (home activity is the first activity in my application), but rahter makes a sliding animation from left to right (for example, starting from a new action) and shows activity at home (but I think one more instance) again. Pressing the back button again closes the application.
- When I launch the application in landscape mode and change the orientation to portrait mode, click the "Back" button to play the slide animation from right to left (for example, close the event) and show home activity again.
- When I launch the application and make two changes in orientation portrait-portrait, then the "Back" button closes the application, as it should be.
Thus, it is similar to landscape and portrait modes, considered as two different actions.
I do not use android:configChanges="orientation|keyboardHidden|screenSize"
, so the change in orientation should correspond to the normal life cycle of the android and destroy the "old" portrait (or landscape) version of the action.
My activity inherits from FragmentActivity
. I use onSaveInstanceState
to pass a parseable (which does not contain an activity reference) and I use onRetainCustomNonConfigurationInstance
( here. ) To go through several AsyncTask
s. But all links in these tasks (if any) are destroyed in onRetainCustomNonConfigurationInstance
and restored (with newly created activity) after getLastCustomNonConfigurationInstance
.
Any ideas what might cause this behavior?
EDIT:
Declaring activity in the manifest file:
<activity android:name=".activities.smartphone.HomeSmartphone" android:label="@string/app_name"></activity>
HomeSmartphone Expands Home
Home extends MyFragmentActivity
MyFragmentActivity extends android.support.v4.app.FragmentActivity
In MyFragmentActivity, I just do some entries / tracking in onCreate, onRestart, onStart, onSaveInstanceState, onPause, onResume, onStop, onDestroy, calling some static methods of the tracking class that just contains a reference to the application context. Not for context of activity.
Home is an abstract class that is extended by HomeSmartphone and HomeTablet. These two classes only perform some special loading / updating / initialization in different layouts.
Most tasks are performed in the abstract class Home.
public HomeRetainedObjects retained = new HomeRetainedObjects(); public boolean adIsShown = false; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.setContentView(R.layout.home); Log.i("DEBUG", "onCreate(Home)"); if (savedInstanceState != null) { this.adIsShown = savedInstanceState.getBoolean("adIsShown"); } // write/update values in shared preferences this.initPreferences(); // recover retained objects (mostly AsyncTasks) this.recoverRetained(); // show content / refresh content this.init(); } @Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); outState.putBoolean("adIsShown", this.adIsShown); Log.i("DEBUG", "onSaveInstanceState(Home)"); } public void recoverRetained() { Object retained = this.getLastCustomNonConfigurationInstance(); if (retained instanceof HomeRetainedObjects) { this.retained = (HomeRetainedObjects) retained; if (this.retained.loadMessageTask != null) { this.retained.loadMessageTask.restoreContext(this); } } } @Override public Object onRetainCustomNonConfigurationInstance() { if (this.retained.loadMessageTask != null) { this.retained.loadMessageTask.destroyContext(); } return this.retained; }
Hope this helps ?!