Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
725 views
in Technique[技术] by (71.8m points)

android - how to finish all activities except the first activity?

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();
        }
    });
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...