Simulate Activity/Fragment re creation - android

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.

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

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.

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.

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.

Related

Stopping the activity from recreating when resumed

I got two activities. When I jump from first to second activity and then come back to first one, the activity gets recreated in some phones. Can anyone tell me how to prevent it?
I don't believe you can prevent it. If you have some state to save, consider saving it. If you need things to keep running in the background, consider using a Service.
You can't stop the recreating from an App, but you can save your state in onSaveInstanceState(Bundle) and retrieve the state in onCreate(Bundle) or onRestoreInstanceState(Bundle).
It's the intended behaviour, and you shouldn't be trying to stop it. But, as mentioned by gnobal, you will need to save the state, and recreate your data to handle these situations properly.
Some phones with low memory, will always do that. A good way to test your implementation is to go to "Developer options" and enable "Don't keep activities".

Multiple classes, onCreate saved instance issue

Ok, so quick explanation. I have an app that consists of 3 files.
Main.java, License.java ,App.java
For an example. This app does function 100% except for what i am about to ask. This app does contain a webview and in here lays the issue. I have researched and most everything points to orientation changes. I am not having a problem with orientation because i force landscape at the moment. I am having an issue with the phone going to sleep.
When it wakes, it reloads the page inside the webview. I can add a lock for that, but if someone manually locks their phone, then unlocks it, it will be the same response. Or can you manually lock it if the force wake is in the app? Guess i need to test that one.
Anyways, i am guessing the saveinstancestate will remember the location in the webview and reload to it? If so, which of the oncreates does it go in?
Main is launched when it first opens, this one calls the license, if it passes it then opens the application
Do you do anything special on onPause and onResume? When the phone goes to sleep, as far as I know only onPause should be called, and when woken up onResume. Maybe you load the webview content in onResume?
Also, if you want to share some data amongst your activities, you could try using an Application context instead. I.e. in the AndroidManifest file define a class to be used as application (just like you do with activities, with the "android:name" attribute). The class of course should be a sub-class of "android.app.Application".
See also:
http://developer.android.com/guide/topics/manifest/application-element.html
http://developer.android.com/reference/android/app/Application.html

How to resume Android app where it was suspended (app not Activity)

How can I have my Android app resume where it last was, i.e. the Activity in view, etc.? If I am showing an Activity and press the Home button and then launch my app again it returns to the startup activity. I'd like it to work like an iPhone app where it suspends in place and resumes back to where the user last was.
Thank you.
You may need to set android:alwaysRetainTaskState="true" for your root activity in the manifest. From the documentation:
Whether or not the state of the task
that the activity is in will always be
maintained by the system — "true" if
it will be, and "false" if the system
is allowed to reset the task to its
initial state in certain situations.
The default value is "false". This
attribute is meaningful only for the
root activity of a task; it's ignored
for all other activities.
Normally,
the system clears a task (removes all
activities from the stack above the
root activity) in certain situations
when the user re-selects that task
from the home screen. Typically, this
is done if the user hasn't visited the
task for a certain amount of time,
such as 30 minutes.
However, when this
attribute is "true", users will always
return to the task in its last state,
regardless of how they get there. This
is useful, for example, in an
application like the web browser where
there is a lot of state (such as
multiple open tabs) that users would
not like to lose.
Be sure to save any information you want in your onPause method and check that information in onResume. This will let the app reload any information that was communicated. Some widgets keep state but others don't.
Additionally when an an activity is killed it's onSaveInstanceState method is called. So anything that needs to be stored to persistent memory should be done there.
Although i'm working with Mono for Android i had a similar question.
After some time researching on the web you can setup an Activity to be created using the singleton pattern. This will cause your app to only create one instance and - if you go back to the activity - this will load the last state of that activity for you.
You can do this by configuring the activity with the
android:alwaysRetainTaskState="true"
and the
android:launchMode="singleTask" //(preffered, or "singleInstance")
properties.
Singletask is the best since singleInstance is more suitable for single-activity-based apps.
In my case setting the launchmode made this baby rock as i wanted but i don't know if this is a Mono for Android or Android issue at the root..
Be aware: you will need to create an initialize method or something alike if you'd like to create a clean/fresh/new instance of the activity at some point.

Android app activity always restarts

Often when I open my Android app from either my Home screen shortcut or from the apps menu it restarts the main activity.
How can I prevent this behavior and make it bring the existing instance to the foreground if there is one running?
I've searched quite a bit to no avail. It would seem as though most others are not having this problem?
Thanks!
You can never assume that Android retains the state of your app after you move away from it - because of a low memory situation, it may have removed it from memory.
Your best bet is to store the current state (including the current activity) in your SharedPreferences, and in your main activity (the one marked as LAUNCHER in your manifest), you check the shared preferences and go to the activity marked there. You can also use the SharedPreferences to store other information about the activity, i.e. you can use it to fill out text fields and other things with whatever values they had when the user left.

Android: Determine when an app is being finalized vs destroyed for screen orientation change

I am relatively new to the Android world and am having some difficultly understanding how the whole screen orientation cycle works. I understand that when the orientation changes from portrait to landscape or vice versa the activity is destroyed and then re-created. Thus all the code in the onCreate function will run again. So here's my situation: I have an app that I am working on where it logs into a website, retrieves data, and displays it to the user. While this is all done in background threads, the code that starts these threads is in the onCreate function. Now, the problem lies in that whenever the user changes the screen orientation, the app will log in, retrieve the data, and display it to the user again. What I would like to do is set a boolean that tells the app if it is logged in or not so it knows whether or not it must log in when the onCreate function is called. So long as the app is in memory the HttpClient will exist and contain the cookies from logging the user in but when the app is killed by the system those will go away. So I would assume that I need to do something like setting the logged in boolean to false when the app is killed but since onDestroy is called when the screen is rotated how is this possible? I also looked into the finalize function and isFinishing() but those seem to not be working.
Shorter version: How can I distinguish between when an app is being killed from memory from when an activity is being rotated and different code for each event?
Any help or a point in the right direction is greatly appreciated. Thank you!
Matt, you need to tell the platform that your activity can handle orientation changes so that there's no need to restart it.
To do this, add android:configChanges="keyboardHidden|orientation" to activity declaration in AndroidManifest.xml. More info here.
As alex pointed out you can tell android to not restart your activity when doing orientation changes... When you have to restart your application (maybe screen orientation changes your render context etc...) you can get informed of the changes by overriding onConfigurationChanged(..) in your Activity.

Categories

Resources