user2515339
user2515339

Reputation: 91

how to destroy android application and clear from memory?

I tried System.Exit(0), but is not that i need;

I'm trying to safely destroy android application and clear it from memory,
can i make a button to exit android application and clear all application from memory ?

Upvotes: 6

Views: 18281

Answers (5)

user1525382
user1525382

Reputation:

calling finish() will only exit the current activity, not the entire application. however, there is a workaround for this:

Every time you start an Activity, start it using startActivityForResult(...). When you want to close the entire app, you can do something like this:

setResult(RESULT_CLOSE_ALL);
finish();

Then define every activity's onActivityResult(...) callback so when an activity returns with the RESULT_CLOSE_ALL value, it also calls finish():

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch(resultCode)
    {
    case RESULT_CLOSE_ALL:
        setResult(RESULT_CLOSE_ALL);
        finish();
    }
    super.onActivityResult(requestCode, resultCode, data);
}

This will cause a cascade effect closing all activities.

Upvotes: 2

Raghunandan
Raghunandan

Reputation: 133560

Do not call System.exit(0).

Check the answer by commonsware

Is quitting an application frowned upon?.

You can use action bar. On click of app icon navigate to home screen of your app and click back button in home screen to exit from the app.

http://developer.android.com/guide/topics/ui/actionbar.html

Check the below link clearly shows navigation with screen shots.

http://developer.android.com/design/patterns/navigation.html

Upvotes: 0

Avadhani Y
Avadhani Y

Reputation: 7636

System.exit(0) is not a better approach. Instead, Call finish() in all the activities and the activity will be removed from stack.

Upvotes: -1

Volodymyr
Volodymyr

Reputation: 1047

Android doesn't have one entry points in app. You should design according guideline. Link: http://developer.android.com/design/patterns/navigation.html

can i make a button to exit android application and clear all application from memory ?

No.You should remove all activities from stack (e.g. use finish()).

Upvotes: 1

Anup Cowkur
Anup Cowkur

Reputation: 20563

Calling System.exit() is a bad idea. You can simply call finish() on all the activities you need to kill. Here's a good solution to do this: https://stackoverflow.com/a/5453228/1369222

The basic idea is to add a broadcast receiver to every activity of your app that will finish itself when a particular "kill activities" intent is fired.

Upvotes: 2

Related Questions