1

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().

Yurii Tsap
  • 3,554
  • 3
  • 24
  • 33
  • You can call `finish()` after `startActivity(intent)`. – GVillani82 Dec 29 '16 at 15:31
  • what do you mean by **"I don't have access to its code"** and why can't you call finish()?? – Amit Upadhyay Dec 29 '16 at 16:12
  • @AmitUpadhyay as I mentioned - ActivityA is custom tabs activity, that's android activity, not mine. – Yurii Tsap Dec 29 '16 at 16:42
  • It seems to me that there is a contradiction in your question. Can you tell the intent code that you have written above is used to open the ActivityA or ActivityB?? and for my knowledge can you also tell what is android's custom tabs activity? – Amit Upadhyay Dec 29 '16 at 16:54
  • @AmitUpadhyay Sure, NP! https://developer.chrome.com/multidevice/android/customtabs – Yurii Tsap Dec 29 '16 at 17:10
  • @AmitUpadhyay some caller, doesn't matter who - launches ActivityA by sending intent(that I described), after ActivityA has finished its work it fires an intent that launches ActivityB. Makes sense to you or still not really clear? – Yurii Tsap Dec 29 '16 at 17:13
  • How and when is ActivityB launched? – David Wasser Dec 29 '16 at 23:06
  • @DavidWasser From ActivityA, some kind of inner logic, in my case - custom Tab is parsing url and building from it an intent, which actually launches ActivityB. – Yurii Tsap Dec 30 '16 at 11:08

2 Answers2

1

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);
}
David Wasser
  • 93,459
  • 16
  • 209
  • 274
0

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
Sadiq Md Asif
  • 882
  • 6
  • 18