Search code examples
androidandroid-activityandroid-fragmentsandroid-lifecycle

Simulate Activity/Fragment re creation


I am trying to simulate Activity and Fragment re-creation and also to check onSaveInstancestate() and onRestoreInstanceState() and generally so check if I am handling activity re-creation in a good way in all cases, for example, just like when screen rotation causing the activity to re-create it self.

But in my case I want to check more options/cases which can cause re-creation because my app cannot be rotate(all activities are in portrait).

I saw many articles, blogs and stackoverflow question/answers about this topic, for example here, here, and here.

And as this stackoverflow answers says Why not use always android:configChanges="keyboardHidden|orientation"? there are many more events which can cause activity re-creation, so after I read it I wanted to test my app for some of those events.

For example I pressed the home button in my activity and then I went into settings and tried to change the language, change the font size , etc... , but non of those actions made my app re-create as I would expect. When I returned to my app , it just resumed and onCreate() never called. So I even check the official documents about this. and they also says that it should cause my activity to re-create: Quoting:

"When a configuration change occurs at runtime, the activity is shut down and restarted by default" 

but as I said it didn't happen to me.

This is I huge for me because I was very naïve and thought that if my app will be only in portrait or if I will add to the manifest this line :

android:configChanges="keyboardHidden|orientation|screenSize"

then every thing will be ok and obviously its not because there are many more configurations changes which can restart my activity, so I can't run from it anymore and I want to handle it in a good manner and now I want to also test it.


Solution

  • Changing the device language is one way to force re-creation of all activities that doesn't involve orientation change.