Don't Keep Activities - What Is It For

Don't keep activities - What is it for?

I believe it's a feature used for debugging purpose.

From the Titanium doc:

Don't keep activities under the Developer Options menu. When this
option is enabled, the Android OS will destroy an activity as soon as
it is stopped. It is intended to help developers debug their apps. For
example, it can simulate the case that Android will kill an activity
in the background due to memory pressure. In normal use, it is not
recommended to turn this option on because this may lead to unexpected
issues on the apps, such as freezes, force closes and reboots.

It sounds like it basically helps testing deterministically how your app behaves when the OS shuts it down due to any reason (out of memory and so on).

So, this replied to point 1. Point 2 is: Yes, I guess :)

EDIT: further references

  • On SO How to know "Don't keep activities" is enabled in ICS?
  • an interesting thread about that on androidcentral (reply from MagouyaWare)

What is the difference between Don't keep activities and a Configuration change?

When a configuration change occurs, the Android framework will call onRetainNonConfigurationInstance() on your Activity. You can return any object you want to. Then Android will destroy your Activity and immediate create a new instance. In onCreate() of the new instance you can call getLastNonConfigurationInstance(). If the Activity was recreated due to a configuration change, the object that you returned from onRetainNonConfigurationInstance() will be returned here. Otherwise the call returns null. In this case you can tell when your Activity was recreated due to a configuration change, and when it was (re)started for other reasons.

The developer option "Don't keep activities" is not something that you usually have to deal with, as normal users should never have it enabled. You can use it for testing purposes to ensure that your Activity properly recovers in case Android decides to kill it off.

In reality, Android usually doesn't kill off individual activities. If Android needs to recover resources from background applications it usually just kills off the entire OS process. However, I have recently seen some situations on some devices where Android does indeed kill off individual activities when an app is in the background. When the user then returns to the app, Android will recreate the Activity. In this case you will get null returned from getLastNonConfigurationInstance(), but you would get a non-null Bundle in the onCreate() call and you would also get a non-null Bundle in the onRestoreInstanceState(). So you should be able to tell the difference between:

  • Activity created for the first time
  • Activity instance created after configuration change
  • Activity instance recreated after Android killed it off and user returned to it

See https://developer.android.com/reference/android/app/Activity.html?hl=en#onRetainNonConfigurationInstance()

I'm not exactly sure of your requriements, but this information should help.

Whats the main advantage and disadvantage of do not keep activities in android

How much this option will affect mobile applications?

If they are well writen this option will not affect them.

What exactly does this do?

If you have this option turned on only variables kept in activity with method onSaveInstanceState
will be saved when you go to another activity or app goes in background. All other variables will be removed immediately.
When this option is off there is possibility that these variable will be kept

Does that mean if I open an app and as soon as I leave it, it actually closes that app and I wouldn't see it in the task manager to manually kill it?

No it means that all not kept variables will be removed. When you in example press home button.

Does this create any positive and or negative functionality on my apps?

No it only helps to develop application properly. It helps to predict unexpected situations.

Don't keep activities option not working

Killing an Activity does not mean Closing the app. It just destroys the activity and frees the device resources. When you press back button, the previous activity is recreated again as if it were opened first time.

Don't keep activities option in Android Studio?

This is an option in the device itself. It is under the Developer Options menu - near the bottom under the Apps heading.

If you have not already, you will need to enable the Developer Options menu by clicking a bunch of times on the Build Number in About Phone under Settings.

Don't keep activities - Is it possible in real scenario?

When a user uses an application like "Clean Master" to free up their RAM. This cleaner clears out memory used by the app but process itself is still running. When the user comes back to the application the onCreate() for the activity he was last in is called.



Related Topics



Leave a reply



Submit