how to finish all activities except the first acti

2019-01-26 04:01发布

I google it but even if i run this code below it didnt finish the other activities.

ButtonClick.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View view) {
            LoginManager.getInstance().ctrl = false;
            UserManager.getInstance().loginControl();
            OrderManager.getInstance().orderCtrl = false;
            Intent intent = new Intent(OrderComplete.this,
                    MainActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
            startActivity(intent);
            finish();
        }
    });
}

9条回答
男人必须洒脱
2楼-- · 2019-01-26 04:40

To clear top activities from stack use below code

Intent intents = new Intent(A.this, B.class);
intents.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
            | Intent.FLAG_ACTIVITY_CLEAR_TOP
            | IntentCompat.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intents);
finish();

It will delete all activities from stack either asynctask run or not in the application.

It works fine and also a good approach

查看更多
不美不萌又怎样
3楼-- · 2019-01-26 04:49

when your going from one activity to other activity call finish();

do like this

public void onClick(View v) {
  Intent i = new Intent(A.this,B.class);

  i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
  startActivity(i);
  finish(); // to end the current activity
 }

call finish() after startactivity(...), so that A activity ll removed from the stack. when you press back button A activity ll be not there in stack.

查看更多
乱世女痞
4楼-- · 2019-01-26 04:51

1.If we haven't the asynctask class or thread in our class then by simply

i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)

write this code,can easily remove all activities from stack

2.this can also be done by making launcher activity as single task this can be done by changes in manifest file. as

android:launchMode="singleTask" 

3.But if we have asynctask in our app and also use of thread then both above methods fail..

for that we have to finish activity one by one

for eg. We have four activities A,B,C,D and each class has async task

and we have to go from A -> B-> C-> D

and from D We have again come back to *"A" and after pressing back button of device the app should be finish or can say exit*

For that we have to make static object of activities of all classes. Like

 **In class A**
     public static Avtivity mactivity;
     @Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_third);
            mactivity=this;
             }

Do this in all classes B,C

And then in D

By pressing a button finish the objects of all classes Like

**mButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) 
        {
            B mB=new B();
            mB.mactivity.finish();
                   //////do this for all class which are present in stack of activities
                            finish();
                            ////this will finish the D class and we reach at A class
                    }
                  });**

Thats solve...

I know its not a gud way but also not a bad one...And main thing is its working :)

查看更多
不美不萌又怎样
5楼-- · 2019-01-26 04:54

just remove every activity from stack except your first activity

youractivity.this.finish();

or you can use the Activity Single top. for that you can use:

intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
查看更多
Explosion°爆炸
6楼-- · 2019-01-26 04:54

We can Use :

intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
  startActivity(i);
查看更多
SAY GOODBYE
7楼-- · 2019-01-26 04:57

remove android:lanchMode="singleTop" from menifest and use setFlag intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

查看更多
登录 后发表回答