Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to finish an android application?

Tags:

android

I need to finish an android application. For that i wrote

@Override public void onBackPressed() {     AlertDialog.Builder builder = new AlertDialog.Builder(this);     builder.setMessage("Are you sure You want to exit")         .setCancelable(false)         .setPositiveButton("YES"),         new DialogInterface.OnClickListener() {             // On             // clicking             // "Yes"             // button              public void onClick(DialogInterface dialog,int id) {                 System.out.println(" onClick ");                 closeApplication(); // Close Application method called             }         })         .setNegativeButton("NO"),         new DialogInterface.OnClickListener() {             // On             // clicking             // "No"             // button             public void onClick(DialogInterface dialog,int id) {                 dialog.cancel();             }         });          AlertDialog alert = builder.create();         alert.show();     }      private void closeApplication() {         System.out.println("closeApplication ");         this.finish();     } } 

But if any activity is not finished in the application, when i tried to exit the application that activity is finished first and the application is not exiting.. i tried 2 times to exit this application... How i can finish all the activities in an application when i need to exit... or is there any way to finish the entire application

like image 397
jennifer Avatar asked Jan 19 '11 05:01

jennifer


People also ask

How do I completely exit an app in Android Studio?

The application automatically exits when you switch off the device. The Android architecture does not support exiting the app. If you want, you can forcefully exit the app, but that's not considered good practice.

What does finish () do in Android?

On Clicking the back button from the New Activity, the finish() method is called and the activity destroys and returns to the home screen.

How do you stop apps running in the background on Android?

Here's how to kill background apps on Android: Go to Settings > Apps. Select an app you want to stop, then tap Force Stop. The app will relaunch when you restart your phone.


2 Answers

To close application just call:

android.os.Process.killProcess(android.os.Process.myPid()); 

Otherwise due-to specific life-cycling of Android activities you can't be sure that application is closed/killed.

like image 169
Barmaley Avatar answered Sep 28 '22 14:09

Barmaley


whenever you are starting a new activity use

myintent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(myintent); 

and in manifest file mention that activity as

<activity android:name=".<Activity Name>" >         <intent-filter>         <action android:name="android.intent.action.VIEW"/>      <category android:name="android.intent.category.DEFAULT" />      </intent-filter>        </activity> 
like image 37
Pinki Avatar answered Sep 28 '22 14:09

Pinki