5

I create one application and never use finish() for each activity. If my user clicks on the logout button it goes to the previous page.

How can I close my previous activity and close the application?

vijay2991
  • 131
  • 2
  • 4
  • 13

11 Answers11

6

This is one workaround that i have tried and it worked for me perfectly.

SOLUTION-1

this.finish();//try activityname.finish instead of this
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);

use this in the activity where you want to exit from your application....

================================================================================== Above code helps to resume your app where you last left off.

SOLUTION-2

If you want to exit from the application and also close all running activities you need to use.

onActivityResult()

For eg- suppose there are 3 activities A,B,C you navigate from A->B->C and at C you want to close all activities then use following sample.

Activity A

public class A extends Activity {
int Finish = 100;
Button launch_second;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    launch_second = (Button) findViewById(R.id.start_second_act);
    launch_second.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent second = new Intent(A.this,
                    B.class);
            startActivityForResult(second, Finish);
        }
    });
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch (requestCode) {
    case 100:
        this.finish();
        break;
    default:
        break;
    }
    super.onActivityResult(requestCode, resultCode, data);

}

Activity B

public class B extends Activity {
private Button launch_next;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.second_main);
    launch_next = (Button) findViewById(R.id.start_third_activity);
    launch_next.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent third = new Intent(B.this,C.class);
            startActivityForResult(third, 100);
        }
    });
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch (requestCode) {
    case 100:
        setResult(requestCode);
        this.finish();
        break;

    default:
        break;
    }
    super.onActivityResult(requestCode, resultCode, data);
}

}

Activity C

public class C extends Activity {
Button kill_app;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.third_main);
    kill_app = (Button)findViewById(R.id.kill_app_btn);
    kill_app.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            C.this.finish();
            setResult(100);

        }
    });

}

}

SOLUTION-3

There is method available finishAffinity() call it from any activity,all previous activities will get destroyed.

Sagar G.
  • 504
  • 7
  • 15
  • 1
    This close the app but doesn't finish the activities. If you reopen the app you don't go to the main activity. – Brais Gabin Jul 01 '13 at 07:15
  • 1
    @BraisGabin I have updated the answer ,check the updated answer,hope it might help for some others looking for the solution – Sagar G. Jul 12 '13 at 14:29
  • @SagarG., this one will make the "Back Button" not working as usual. INstead it will make the button work as "Close All and Out". And that's not the effect we want to get. How to solve it out? – gumuruh Jul 13 '14 at 05:42
  • SOLUTION-1 is not working for clear all activity from stack, it resume from last stored activity and solution 2 is long process. – Anand Savjani Apr 12 '16 at 07:41
3

EDIT

Sam Janz answer is cleaner than this method. Use the intent flags to your advantage

When the user presses log out:

Intent intent = new Intent(this,MyHomeActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putBooleanExtra("finishApplication", true);
startActivity(intent);

Then in MyHomeActivity (Your start activity) in onResume:

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

This way you don't have to have checks in all your activity's only the Home activity.


Dirtier option:

Create a static boolean variable in a singleton somewhere (probably in a class that extends application);

public static boolean loggingOut = false;

When the user presses log out set this to true and call finish on that activity.

YourApplication.loggingOut = true;
finish();

In each activity in onResume()

if(loggingOut){
   finish();
}

Ensure you set this boolean back to false in your main/start up activity:

if(loggingOut){
   finish();
   YourApplication.loggingOut = false;
}

If you also want the back button to do it, override onBackPressed(), that would then also do

@Override
public void onBackPressed(){
     YourApplication.loggingOut = true;
     super.onBackPressed();
}
Blundell
  • 75,855
  • 30
  • 208
  • 233
1
Intent intent = new Intent(this, LoginActivity.class);
ComponentName cn = intent.getComponent();
Intent mainIntent = IntentCompat.makeRestartActivityTask(cn);
startActivity(mainIntent);

Description:

public static Intent makeRestartActivityTask (ComponentName mainActivity)

Make an Intent that can be used to re-launch an application's task in its base state. This is like makeMainActivity(ComponentName), but also sets the flags FLAG_ACTIVITY_NEW_TASK and FLAG_ACTIVITY_CLEAR_TASK.

Vlad Yarovyi
  • 709
  • 1
  • 8
  • 16
1

use finish() for each activity

1) Activity lifecycle

2) Shutting down Activity and managing Activities lifecycle

3) The same question answered in detail (2 approaches - Broadcast Receiver (+1) and Intent.FLAG_ACTIVITY_CLEAR_TOP)

Community
  • 1
  • 1
hovanessyan
  • 30,580
  • 6
  • 55
  • 83
  • Hi thanks for reply i use finish() on each activity but then my mobile back button is not work if i press back it close my application – vijay2991 Dec 23 '11 at 11:25
  • it depends on what you're trying to achieve. You want to click logout and close your program, but in any other case, when hitting the back button, you want the previous Activities to be visible? – hovanessyan Dec 23 '11 at 11:31
  • yes, actually i have login and logout facility for user so i want when user is login he can navigate in my app easily using mobile back button or using menu but when click on logout that time i want to close all activity and he can not be enter in to previous page without login again... and thank hovanessyan for giving me your valuable time..:-) – vijay2991 Dec 23 '11 at 11:42
1

Thanks for your replies. I solved my problem.

Solution: I write a file with some data and when the user clicks logout, I remove data from that file and finish() current activity, and all in previous activity I write code in onResume() I read file and if it is blank or null finish() so in that it will close all your activity and your application get close. Thank you for the great help.

axel22
  • 32,045
  • 9
  • 125
  • 137
vijay2991
  • 131
  • 2
  • 4
  • 13
  • 1
    abusing a file with arbitrary data as a flag is probably not the best design. You could at least make it a boolean on some singleton or some such. – katzenhut Sep 04 '14 at 15:13
0

you can use this
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);

from this question answer How to exit from the application and show the home screen?

Community
  • 1
  • 1
jamil
  • 352
  • 1
  • 3
  • 12
0

you can add a List in Application that save every activity you created. and when you exit, you just need to finish all activity in the list.

linkaipeng
  • 483
  • 7
  • 9
0
@Override
    public void onClick(View v) {

        if (v == btnAtGlanceBack) {
            Intent backIntent = new Intent(this, Dashboard.class);
            startActivity(backIntent);

        } else if (v == btnAtGlanceLogOut) {
            Intent logoutIntent = new Intent(this, GlobalScholarLogin.class);
            logoutIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(logoutIntent);

        }
    }
NikhilReddy
  • 6,904
  • 11
  • 38
  • 58
0

In general android main stack for all you activity your launching and remove it based on the way you call to next activity. If you want to clear all activity set this flag Intent.FLAG_ACTIVITY_CLEAR_TASK while launching activity in order to clean up previous activity.

Karthi
  • 13,624
  • 10
  • 53
  • 76
0

To clear whole stack which is tracking your activities in android you can use following code

Intent intent = new Intent(this, MyHomeActivity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        finish();
        startActivity(intent);
Umar Qureshi
  • 5,985
  • 2
  • 30
  • 40
0

put this line of code in your application System.exit(2);

Rishi
  • 44
  • 2