Exclude Activity from recents and history

1.2k views Asked by At

Actually I have a direct issue with android custom tabs. But nevertheless I will generalize my question. Let's assume that I have ActivityA and ActivityB. ActivityA - is an external one, so that we launch it from an Intent :

Intent intent = customTabsIntent.intent;
        intent.setData(requestUri);
        intent.putExtra(CustomTabsIntent.EXTRA_TITLE_VISIBILITY_STATE, CustomTabsIntent.NO_TITLE);
        mContext.startActivity(intent);

From here we see that ActivityA - is actually a custom tab activity. When things get done I launch ActivityB. I want ActivityA dissaper from task history, I can achieve this by aplying flag Intent.FLAG_ACTIVITY_NO_HISTORY to my intent. But this approach is causing problems, because when user switches to recents,or goes to other app - ActivityA disappears forever. Of course - that's how flag no history works. But I want a different flow, I want ActivityA disappear only when ActivityB was launched. Any ideas how to achieve this?

P.S. I need to finish ActivityA, which gets launched via intent, I don't have access to its code and I don't have the ability to call finish().

2

There are 2 answers

2
David Wasser On BEST ANSWER

I don't think you can do this by using the NO_HISTORY flag since ActivityA is doing the launching and you have no control over it.

However, you should be able to achieve your goal (not being able to go from ActivityB back to ActivityA by overriding the BACK button in ActivityB and having it go back to whatever is underneath ActivityA in the task stack.

Assuming that ActivityC is the one that starts ActivityA, you could do something like this in ActivityB:

@Override
public void onBackPressed() {
    // Direct user back to ActivityC
    Intent intent = new Intent(this, ActivityC.class);
    // Add flags to ensure that we reuse the existing instance of
    //  ActivityC and that we clear any other activities above ActivityC
    //  from the stack.
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
                    Intent.FLAG_ACTIVITY_SINGLE_TOP);
    startActivity(intent);
}
1
Sadiq Md Asif On

You can use finish() method to clear any activity. so before starting another activity finish that.

 Intent intent = customTabsIntent.intent;
            intent.setData(requestUri);
            intent.putExtra(CustomTabsIntent.EXTRA_TITLE_VISIBILITY_STATE, CustomTabsIntent.NO_TITLE);
            mContext.startActivity(intent);
        finish();     //add this to clear that activity