gaomode
gaomode

Reputation: 176

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

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?

Upvotes: 1

Views: 570

Answers (2)

vishal jangid
vishal jangid

Reputation: 3025

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

Upvotes: 0

Ben P.
Ben P.

Reputation: 54244

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.

Upvotes: 1

Related Questions