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
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.
On Clicking the back button from the New Activity, the finish() method is called and the activity destroys and returns to the home screen.
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.
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.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With