jaredbro
jaredbro

Reputation: 61

Opening browser activity, but prevent it from being in the activity history

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?

Upvotes: 6

Views: 3134

Answers (4)

Big Ali
Big Ali

Reputation: 307

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

Upvotes: 2

ATom
ATom

Reputation: 16180

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

Upvotes: 0

Simon
Simon

Reputation: 13283

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.

Upvotes: 0

Mathias Conradt
Mathias Conradt

Reputation: 28665

call

finish();

after

ctx.startActivity(webIntent);

Upvotes: 0

Related Questions