vijay2991
vijay2991

Reputation: 129

Close all running activities in an android application?

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?

Upvotes: 5

Views: 25095

Answers (11)

Sagar G.
Sagar G.

Reputation: 534

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.

Upvotes: 6

Vlad Yarovyi
Vlad Yarovyi

Reputation: 709

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.

Upvotes: 1

linkaipeng
linkaipeng

Reputation: 503

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.

Upvotes: 0

jamil
jamil

Reputation: 352

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?

Upvotes: 0

vijay2991
vijay2991

Reputation: 129

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.

Upvotes: 1

Rishi
Rishi

Reputation: 44

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

Upvotes: 0

Blundell
Blundell

Reputation: 76544

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();
}

Upvotes: 3

hovanessyan
hovanessyan

Reputation: 31463

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)

Upvotes: 1

Umar Qureshi
Umar Qureshi

Reputation: 6115

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);

Upvotes: 0

Karthi
Karthi

Reputation: 13752

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.

Upvotes: 0

NikhilReddy
NikhilReddy

Reputation: 6954

@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);

        }
    }

Upvotes: 0

Related Questions