3

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.

Zoe
  • 27,060
  • 21
  • 118
  • 148
Elizabeth
  • 95
  • 1
  • 7
  • How are you asserting that your activity has or hasn't called onCreate? Debug? Logcat? – Ben Pearson Nov 07 '14 at 16:08
  • Logcat - when I press on home button then onPause() -> onStop() are called. After my test(no matter which one, for example, change font size from regular to huge) I long press the home button and then press on my app and then onRestart() -> onStart() -> onResume() are called – Elizabeth Nov 07 '14 at 16:14

4 Answers4

5

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

JHH
  • 8,567
  • 8
  • 47
  • 91
0

You said that it wasn't being recreated when you change the orientation of your device while in the app. Normally it would. When you added the lines android:configChanges="orientation" it means that you're telling the system you will deal with orientation changes in your app and not to worry about the normal behaviour.

If you remove that from your activity declaration in the manifest, you'll see it operating as expected (destroying/recreating on orientation change).

Hope that helps.

Ben Pearson
  • 7,532
  • 4
  • 30
  • 50
  • You didn't understood what I wrote or I wasn't clear, but I said that I intentionally did it so there wont be any orientation so my activity will not get restart/re-create, but after reading stuff about the issue I revealed that there are many more cases that can re-create/restart my activity - please read again what I wrote and look at http://stackoverflow.com/questions/7818717/why-not-use-always-androidconfigchanges-keyboardhiddenorientation/7990543#7990543 – Elizabeth Nov 07 '14 at 16:19
  • What I want is to simulate more cases of configuration change which cause the activity to re-create/restart, not including orientation change. – Elizabeth Nov 07 '14 at 16:21
0

Pressing the home button will only initially cause onPause to be raised. Your Activity will not go through onCreate again until it has been fully destroyed, which won't happen unless Android decides it needs the memory for other processes.

The easiest way is just to remove orientation from configChanges and then rotate your Activity and see what happens. The layouts might not work correctly, but your should be able to inspect your lifecycle code. Similarly, remove keyboardHidden and then toggle the soft keyboard. Your Activity should go through the lifecycle sequence.

Changing the locale for the device should cause your Activity to be recreated. Note that this is the global locale that needs to be changed, not the language the keyboard is typing in.

Other ways come to mind such as manually destroying the app either via a key sequence on the phone (for example, holding down the Home button and swiping left on the app to kill on some phones), or by terminating it from your debugger.

Jeffrey Mixon
  • 12,846
  • 4
  • 32
  • 55
  • What I want is to simulate more cases of configuration change which cause the activity to re-create/restart, NOT including orientation change. I also tried to change the device language but it didn't re-create my activity. You wrote: "...which won't happen unless Android decides it needs the memory for other processes" but this is not correct because ,as android says, there are many more cases which can cause the activity to re-create. Look here http://developer.android.com/guide/topics/manifest/activity-element.html#config – Elizabeth Nov 07 '14 at 16:25
  • If I will hold down the home button and then swipe left my app it will destroy the process and my app with it. I can also use the DDMS tool to simulate Android removes my app because of memory loss, read this: http://stackoverflow.com/questions/11365301/how-to-simulate-android-killing-my-process. I can also simulate this using developer tools: http://android-er.blogspot.co.il/2013/04/simulate-kill-activity-to-test.html, but I already described it in my question and this is not what I want to simulate. – Elizabeth Nov 07 '14 at 16:28
  • A configuration change causes an `Activity` to go through the `onDestry()` and `onCreate()` lifecycle. It doesn't matter if it was caused by orientation, locale, or terminating the app manually--they all will have the exact same effect on your `Activity`. You do not need, nor is it feasible to, test every possible configuration change. – Jeffrey Mixon Nov 07 '14 at 18:27
  • I know, i just want one, not including oriantation change. And also why its not working like android official docs says. for example they said that changing the font size should restart the activity. – Elizabeth Nov 07 '14 at 19:06
0

On your device/emulator Developer Options, activate the option: "Don't keep activities".

With it on, anytime the activity is paused and returned the cycle for saved instance is called (e.g. another activity in front of it or home button pressed to minimize all apps, ...). It's useful to test and simulate recreation and related bugs.

Isan Campos
  • 321
  • 2
  • 5