Save activity state in android when home button is pressed

I have a problem with my home key. When I press the home key, the current activity is hidden, but when I run the executable again, it starts from the first screen. I have overridden all methods (onSaveInstanceState, onPause, onStop onResume), but there are still problems. What is the right way to handle this?

When I click the home button, I save the state of the widget in the database, and when I start exectable again, I check the status of the database and start a new intention, which was saved when the user clicked the home button. This is the right way to do this, since I ran into a problem when the user does the same thing and presses the back button again, there is already an intention available in the action stack.

+1
java android android-activity
source share
2 answers

I struggled with this odd behavior for over a month, but I finally figured out the explanation as a result of trial and error.

This behavior occurs when you launch the application from Eclipse, from the command line, or if you install the application and click the Open button (instead of the Done button) to launch the application immediately after installing it.

If in one of these cases you run your application, go to Activity1, and then to Activity 2, click the HOME button and then click the application icon and it will open a new instance of Activity1. Do not take my word for it. Just hit BACK and make sure it takes you to Activity2, which you left when you clicked HOME.

It seems that the launch activity does not fit into the activity stack if the application is launched in one of the above ways, so it creates a new instance of the launch activity on top of the current actions in the application stack. It looks like an error.

So, a workaround would be to exit the application, the first time you start from Eclipse or the command line or Open button, etc., by pressing the BACK button as many times as necessary, and then enter the application again. From now on, the behavior will be as expected.

+5
source share

From what you described, you probably redefined android: launchMode in AndroidManifest.xml or if you are testing "run as" from Eclipse, try to exit the application after installation and automatic launch. Then start again with the emulator and check the behavior of the Home button. I believe this is due to the fact that Android does not launch Activity on the OS stack when it starts with Eclipse, and then the behavior of the Home button is not as usual. If this does not solve your problem, try reading http://developer.android.com/guide/topics/fundamentals.html#lmodes .

Hope this helps.

+4
source share

All Articles