문제

I have two activities Activity A and Activity B. When I click button in Activity A, The Activity B starts. Now When I press back button from Activity B the Activity A get restarted. But I want to come out of the app when the back button in Activity B is pressed. I tried using this but not getting success

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

Thanks in advance

도움이 되었습니까?

해결책

You need to finish ActivityA once you're starting ActivityB:

Intent intent=new Intent(ActivityA.this, ActivityB.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();

다른 팁

Call finish() after starting new Activity - this will close the calling Activity:

startActivity(new Intent(ActivityA.this, ActivityB.class));

//calling finish() closes current Activity
finish();

Read more about Activity life cycle here and here.

Just call finish() after startActivity(intent).

Intent intent=new Intent(ActivityA.this, ActivityB.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
finish();

Intent.FLAG_ACTIVITY_CLEAR_TOP is not suitable for your situation.In your code,ActivityA will re-builded again.There has two instances of ActivityA in the TaskStack.Delete the addFlag.

Intent intent=new Intent(ActivityA.this, ActivityB.class);
startActivity(intent);
finish();
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top