2

I was using this code to exit a entire Application. actually i try to exit apps in any page of my application. i was try to this code..

please any one can help me.

    @Override
    public void onBackPressed() {
     this.finish();
    }
Raiv
  • 5,731
  • 1
  • 33
  • 51

8 Answers8

6

As far as I know, Android does not favor exiting an application by choice.Android OS itself manages it.

However you can consider following as a workaround.

  1. You can bring up the Home application by its corresponding Intent:

    public void exitAppMethod(){
    
           Intent intent = new Intent(Intent.ACTION_MAIN);
           intent.addCategory(Intent.CATEGORY_HOME);
           intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
           startActivity(intent);
    }
    
    then you can call this method wherever you want. like
    
    @Override
    public void onBackPressed() {
     exitAppMethod()
    }
    
  2. Put the following code in the manifest for each activity so that the application is not displayed in recent apps list. (on long pressing home button)

        <activity
        android:name=".your_Activity"
        android:label="@string/app_name"
        android:excludeFromRecents="true"/>
    

So whenever user tries to exit using exitAppMethod(), the user will be taken to homescreen, at the same time the application will not be displayed in recent apps list. So user will have to launch app from the start only.

CRUSADER
  • 5,486
  • 3
  • 28
  • 64
Ritesh Gune
  • 16,629
  • 6
  • 44
  • 72
1

You need to maintain the flow of your application. If, at any point, you want to exit the application on Back button press, then you need to make sure that there is no activity pushed into stack.

For example, you can finish each activity when you move to another activity. This way you do not even need to handle the device's back button and the user will exit the application.

1

If you want to close the entire app without keeping up with the flow or killing activities every time you start a new one, you can use this solution:

@Override
public void onBackPressed() {
    Intent intent = new Intent(getApplicationContext(), FirstActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    intent.putExtra("EXIT", true);
    startActivity(intent);
}

Then in your first activitie's onCreate method do:

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

You will have to put the onBackPressed code in every back button you want to completely close the activity of course.

Community
  • 1
  • 1
Adam Johns
  • 35,397
  • 25
  • 123
  • 176
  • This worked great except my app would crash if I immediately touched its icon on the launch screen after hitting back. Instead of `finish()`, I had to use `this.moveTaskToBack(true)`. – Noumenon Oct 30 '13 at 09:57
1

Say you have activity stack like A>B>C>D>E. You are at activity D, and you want to close your app. This is what you wil do -

In Activity from where you want to close (Activity D)-

Intent intent = new Intent(D.this,A.class);
intent.putExtra("exit", "exit");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP| Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intent);

In your RootActivity (ie your base activity, here Activity A) -

@Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        if (intent.hasExtra("exit")) {
            setIntent(intent);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (getIntent() != null) {
            if (("exit").equalsIgnoreCase(getIntent().getStringExtra(("exit")))) {
                onBackPressed();
            }
        }
    }
Darpan
  • 5,623
  • 3
  • 48
  • 80
1

if You need to close the app use finish(); or or close your app from nested activities you can use finishAffinity();

Jey Arun
  • 103
  • 5
0

there are many strategies wher you can implement this feature : Suppose one example :

every where in app when going to start new activty i.e :

startactivity(intent);

just finish the current activity before going to next activity like this :

 startactivity(intent);
 this.finish();

in this way at any where in app when you click back your whole app will be closed :

else You can also Use FragmentActivity if you have any idea about fragments this also a good approach.

Muhammad Omer
  • 229
  • 2
  • 11
0

You can close all UIs(activities), but you can not close your process really. System.exit(0); might work, but the process can be re-live, when other processes need resource from you. It is not a good idea to "close application". You can only handle event for "ui closing". Try to define "count" of all activities, "dec" them in onDestory.

    public class BasicActivity extends Activity {
 onCreate() {
     mPreference.incActivityCount();//++
     super.onCreate();
 }

 onDestory() {
     mPreference.decActivityCount();//--
     if( mPreference.getActivity() == 0 ) {
         //All being opened Activities have been closed.
         onAppHasNoUIs();
     }
     super.onDestory();
 }

 onAppHasNoUIs() {
        //All being opened Activities have been closed.
 }
}
TeeTracker
  • 7,064
  • 8
  • 40
  • 46
0

Since you haven't mention about your activity flow it is adviceable that whenever you start a new activity kill the existing activity ku finish() method.

Sukan
  • 346
  • 1
  • 3
  • 19