Clear back stack after activity is opened from web

2020-07-27 16:00发布

My application has an activity, that launches oauth-authorization process in browser, and finally browser receives redirect to url "appname://com.appname", that calls back to my activity. (activity declared that it can view such urls)

Everything is OK, but if user presses "Back" he goes back to web browser. I want to clear history stack when activity is called back, to make this activity root and top in this task.

What flags or hooks can I use for this case?


I found only one ugly solution: receive appname://com.appname urls to special "Gag" activity, that will launch main activity on its onResume.

I tried to make main activity "singleTask", launch browser in new task, set flags NO_HISTORY and EXLUDE_FROM_RECENTS, CLEAR_TOP, set clearTaskOnLaunch="true". Tried many combinations of that flags and tags in manifest - nothing helps.


I solved this problem:
1) set clearTaskOnLaunch="true" in AndroidManifest.xml
2) Launch browser in new task (with FLAG_ACTIVITY_NEW_TASK), and finish current activity(to finish current task).
3) override onBackPressed: instead of finish this activity call to moveTaskToBack(true);

when user presses back - task(browser is in root of it) goes to background and user see his homescreen.

when user launches application again - it opens from existing instance,clears task and becomes root of it (because clearTaskOnLaunch="true")

1条回答
再贱就再见
2楼-- · 2020-07-27 16:22

When click link appname://com.appname in web browser, the browser call your activity, but browser did not call finish itself, you can't change the browser.

I think that make a new activity that has a webview inside can solve this.

You will start that new activity instead of android browser. In the new activity, set webViewClient to navigate back to your main activity:

wv_mem_rank.setWebViewClient(new WebViewClient() {
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if(url.startWith("appname://com.appname"))
        {
            startActivity();
            finish();
        }
        view.loadUrl(url);
        return true;
    }
    public void onLoadResource (WebView view, String url) {
    }
    public void onPageFinished(WebView view, String url) {
    }
});
查看更多
登录 后发表回答