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

前端 未结 5 1774
余生分开走
余生分开走 2020-12-18 04:02

How can I do that the web browser call a custom scheme to start a activity, then I want I press the Back Button but not return the web browser.

I just want to implem

相关标签:
5条回答
  • 2020-12-18 04:30

    I also meet this issue. Here's the summary:

    1. If browser was not running before, it works well with the above answers; if browser was run before:
    2. intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY) does NOT work;
    3. android:allowTaskReparenting="true" android:launchMode="singleTask" looks like to put the browser in the bottom of the activity stack, but after quit the application (by BACK key), eventually the browser is launched again.

    So basically this issue still exists. I'll post if I find a way to resolve it.

    0 讨论(0)
  • 2020-12-18 04:36

    This probably happens because the second activity is launched into the same task you started the browser from, which means that starting your second activity returns to the already existing task with the topmost activity (activity 1) pushed to the back-stack. See here.

    If you make your second activity being started on a different task, pressing "back" should end that task and return to the task behind, the browser.

    To accomplish that, you could use FLAG_ACTIVITY_NEW_TASK in the intent starting activity 2, or set the activity's android:launchMode attribute to singleTop in your manifest.

    0 讨论(0)
  • 2020-12-18 04:39

    As explained by other users, your Activity is being created on the browser's stack. You cannot prevent the browser from opening on its own stack, but you can do the same configuration on your application.

    In your activity that is called by the browser, add the following in your manifest file:

    android:allowTaskReparenting="true"
    android:launchMode="singleTask"
    

    This will make your activity to be created on your application stack (instead of the browser's). This will basically bring your application back to the front.

    android:launchMode="singleTop" will not work because it prevents creating a new activity only if that activity is on top of the stack, which is not the case as the browser is the one currently at the top. android:launchMode="singleTask" will ensure no other instances are created regardless of them being on top or not.

    If you set the Intent.FLAG_ACTIVITY_NO_HISTORY with the configuration above then everything should work fine, regardless of the browser being previously open or not. In the worst case, the browser will be at the bottom of the stack and if you keep hitting back you would eventually reach the browser instead of exiting the application.

    0 讨论(0)
  • 2020-12-18 04:39

    I found that only 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.

    I tried add to manifest to activity definition android:launchMode="singleTop" according the user634618. But it doesn't work. I don't understand why singleTop should help.

    In documentation is also this:

    BlockquoteAs another example, the Android Browser application declares that the web browser activity should always open in its own task—by specifying the singleTask launch mode in the element. This means that if your application issues an intent to open the Android Browser, its activity is not placed in the same task as your application. Instead, either a new task starts for the Browser or, if the Browser already has a task running in the background, that task is brought forward to handle the new intent.

    I think that this cause the problem. But how to force run browser in current task?

    0 讨论(0)
  • 2020-12-18 04:47

    When you open your Intent maybe you can set it with no-history flag

    intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
    
    0 讨论(0)
提交回复
热议问题