0

On pressing the back button , I'm trying to exit the application from activity.But the problem is after pressing the back key refreshing the same activity 2 to 3 times and goes to Login activity and does not exit the application.Can someone help for solve the issue .

Here is how I have to tried to exit the application.

@Override
    public void onBackPressed() {
        super.onBackPressed();

        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
        final SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putBoolean("LISTVIEW_EVENT_ONE", false);
        editor.putBoolean("LISTVIEW_EVENT_TWO", false);
        editor.commit();
        this.finish();

        //Intent startMain = new Intent(Intent.ACTION_MAIN);
        //startMain.addCategory(Intent.CATEGORY_HOME);
        //startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        /*Intent startMain = new Intent(Intent.ACTION_MAIN);
        startMain.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(startMain);*/

       /* Intent a = new Intent(Intent.ACTION_MAIN);
        a.addCategory(Intent.CATEGORY_HOME);
        a.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(a);*/
    }
sherlock
  • 97
  • 5
p. ld
  • 585
  • 3
  • 10
  • 23
  • android.os.Process.killProcess(android.os.Process.myPid()); – tianwei Jan 12 '16 at 08:55
  • Not a good idea to finish your activity `onBackPressed()`. when you start this activity finish your login activity. Once you do this normal back would close the app. – Rohit5k2 Jan 12 '16 at 08:58
  • back will pop the backstack, if you dont have any activites on the back stack it closes the app; only use kill process if you know what you are doing – Patrick Jan 12 '16 at 08:59
  • @Rohit5k2 : Actually i have use also onBackPresee() in Login activity – p. ld Jan 12 '16 at 09:00
  • `finish()` the Login activity before you navigate to the Home. That will take care of Login activity appearing on back press. – K Neeraj Lal Jan 12 '16 at 09:02
  • Using `onBackPressed()` to finish your activity is never a good idea. when you launch your new activity finish the Login activity. This way you wont have any backstack and you will be able to exit the app normally. why don't you put you login activity code where you are launching this activity. – Rohit5k2 Jan 12 '16 at 09:02
  • Why do you want to close your application entirely? Let the system handle this. – Niels Masdorp Jan 12 '16 at 09:03
  • http://stackoverflow.com/a/20592115/2826147 – Amit Vaghela Jan 12 '16 at 09:05

4 Answers4

0

It working for me:

finishAffinity();
android.os.Process.killProcess(android.os.Process.myPid());
rome 웃
  • 1,821
  • 3
  • 15
  • 33
0
public void onBackPressed() {
    super.onBackPressed();

    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
    final SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putBoolean("LISTVIEW_EVENT_ONE", false);
    editor.putBoolean("LISTVIEW_EVENT_TWO", false);
    editor.commit();
    this.finish();

   super.onBackPressed();

}
Rohit5k2
  • 17,948
  • 8
  • 45
  • 57
MurugananthamS
  • 2,395
  • 4
  • 20
  • 49
0

In your activity or with activity context (Downcasting the context will also do) use below code.

 moveTaskToBack(true);
Sush
  • 3,864
  • 2
  • 17
  • 35
-2

Add the following code in your method onBackPressed():

android.os.Process.killProcess(android.os.Process.myPid());
frogatto
  • 28,539
  • 11
  • 83
  • 129
simonws
  • 65
  • 5