I have an application.In that if we press on home button app closes but when i launch the app it resumes where i stopped.I mean it do not closed completely.how to solve this problem.
When you press Home button, onDestroy method of the current activity is called. You can perform any shutdown operations there. Design of Android doesn't have a concept of explicit application shutdown, so that the user can continue on the same activity where he started.
You are trying to copy desktop application behavior (the application is shutdown explicitly) to Android with different usage patterns. While this is understandable, in most cases this will contradict to other applications behavior and will annoy users. So if you have anything to shutdown, do this in onDestroy method.
You can make your android app return to the root activity each time you open it by modifying your AndroidManifest.xml to include
android:clearTaskOnLaunch="true"
in the desired <activity> declaration.
Android does not allow you to terminate the application at any time. Application lifecycle is maintained by the Android OS itself. You are not supposed to meddle with it. Unlike desktop applications, Android application lifetime is determined by the OS itself. You can only end an activity.
For more Info refer to this bug http://code.google.com/p/android/issues/detail?id=1572
You need to call finish() in your onDestroy() method.
Related
I didn't use any services in my application and closing the application by using
this.finish();
but my application still not stopped properly and it is running in background.when i go to application settings the force stop button is still enabled.
kindly share your views on proper exit of android application.
It's quite possible you have another activity around.
From the Android docs, see Activity.finish():
Call this when your activity is done and should be closed. The
ActivityResult is propagated back to whoever launched you via
onActivityResult().
There's no promise made that the activity will be closed right away on calling finish(), only that this is something that should be done. Usually this does happen right away, but without seeing your project I cannot comment further.
Note that Android, unlike iOS, doesn't really have a well-defined notion of an app. "Apps" can share activities and so on. For example it's not hard, but it's also non-trivial, for an "app" to know that it will go to background or that it has resumed.
How can I detect the start of my application from a completely terminated stated (as if the user went to Manage Applications and forced stopped the app)? I don't mean like a first-time start up when the user first installed the app nor do I mean just in an Activity by using onStart() or onCreate().
I'm not sure exactly what you're asking, but Application.onCreate() will be called when the application is starting, before any other application objects have been created. So you could use that to detect when an app is started from a non-running state.
There's no way to detect when your app is "force stopped" from Android settings > apps page, or if the app is killed by another app. In both cases, the UNIX process is killed, and this severs the normal Android lifecycle.
Here's a post from dianne hackborn on the topic.
I am making an application.
Now what i want is that whenever user Press HOME key on android, it does not go in Background Mode.
Can any body give any suggestions how to implement all that?
Please Reply.
Thanks a bunch in advance
You cannot (easily) force your app's process to exit, nor should you. You can force an activity to disappear from the stack when the user navigates away from it by adding android:noHistory="true" to the manifest for that activity. But that will apply even to another activity in your app. You can also add android:finishOnTaskLaunch="true" to force an activity to be closed down if the user launches it again from the home screen. However, the activity will run until then (or until the system shuts down the process for other reasons).
Android naturally keeps your application alive, but calls the onPause because it's not an active activity, then calls onResume when the user goes back to your application.
If you need to run code without a UI or you want it to be checking something then opening your app at some point You can use a service.
API Demos have some great examples of how to use a service
Hi I have application with more than 20 activities.I want to close my app when Home button is pressed.
There is no notion of "close my app" in Android. Android will get rid of your activities, followed by your process, after some period of inactivity by the user.
You could use the launchMode and clearTaskOnLaunch flags on your root activity from your AndroidManifest.xml:
android:launchMode="singleTask"
android:clearTaskOnLaunch="true"
When you again start your app, all activities will be killed.
You don't want to do System.exit() -- that's not how the Android Activity Lifecycle normally works (read this also).
What you should do is move the App to the background with moveTaskToBack(). Android will then keep your app running in the background, and kill it if it's unused and something needs its resources later.
If you want it to close all of the open Activities when your App is no longer visible, you can set noHist = "True" for all of the child activities (leave the main activity with noHist = "False", though). This will make it where instead of reopening your application on the last Activity they were on, it will open it on the "main" activity (i.e. it will be as if they just restarted the app).
Anyhow, read through the following answers for more information: Close application and launch home screen on Android
I have the same problem. Im writing a banking app and am required, by contract, to log off the user (or exit) when the app is put into background. There are obvious security concerns there.
There are a couple of ways Im looking to do this:
1. Intercept home button (and back button for the root activity) key press events to call logoff and/or finish()
2. In the onStop() method, for every activity, detect whether the activity is being stopped due to a new activity being show - if not, assume app is being put to background so logoff and/or finish()
The first may not work if a notification is brought to the front then the user clicks home (I havent investigated yet). Or maybe there are other ways for an app to be put into the background without pressing these buttons
The second way sounds messy & difficult to maintain
Id welcome any other ideas
Drapes
I know android has been designed this way, but it seems naive to think that apps wouldnt want an applicationOnStop event
Hi guys what I understood is that u need to know when app goes in background and how to detect it and if I am wrong plz correct me----
The user can go in background if ur app does not provide any way by pressing Back key or Home Key.
You need to use methods "dispatchKeyEvent(KeyEvent event)" to get home key event or back key event and after getting the event you can execute your task.
you can even restrict user from pressing any key but u can not control the home key.
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