Loadeed
Loadeed

Reputation: 567

Exit Android application on back button

I have ImageButton with back arrow in header of my application. When user presses it, I call finish() on current Activity. I also have option for user to go home, but I want to somehow clear Activity history, so when user presses back button on home activity, application closes.

I can also put a parameter to the intent, when calling home activity, but what do I call for application to close?

Upvotes: 0

Views: 2454

Answers (6)

Seshu Vinay
Seshu Vinay

Reputation: 13588

When you go to an activity, save that activityContext some where(say in some array of Contexts).

When you press the exit button, you can just loop the array and say activity.finish()

So all the activities in the stack are now finished and the user is out of app.

Code may be like this:

public class ActivityStack {

    public ArrayList<Context> contextArray=new ArrayList<>(); 

    public void setContext(int i,Context context) {
          contextArray.add(context);
    }

    public void quitApp() {
           for(context:ContextArray) {
              context.finish();  

            }
    }

}

Edit: From API 16, finishAffinity() method can be used to finish all activities.

Upvotes: 1

kittu88
kittu88

Reputation: 2461

You can use this function:

//to remove application from task manager
public void onQuitPressed() {

    int pid = android.os.Process.myPid();
    android.os.Process.killProcess(pid);
}

Upvotes: 0

user1838091
user1838091

Reputation: 21

do something like this..

close your services Manually

@Override
public void onBackPressed() {
    Intent intent = new Intent(NewFox_Project.this,RadioService.class);
    moveTaskToBack(true);
    stopService(intent);


    Intent radioTimes = new Intent(NewFox_Project.this,RadioTimeService.class);
    moveTaskToBack(true);
    stopService(radioTimes);

    Intent radioPlyerService = new Intent(NewFox_Project.this,RadioPlayerService.class);
    moveTaskToBack(true);
    stopService(radioPlyerService);

    NewFox_Project.this.finish();
    super.onBackPressed();
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {

    if(event.getKeyCode()==KeyEvent.KEYCODE_BACK)
    {
        Intent intent = new Intent(NewFox_Project.this,RadioService.class);
        moveTaskToBack(true);
        stopService(intent);


        Intent radioTimes = new Intent(NewFox_Project.this,RadioTimeService.class);
        moveTaskToBack(true);
        stopService(radioTimes);

        Intent radioPlyerService = new Intent(NewFox_Project.this,RadioPlayerService.class);
        moveTaskToBack(true);
        stopService(radioPlyerService);

        NewFox_Project.this.finish();
    }
    return super.onKeyDown(keyCode, event);
}

Upvotes: 0

DroidBender
DroidBender

Reputation: 7892

The Intent flag FLAG_ACTIVITY_CLEAR_TOP will suit your purpose.

Example for starting your Activities without a history:

Intent intent = new Intent(this,ExampleActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);

Upvotes: 1

ernazm
ernazm

Reputation: 9258

When you close your last activity consider the app as closed. The lifecycle of the app is beyond your responsibilities as a developer and is handled by the framework. When all your activities are finished, the Activity stack (history) is empty, so the next app launch will start your first activity invoking onCreate first.

The singletons and other static fields will be kept however, but Android will clear them as it will need more memory, so that's not a problem.

I'd also suggest you to refer to this thread for more details.

Upvotes: 0

Stephane Mathis
Stephane Mathis

Reputation: 6612

You need to call an activity with startIntentForResult(). Then in the second activity, you will do something like this :

setResult(RESULT_OK);
finish();

And in the first activity, get this code and close that activity as well :

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
 if (resultCode == RESULT_OK) {
  if (getParent() == null) {
  setResult(Activity.RESULT_OK, null);
  }
  else {
   getParent().setResult(Activity.RESULT_OK, null);
  }
  finish();
 }
}

Upvotes: 1

Related Questions