6

I'm working on an app that launches the browser activity to perform a Twitter OAuth authorization. This process uses a callback url which will re-launch the activity that started the browser activity in the first place.

My problem is that the browser pages remain in the history stack and when the user then clicks back from the preferences activity that launched the browser in the first place, they don't go back to the app's main activity, but instead are brought back to the browser. I've tried adding flags to the launching intent to prevent history and reset on clear, but it doesn't seem to work when running on my phone, only on the emulators.

Here is the code I'm using to launch the browser activity:

                Intent webIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(authUrl));

            webIntent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
            webIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
            webIntent.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);

            ctx.startActivity(webIntent);

Anyone have any idea what might be wrong?

jaredbro
  • 61
  • 1
  • 3

4 Answers4

2

Set the activity flag to Intent.FLAG_ACTIVITY_CLEAR_TOP after starting it. This way, the task will be deleted form the app stack so your main activity remains the top one on every new launch:

ctx.startActivity(webIntent);
browserIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Big Ali
  • 217
  • 1
  • 8
0

call

finish();

after

ctx.startActivity(webIntent);
Mathias Conradt
  • 28,420
  • 21
  • 138
  • 192
  • That will close the activity that I actually want to stay open, here is a sequence of events. App starts with a MainActivity, the user opens PreferencesActivity, then the user clicks Authorize Twitter which launches the browser activity with the above code, passing it a callback url that PreferencesActivity has an intent filter to handle. User logins into Twitter, authorizes it to be used by the app and the browser runs the callback url which re-activates PreferencesActivity through it's intent filter. At this time, if the user clicks the 'Back' key, I want it to go back to MainActivity. – jaredbro Aug 12 '10 at 11:13
  • Why do you reactivate PreferencesActivity with the IntentFilter instead of using startActivityForResult ? I'm not sure if it would work in your case as I don't know the details of your code, but might that be an idea? This way you'd actually really going back in the stack history rather than opening another instance of PreferencesActivity. Not sure though if that's what it does right now, easier to see with the complete code, callbacks, intent-filters, etc. – Mathias Conradt Aug 12 '10 at 12:10
0

Hi i have the same problem at the moment. I think the solution is to start the activity you want to return to with a special flag:

Intent intent = new Intent(this, acticityToReturnTo.getClass());
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intent);

The problem is that is does not work (on 1.6 at least).. But maybe someone finds a solution for this. I think FLAG_ACTIVITY_CLEAR_TOP is exactly what we.

Simon
  • 13,173
  • 14
  • 66
  • 90
  • 1
    Do you realise that the second `setFlags()` is overriding the first one? You want to use `addFlags()` to additively configure flags for the intent. – ohhorob Dec 15 '11 at 18:17
0

I found that Intent.FLAG_ACTIVITY_NO_HISTORY works only if the browser was not running before. If I restart Android and run my app which call browser everything work well. But when I start browser before my app, browser will stay in history.

Here is related question also, but with no really working solution

How can I do that in Android. Activity -> WebBrowser -> Acrivity, but Press Back not see the WebBrowser

Community
  • 1
  • 1
ATom
  • 15,960
  • 6
  • 46
  • 50