7

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.

James
  • 13,891
  • 26
  • 68
  • 93
  • Please tell me in brief about your question. – Lucifer Jan 20 '12 at 11:42
  • try this post http://stackoverflow.com/questions/7426751/how-to-close-stop-running-application-on-background-android it may helps you. – Ajay Jan 20 '12 at 11:46
  • Actually I was doing a cocos2D application and I want to start the game from begning each time. when I call finish() on onStop() the application closes when it starts running. Is that a problem with cocos2D. – James Jan 20 '12 at 14:13

5 Answers5

7

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. enter image description here

Community
  • 1
  • 1
Okan Kocyigit
  • 13,203
  • 18
  • 70
  • 129
7

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).

Dmitry Zaytsev
  • 23,650
  • 14
  • 92
  • 146
  • This will kill the activity when the user suspends the phone, or when otherwise leaving the activity. – Mark Gjøl Jan 20 '12 at 11:48
  • @MarkGjøl, I thought this is what he wants. However it's bad idea ) – Dmitry Zaytsev Jan 20 '12 at 11:50
  • 1
    That's also what I understood in wht the OP asked. That is an answer to the question, but it's bad practice. 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. – Guillaume Jan 20 '12 at 11:55
  • It sounds like a badly thought out question (Otherwise it would most likely be longer :)), not taking into account how the phone tends to be used. I would prefer Ocanal's approach by reiniting the application at onResume though. Using this approach the user will simply exit the activity when he hits the power button, not start from scratch. – Mark Gjøl Jan 20 '12 at 12:04
  • 1
    @MarkGjøl, I agree with you, but I think the "bad approach" should be in answers too :) – Dmitry Zaytsev Jan 20 '12 at 12:06
4

After adding finish();

This code will completely stop the application.

System.runFinalizersOnExit(true);
System.exit(0);
android.os.Process.killProcess(android.os.Process.myPid());
Mal
  • 373
  • 4
  • 15
2

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.

Guillaume
  • 22,694
  • 14
  • 56
  • 70
0

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

Mahesh
  • 1,257
  • 1
  • 14
  • 24