Activity state only restored when launched from Eclipse - android

I have the problem that the state of my activity gets only restored (after killing or put to background) if I launch the App via the Eclipse Run menu.
If I export the App and put it manually on the (same) device, the state gets not restored when I kill and restart the App.
I assume I overlook something. Can anyone tell me what? Thanks!

May be an old thread, but this may help others.
This is a known defect
http://code.google.com/p/android/issues/detail?id=18338
Installing and launching app from eclipse, or launching from apps menu will restore the state, but if we are installing the app manually (by copying to SD card etc.) and launch directly will not restore the sate.

The android system automatically remembers the state when you close the app. You specifically have to kill it from Settings -> Applications -> Your app -> Kill

Related

Force full app restart in order to refresh packages in Cordova

I'm looking to restart the app completely from the app itself( really restart the app(full load) and not just rerender the index).
This needs to happen because some packages also need to re-initialize and this can only be done when fully restarting the app.
I've tried this package https://www.npmjs.com/package/cordova-plugin-exit which doesn't seem to work.
And navigator.app.exitapp() also isn't what i'm looking for or does this exactly do what I want it to do?
Is the best solution to create a cordova wrapper plugin which does this for android and IOS differently?
You can do this on Android with the restart() method of cordova-diagnostic-plugin:
// Warm restart
cordova.plugins.diagnostic.restart(null, false);
// Cold restart
cordova.plugins.diagnostic.restart(null, true);
By default, a "warm" restart will be performed in which the main Cordova activity is immediately restarted, causing the Webview instance to be recreated.
However, if the cold parameter is set to true, then the application will be "cold" restarted, meaning a system exit will be performed, causing the entire application to be restarted. This is useful if you want to fully reset the native application state but will cause the application to briefly disappear and re-appear.
Note: There is no successCallback() since if the operation is successful, the application will restart immediately before any success callback can be applied.
It is not possible to programmatically restart an app on iOS; at least doing so will likely get your app rejected from the App Store.

Is it possible to prevent device from destroying my android app on reboot?

I am making an android app. In this app I am trying to save my app from destroying on reboot or factory reset. can anyone help me out?
Apps won't get deleted if you reboot the device. About the second part(factory reset), the whole point of a factory reset is that everything on the phone will be deleted and restored to factory settings. If you want to keep your app even after that you'd have to install your own OS with the app installed as a part of it.
Also, if a reboot is somehow "destroying the app", something is very wrong with the app and you need to find out what it is.
Assuming you mean by "Save my app" that the app .apk should be still present and installed, thats impossible for a factory reset, like the name suggest its a reset.
If you want to save the settings of the app prior to rebooting, you can do that with a background service, which can be also called when the phone finishes the reboot.

PhoneGap App state not saved on pause

When ever I export Android App(using a generated key store from eclipse) application state is not saved when running it. Say I am in 3rd screen, then if I go back to home and click on App drawer and launch it, it starts all over again. But if the app was installed from eclipse itself (through USB debugging) app behaves normally and state is saved.
Weird issue ! What might be causing this ? I checked developer options and activities are not getting killed as well on exit.

How can you easily test the save restore methods in android

I know that if you have to test the onrestoreinstance() the system must kill previously the application. Is there any easy way to test that? I mean not going to open tons of other applications for the system to kill it.
If your AndroidManifest.xml does not block Activity's kill-restore on device rotations, then you can just rotate the device (or emulate device rotation if on emulator).
There is also another cool tool - on the emulator by default you should have 'Dev Tools' app installed. Open it and go to 'Development Settings', then check the 'Don't keep activities' checkbox. Since this moment the activities will be killed by OS as soon as they become invisible (in the background). So pressing 'Home' button will cause a kill for your activity. Then on the home screen pressing your app icon will restore your activity.
You could open your app then kill in the via the home menu.
Device menu
Applications sub menu
Manage Applications
Find application and kill it.
That should help ya out.

How to simulate killing activity to conserve memory?

Android doc say:
"When the system, rather than the user, shuts down an activity to conserve memory, ... "
But how to simulate this situation?I want to debug the onRestoreInstanceState(Bundle) method,but don't know how to.
You can't do it in an automated way b/c its completely non deterministic.
See my answer here: https://stackoverflow.com/a/15048112/909956 for details.
But good news is that all you need to do is just simulate calling onSaveInstanceState and you are indirectly testing this low memory situation.
onSaveInstanceState can be triggered by:
losing focus (by pressing home which in essence is like switching from your app to launcher app), launching another activity, pressing recents
changing orientation. this is the easier way if you are using an emulator
changing developer setting: goto developer options --> Apps --> Don't keep activities. This is best option if you are testing temporarily on an actual device.
I've used the "Don't keep activities" developer option to reproduce a crash that happened when an activity was killed due to memory pressure. You can find it in the Apps section of Settings->Developer Options.
It destroys every activity as soon as you leave it. E.g. if you press home to put your app in the background the current activity is destroyed. See https://stackoverflow.com/a/22402360/2833126 for more information.
There's two way to simulate the android killing process: using the setting "Don't keep activities" in developer settings or killing the app process by yourself.
To kill the process, open the activity you want to test, then press home button to send your app to background, and then, using the DDMS in Android Studio (Android Device Monitor), select the process and then stop the process (as seen in the image below). Your app was killed. Now, open your app again (accessing the list of open apps). Now you can test the killed state.
For the purposes of debugging onRestoreInstanceState(), just change the screen orientation ([Ctrl]-[F11] in the emulator). Your activity will be destroyed and recreated, and the onSaveInstanceState()/onRestoreInstanceState() pair will be invoked.
Use the SetAlwaysFinish app (works on a real device and in the emulator) or use the Google DevTools app (works in the emulator only).
These apps use the hidden AlwaysFinish setting of the ActivityManagerNative class to change the behavior of the OS and cause it to immediate unload every activity as soon as it's no longer in the foreground. This will reliably trigger the onSaveInstanceState and onRestoreInstanceState events.
See link below for more details:
http://bricolsoftconsulting.com/how-to-test-onsaveinstancestate-and-onrestoreinstancestate-on-a-real-device/
To debug onRestoreInstanceState you could do the following:
make sure you can debug application right after its start (calling android.os.Debug.waitForDebugger() from your constructor helps, it hangs your application until debugger is connected),
put you application in some state,
causally kill it from Settings->Apps,
causally switch back to it through Recent Apps button (it will still be in the list),
at this moment your application will be started anew and onRestoreInstanceState will be immediately called on the top activity.
Good answers here.
Now, residing in the distant future, using Instant Run in Android Studio will also trigger a save and restore when activities are restarted with code changes.
There's a decent solution for this in Android 6 and newer. See my answer here: Simulate killing of activity in emulator

Categories

Resources