4

I have used the code from here in my app, when I click on 'Cancel' option ,the app just minimizes ,when I long press and hold Home button and select the app.It shows the same screen not the splashscreen or the usual way the app starts - So I am assuming it just takes me to home screen. I am looking for a way I can close the main activity completely.

I have tried the following methods and it crashed every time:

  1. finish();
  2. mainactivity.class.finish();
  3. opening an intent which has systemexit()
  4. setresult() in the another activity.
Michael Berry
  • 70,193
  • 21
  • 157
  • 216
Jason Wood
  • 737
  • 1
  • 13
  • 22

2 Answers2

9

Not recommened but still you can use this. Better go with this solution in case you need to quit the app.

According to me the best solution is finish every activity in your app like below.

step1) maintain a static variable in mainactivity say.

  public static boolean isQuit = false;

step2) on click event of an button make this variable to true.

   mainactivity.isQuit = true;
   finish();

step3) And in every activity of your application have onrestart method as below..

  @Override
      protected void onRestart() {
         // TODO Auto-generated method stub
         super.onRestart();
        if(mainactivity.isQuit)
            finish();
    }
Shankar Agarwal
  • 34,573
  • 7
  • 66
  • 64
-3

u can kill all thread asynctask every running activity with :

System.exit(0);
user987760
  • 1,061
  • 3
  • 12
  • 26
  • I am using gps and internet hence it drains lot of battery.I have tried the above code and its still running in the background. – Jason Wood Apr 12 '12 at 11:56
  • the wifi and gps drains battery non-stop , so i suggest to disable these two from settings when u r not using them...and u can turn on and off GPS programmatically – user987760 Apr 12 '12 at 12:02
  • if the System.exit(0); didn't work for you, i guess you need to finish your activity by (this.finish();) then System.exit(0); it always work for me... – user987760 Apr 12 '12 at 12:06