I have an application which I would like to be fully disabled/closed when it is paused (IE. When the user presses the Home, End (call) and Back button I would like the application to be closed, instead of being saved in the history stack).
How do I do this....?
Thanks.
Implement onPause() in your activity and call finish() on your activity. Bear in mind, though, that this will occur on every pause, including dialogs, incoming calls, users activating a Notification. You might want to consider doing finish() in onStop(), which would at least solve the dialog problem.
Also, bear in mind that users will may get confused when using your app, thinking it has crashed since it is gone when they try to get back to it.
you can easily do that by setting true the "noHistory" attribute in to your activity element, in the manifest
http://developer.android.com/guide/topics/manifest/activity-element.html#nohist
You know how you have an OnCreate() method in your activity which performs actions when you start. You need to add something like:
#Override
protected void onPause(){
finish();
super.onPause();
}
in your activity to add actions before it starts
in this case the
finish();
command is what you want to execute before your activity pauses.
Related
I have an app sort of like a browser, so I don't control the javascript that runs inside the pages, some of it might be looping like crazy and wasting battery. Also video or audio might be playing. I want all that to pause when the user leaves my app for another app. To do this I call webView.onPause() and webView.pauseTimers(). I tried only doing onPause but depending on what pages I had loaded my battery would still run down. As you might know, pauseTimers applies to all webviews so that means any ads I might have stop loading.
So for this reason I want to call pauseTimers only when my activity is being paused because the user is leaving the app, but not when the user is leaving my activity to go to another activity in my same app, because it might have ads.
So can I know when my activity is being paused because the user is leaving the app?
Thanks.
You should override Activity.onUserLeaveHint()
Called as part of the activity lifecycle when an activity is about to go into the background as the result of user choice. For example, when the user presses the Home key, onUserLeaveHint() will be called, but when an incoming phone call causes the in-call Activity to be automatically brought to the foreground, onUserLeaveHint() will not be called on the activity being interrupted. In cases when it is invoked, this method is called right before the activity's onPause() callback.
http://developer.android.com/reference/android/app/Activity.html#onUserLeaveHint()
Have you tried re-implementing the onPause() method? You can analyze the stacktrace in order to get some clues.
Also, this link might help you:
http://developer.android.com/training/basics/activity-lifecycle/pausing.html
Override the onPause method in your actvity.
#Override
protected void onPause() {
super.onPause();
//Do whatever you want to do
}
I am having an error state in my app , so when the user doesn't have internet enabled view an xml to inform that he should connect first. The problem is that when he enables internet and tries to connect he might gets a force close. I do not know why is this but I think that if in my error state screen add code for killing the activity on exiting will help me solve this. My question is rather simple. Do I need both of them? Or only of them? Add anything else?
#Override
protected void onStop() {
super.onStop();
// The activity is no longer visible (it is now "stopped")
finish();
System.exit(0);
}
#Override
protected void onDestroy() {
super.onDestroy();
// The activity is about to be destroyed.
finish();
System.exit(0);
}
The flow of my app is this: user enters the app, check if is online. If yes go to the main screen and everything goes according to the plan. If now go to the error state. So, if called, the error state will be the first activity to run (after the launching one).
EDIT: I just want to inform user that there is no connection, so please try again and because of this kill all the activities running (This is the only one actually as if it runs it will be the first). So next time he enters the app, start from the beginning not from that point that he was earlier.
That depends. OnStop and OnDestroy have two different purposes. You should surround what ever it is that may error with a try/catch to avoid fc
#pseudo code
Try:
Make a connection
Catch
Dialog to alert that there is no connection
super.finish ()
Never use
System.exit(0);
Let the main activity launch finished, then check connection. If there is connection, everything is fine. If not, pop up an AlertDialog which call finish() onClick.
Is there any problem in finishing main activity when there is no internet connection? and also when the main activity get finish , after re launching it will start from beginning.
well refer this thread :
How to close Android application?
How to stop application from running in background in android?
I want my application to start fresh everytime it loads. How to do it programatically.
Override onStop method of your activity:
#Override
public void onStop(){
super.onStop();
finish();
}
But I think it's a bad idea to restart your app each time. It's better to override onStart method, and handle "restart" here.
Actually, your app doesn't "run" in background. Android OS keeps it in memory, or saves state of your activity to device (and then you can load it, using savedInstanceState param in onCreate method).
You can use onResume event to reload again, or look here.
EDIT:
Actually you need to use these functions to reload your application when user navigate it.
After adding finish();
This code will completely stop the application.
System.runFinalizersOnExit(true);
System.exit(0);
android.os.Process.killProcess(android.os.Process.myPid());
The whole Android ecosystem is based on the fact that the user shouldn't have to worry about "terminating" or "starting from scratch" an application. If you need to start your application from scratch every time, that's probably because you have tasks in your "scratch" that shouldn;t be there, and should probably be somewhere in onResume.
Please give us more details if you want a more detailed answer.
you should make use of Intent.FLAG_ACTIVITY_CLEAR_TOP to finish all other activities running in activity pool and call the first activity where you can ask to exit from app
In my android application I have noticed that on press of back key, after onPause() automatically, onDestroy() is getting called.
How do I prevent the application from calling onDestroy() after onPause()?
I dont want to destroy the instances after back key press.
On press of Back key, my webview object is getting destroyed. So, I am not able to access the webview again,once I press back key.
I have got two URLs. Imagine them as URL1 and URL2.
When I launch URL1 in the webview and press back key the flow is onKeyDown() -> onPause()
When I launch URL2 in the webview and press back key the flow is onKeyDown() -> onPause() -> onDestroy()
Why is there a difference in the behaviour? Has it got anything to do with cookies?
For URL1 -- cookies.isSecure=true;
For URL2 -- cookies.isSecure=false;
Check the declaration of your activity in Manifest. Declare it in such a manner that it can store the data when minimized.
Activity should have the capability to restore the content.
That's how the Activity lifecycle is designed in Android and you should not interfere with it. You app can be destroyed by runtime at any point of time regardless of you want it or not :) So a better approach would be to accommodate the lifecycle in your application's logic.
you can override finish() to avoid this:
#Override
public void finish() {
//super.finish(); // do not call super
moveTaskToBack(true); // move back
}
you can override onBackPressed() in your activity, but that should be the last resort,
get a hang of these links before you start
activity lifecycle
developer blog
Normal android application behaviour states that the Home button hides the app to the background (you'll be shown the home screen), and Back button finishes the application (goes through onPause, onStop, onDestroy, in that order).
If you want to retain instances/states of your application when back button is pressed, I suggest you do the saving in onPause - save the states in SharedPreferences or in a place in your sqlite db. But mind that the saving process should be as quick as possible, because your application will wait until onPause executes completely before exiting.
Then load your instances/states in onResume.
You can know that onDestroy() will be called after onPause() using isFinishing() so you can avoid some code.
The answer here specifies that we have to configure the manifest in such a way that it should not get destroyed, but it does not answer how to configure. I arrived at this post when I had a similar problem for, the solution is pretty simple. Just mention
android:persistent="true"
Reference : - https://developer.android.com/guide/topics/manifest/application-element.html
This should prevent your activity getting destroyed when you click back button
I explained in detail with a use case in another post,
Prevent activity from being destroyed as long as possible
I hope this helps others who arrive at this place for the same problem
My app is retaining all of the variable values when it closes and this is effecting how it runs when reopened. Is there any way to reset them all upon app close? or is there a way to clean the app from memory when it is closed so to speak? For the moment I have just been setting all of the important variables "=0" in the last few lines of execution but I know there must be a correct way to doing this.
EDIT:
OK I thought that it would just be easier to reply here instead of individually to everyone.
The app is indeed staying alive in the background, I checked with advanced task killer. How would I get the ap to "Die" by presing the back button? I think this would be the easiest solution given how the app works:
open app > press start button > press stop button > results screen > press back button to exit.
so basically each time the app runs should be an independant execution.
Override the onPause, onResume, and onDestroy methods. onPause should save anything upon pausing, onResume should reload these values when it is resumed, and onDestroy will be called when your app closes. You can clean up stuff in onDestroy. See this link.
You app is probably not closing but remaining in background. Check advanced task manager and see if the app is running or not.
You need to familiarize yourself with the Activity Lifecycle.
You could leverage onResume() to reset your variables; also note onDestory() and onPause().
UPDATE:
Killing the application in its entirety each time the app moves to the background is an anti-pattern. You should really look at your application and follow the aforementioned activity lifecycle pattern and take the needed steps to insure your variables exist as you desire based on state.
I like what #Alex and #Jack said. To add to that, also consider that you can call finish() in your Activity if you want to force it to close up and return to the last Activity. Going along with this, also consider the use of setResult(int) (JavaDoc Here)
You can also set a flag on the Intent when you call the Activity you are questioning about. A flag like FLAG_ACTIVITY_NO_HISTORY could be helpful:
If set, the new activity is not kept in the history stack. As soon as the user navigates away from it, the activity is finished. This may also be set with the noHistory attribute.
List of Intent Flags
Uninitialized variables are bad. Don't do it. ALWAYS manually reset variables before using them for the first time.
the onResume() method will let you reset the variables when the program resumes, but will also do it when you return to the activity unless you add the logic that says you are coming from in the app, not the home page. Maybe onRestart() is what you really need? I'm not positive, but it's possible with onResume.