How to ignore the launcheMode of "SingleTask" when starting an Activity?

486 views Asked by At

enter image description here

There are three Activites where B's launch mode is "SingleTask". I'm wondering How to start a new B rather than restarting the old B when using C?

2

There are 2 answers

0
Ben P. On BEST ANSWER

Unfortunately, if Activity B is set in the manifest to use android:launchMode="singleTask", there is no way (that I know of) to override this.

However, you could remove this attribute from your manifest and instead use code like this when you wanted the singleTask behavior:

Intent intent = new Intent(this, ActivityB.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);

The end result is that you get singleTask behavior when you want it, and you don't get it when you don't want it. You just have to change the strategy.

0
vishal jangid On

Use below code in onBack press method of activity

    @Override
    public void onBackPressed() {
    Intent BackpressedIntent = new Intent();
    BackpressedIntent .setClass(getApplicationContext(),B.class);
    BackpressedIntent .setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(BackpressedIntent );
    finish();
}