1

i want to close my whole application. i have tried out finishActivity() on back press but it doesn't work. I want to close my application on back press event completely. How could i do it?

 public void onImageViewClicked(View view){
        switch(view.getId()){
            case R.id.viewStk:
intent = new Intent(MainActivity.this,ViewStkActivity.class);
                startActivityForResult(intent, 12);
                break;
            case R.id.about:
                Intent aboutIntent = new  Intent(MainActivity.this,AboutActivity.class);
                startActivity(aboutIntent);
                break;
    } 

}
Kailash Dabhi
  • 3,473
  • 1
  • 29
  • 47

4 Answers4

5

I use:

@Override
public void onDestroy()
{
    super.onDestroy();
    // Force the process to be killed on finish(), not kept around
    System.runFinalizersOnExit(true);
    System.exit(0);
}

which is called after using this.finish()

(This is an overridden method of the activity)

People will complain and tell you it breaks the Android app lifecycle, but having an "exit" button on my app during development was an immediate boost of my productivity.

Edit

Apparently, the latest version of the API notes that the runFinalizersOnExit function is deprecated and unsafe. I've had no issue with it on Gingerbread, ICS, or JB. But in interest of full disclosure, I'll point this out.

jedwards
  • 29,432
  • 3
  • 65
  • 92
5
public void onClick(View v) {
    finish();
    android.os.Process.killProcess(android.os.Process.myPid());
}
Praveenkumar
  • 24,084
  • 23
  • 95
  • 173
Anklet.
  • 479
  • 3
  • 14
  • thanks . hey do u know how to close only single activity completely , may be u r getting my point? – Kailash Dabhi Sep 12 '12 at 06:57
  • but @trisha it doesnt work i had already used that.somehow android remember the state of my TableLayout and add more data when i come back to that particular activity. thats why i want to kill my activity completely... – Kailash Dabhi Sep 12 '12 at 07:13
0

This will also help

Intent i = new Intent(yourScreen.this,Home.class);
        i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        i.putExtra("EXIT", true);
        startActivity(i);

and in the onCreate of the Home class, do this to check,

if (getIntent().getBooleanExtra("EXIT", false)) 
    {
        finish();
    }

what this will essentially do is no matter at what activity you are, you can call the home screen with the clear top flag. In the home screen there is a check condition in the onCreate method which will help to clear the stack..Now ,if you press back button you will exit the app as the stack is cleared..

Let me know if the problem still persists...

Gautam Mandsorwale
  • 1,580
  • 1
  • 18
  • 27
-1

In the activity.java-file that needs the implementation (fx MainActivity.java) insert this:

    public void exitProgram(View view){
    finish();
    System.exit(0);
    }

Just remember to also call the function in your .xml file, fx:

<Button
...
...
android:onClick="exitProgram"
...
/>

and finally in the buttons attributes, at the "text" attribute - press the small button next to the field and choose the one for the button you want to work with - done.

A good way to do it? No. Not at all. But if you just want a "panic-lets-get-outta-here"-button, then here you go.

There are a ton of different ways to implement the exit-functionality (one of them shown here) - as there are a ton of ways to implement the simple "Hello World" program - each has their value and you should really consider, why you have chosen the one you have.

nelion
  • 1,712
  • 4
  • 17
  • 37
  • any reason for the downgrade? Would be great if there was some arguments behind it - generally people downgrading without writing why, doesn't really make sense. – nelion Sep 25 '19 at 20:38