Android studio home button problem.
I have so many activities, when I press home buttom in any activities.
Then if I restart my app.
it started from splash activity(logo activity).
However, I want to start this from activity where I pressed home button.
Can anybody figure this out?
By default, Android handles this behavior. When you press the home button, the app should go to background and at the next time when you open it, it should start from where you left off. But, Android's memory management is designed to automatically terminate minimized apps that have not been accessed in a while when memory is needed for newly launched apps.
If there is enough memory available and still your app gets terminated, that means you are not using the API's correctly. Please read this [article] to know how to handle onPause() and onResume() to achieve this behavior.
Related
Clicking my app icon will sometimes take me to the launcher activity, but other times it will resume the activity where it left off (emulates the behavior of clicking the application in recents).
I've read (can't find concrete docs about this, other than a one off post from 3 years ago by Diane Hackborn) that after about half an hour, the OS will make sure that when you click the app icon, it will relaunch the app, but sometimes it seems to do this directly after I exit the application (with the home button, not the back button as that will call finish on the activity).
Why is this?
Is there any way to force the launcher activity in the Android OS without killing the app first then restarting it? I need to test how my app handles the launcher activity after the application may already be running.
Android will free up memory whenever it feels necessary. For that, it will kill inactive apps. If your app goes to background, at some point Android will probably kill it. Matter of time.
You can kill your app, or you can make it kill itself when it goes to background. No more options.
in order to make your app kill itself, you should override the onPause() method (which is always called when the app goes to background) and add in it a call to onDestroy().
I am working on a fitness app which has a home activity which launches a workouts activity which launches a specific workout activity. In the workout activity, one may start a workout. Thereafter, one might want to then press the Home button and launch a music player or perhaps the web browser. At some point, one would probably launch the app again to return to the already running workout, but that ends up launching a new instance of the app. When I set the launchMode on the home activity to singleTask, it simply goes back to the existing home activity when I tap the launcher icon. What I would like is for it to go back to the workout in progress, which is where you would depart the app.
Essentially, I'm looking for behavior identical to iOS where it would simply restore the app to its current state if you "relaunched" the app and it was still running.
It is supposed to work as you've described. In most cases, it actually does work like that. However, there is a long-standing nasty Android bug which causes the behaviour you've described. This happens when you launch the app for the first time from an IDE (like Eclipse) or by clicking the "open" button on the Installer screen. To see if this is what you're seeing, just do this:
Go to Settings->Applications, choose your app and click "Force close"
Launch your app, do something, press the HOME button
Launch your app again.
You should return to where you left off. If not, something else bad is going on. If that is the case, please post your manifest in your question, because the problem is likely in these.
Don't try to use special launchModes to fix this. This just creates more problems.
See this answer for more information about the nasty long-standing Android bug.
I'm working on large project, so there is some logic for saving application state, and then opening correct activity(fragment) when it comes from background.
But anyway, I've found that if user navigates through my app and then minimize it - android opens it from background in different ways in following cases:
User taps on app icon (behavior: user see home activity, doesn't matter where he was, when application was minimized)
User select app from android "recent apps" (behavior: user see exactly what he saw, when the application was minimized)
So, what is the difference between launching application from background by this two ways? I always thought, that it is the same mechanism, but, obviously, I was wrong.
Thanks for any answers
You should pay atention on the folowing docs Activity and Tasks. In short words: if user start app from recents you will receive onRestart before onStart (without onCreate it means that your app was just "suspended"). You able to save screen state using onSaveInstanceState(). But in general starting from icon and from recents - different application behaviors and you should provide proper code for this ways.
UPD
As described below root cause of unexpected behaviour was additional lunchmode attribute.
From what I experience as an Android user, both are same.
The difference we usually see is how we close the app
Press back button until app close / finish()
On this state no matter how we open the apps it will go to the main screen
Press Home button
On this state depend on the app. If the app does not handle any Activity the app will same with the first state. But if the app handle something like when onPause() the Activity then finish() the apps, then whatever you open with app icon or recent apps will provide the same result.
Correct me if I am wrong
In iphone when we exit app and start app again by clicking launcher icon the app start from screen which was open last time before exiting app.
We can achieve same in android when we exit app using center or home button on android phone. In android app if app is exited by pressing home button and started again by clicking on launcher icon the app start from activity which was open last time before exiting app.
Is that functionality similar to iphone in terms of keeping app in memory longer? Will that work in android all time because I think android system will remove app from memory after sometime or will it remain in memory longer? Can I depend on this functionality in android and expect it to work all time.
I have a project in android which client has asked me to have iphone like functionality if we exit app and start app again it should open from screen which was opened last time before exiting. Now this is possible in android only if we use centre button. And also if user has exited using centre button and started it again, app should check for user current location and do some other operations. If app is launched and user navigate to any activity is there any way to check if app was exited using back button or centre button so that I can run code if app is exited using center button.
Thanks
We can achieve same in android when we exit app using center or home button on android phone.
The HOME button does not "exit app".
The HOME button brings the home screen to the foreground, just as the CAMERA button (where available) brings the camera application to the foreground, the CALL button (where available) brings the dialer to the foreground, tapping on a Notification may bring something else to the foreground (e.g., SMS client) based on the Notification, etc.
In android app if app is exited by pressing home button and started again by clicking on launcher icon the app start from activity which was open last time before exiting app.
No. If you tap on a launcher icon, and the app's process is still in memory, the existing app instance will be brought to the foreground, returning you to whatever activity you had been on. If you tap on a launcher icon, and the app's process had been terminated to free up RAM for other apps, you launch a fresh copy of the app and bring up whatever the ACTION_MAIN/CATEGORY_LAUNCHER activity was that the user tapped upon.
Will that work in android all time because I think android system will remove app from memory after sometime or will it remain in memory longer?
The length of time that a non-foreground app's process will be in memory is indeterminate and will be based on what is going on with the device, plus the device capabilities (e.g., how much RAM). I suggest you read more about the process lifecycle.
Now this is possible in android only if we use centre button.
It is not possible "if we use centre button" (what Android developers refer to as the HOME button). It may happen automatically, but if the app's process has been terminated, it will not happen automatically.
If app is launched and user navigate to any activity is there any way to check if app was exited using back button or centre button so that I can run code if app is exited using center button.
You should not care whether the "app was exited using back button or centre button".
You should care whether you have your data and how old that data is, refreshing it if it is stale.
Whether the user left your app via HOME, BACK, CAMERA, CALL, a Notification, the recent tasks list, an incoming phone call, by smashing their phone to bits with a rock and replacing it with an exact duplicate, or by any other means, should not matter to you.
To draw an analogy, think of a Web app. In a Web app, you care about whether you have a session cookie and whether that session is stale (e.g., to force a fresh login). Whether the page request came because the user clicked a link within the app, or clicked on a link from a third party site pointing to your app, or refreshed their page, or used a bookmark to get at another page in your app, or double-clicked on a desktop icon that brings up your app, or right-clicked on a link and opened a fresh tab, or anything else, should not matter to you.
Review the Android Activity Lifecycle at http://developer.android.com/reference/android/app/Activity.html. If you do nothing, you have no guarantee of your app starting at the same point it left off, however you have control here. You can, for example, overload the onPause() method to save your state to a file, and onResume() to restore it.
hi guys i am finding the strange behaviour in android while launching the Application. Let me explain the senerio. I am launching my application from android's launcher page and my application starts and runs fine and after few minute i press home button and go to android home page and then go to launcher page and again select my application and it is starting it again from first but it should have resumed from the last place where i left. And when i press back button on the launch screen of second instance of my app i am able to go back to the last page where i have left. I am more confused about what was happening and it too happens sometimes only not every time. Hope you people could help me sort this problem, Hoping for better responses. Thanks in Advance.
Edit #1:
It is not happening in all the device it happens only with Samsung and Sony but works fine with LG and HTC.
To keep an activity running in the background is not in your hand. When you press the home button, your current activity goes to the background and can be killed (onDestroy() will be called) at any time depending on the need for memory of the other applications you launch.
The more apps you launch, the more chances of killing your background app is.
The behavior may be device specific - try saving your game settings in a persisted location within the 'onPause()' function, and retrieving it on 'onResume()'. Then it doesn't matter if a new activity gets launched or the old one gets called.