Android的 - URI方案 - 从打开的应用程序的多个实例停止 [英] Android - URI Scheme - Stop from opening multiple instances of application

查看:204
本文介绍了Android的 - URI方案 - 从打开的应用程序的多个实例停止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是自定义的URI方案从网站将用户重定向到应用程序,而是启动应用程序时,它会打开一个新的实例,而不是仅仅恢复了已经打开的。

I'm using a custom URI scheme to redirect the user from a website to the application, but when launching the app, it opens a new instance instead of just restoring the already open one.

我曾尝试使用两个= launchModesingleTask或singleInstance,但他们似乎并没有影响到它。

I have tried both using launchMode="singleTask" or "singleInstance" but they don't seem to affect it.

<!-- Splash. -->
    <activity
        android:launchMode="singleTask"

        android:name=".Activities.Splash"
        android:label="@string/app_name"
        android:screenOrientation="portrait"
        android:windowSoftInputMode="stateHidden"
        >

        <intent-filter>
            <data android:scheme="customscheme" />
            <action android:name="android.intent.action.VIEW"/>
            <category android:name="android.intent.category.BROWSABLE" />
            <category android:name="android.intent.category.DEFAULT"/>
        </intent-filter>

        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>
            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>


    </activity>

例如:


  1. 用户打开应用程序,从活动A去活动B。

  1. User opens the application, from Activity A goes to Activity B .

用户打开网站,并得到通过URI重定向回应用程序。

User opens website, and gets redirected back to app via URI.

应用程序现在在活动A。

Application is now on Activity A.

用户presses后退按钮,破坏活动A。

User presses back button, destroying Activity A.

活动B现在是$从一审p $ psent。

Activity B is now present from first instance.

感谢您的时间和帮助!

推荐答案

假设你没有设置的android:taskAffinity 的活动B,这是在做正确的事

Assuming that you've not set android:taskAffinity for Activity B, this is doing the correct thing.


  • 当你启动应用程序启动ActivityA

  • ActivityA推出ActivityB到同一个任务(即使你设置 launchMode =singleInstance为ActivityB,Android将忽略,因为它具有相同的 taskAffinity 作为ActivityA)

  • 用户打开网站,并重定向回应用程序。 Android将应用程序(含有ActivityA和ActivityB)到前台现有任务,并创建ActivityA的一个新实例,并把在现有activies的前头。现在,你有ActivityA任务堆栈 - > ActivityB - > ActivityA

  • 用户presses的BACK按钮,完成对堆栈的顶部ActivityA的实例,下面揭示ActivityB的实例。

  • When you launch the app it starts ActivityA
  • ActivityA launches ActivityB into the same task (even if you set launchMode="singleInstance" for ActivityB, Android will ignore that since it has the same taskAffinity as ActivityA)
  • User opens website and is redirected back to the app. Android brings the existing task for the application (containing ActivityA and ActivityB) to the foreground and creates a new instance of ActivityA and puts that on top of the existing activies. Now you have a task stack with ActivityA -> ActivityB -> ActivityA.
  • User presses the BACK button, which finishes the instance of ActivityA on top of the stack and reveals the instance of ActivityB underneath.

如果你想一想,Android的不能,因为它是由ActivityB覆盖打开ActivityA的现有实例。

If you think about this, Android cannot open the existing instance of ActivityA because it is covered by ActivityB.

有得到解决这个问题,这取决于您希望您的应用程序在不同情况下的行为的多种方式。让我们假设,如果应用程序已经在运行,你想要的任务堆栈被清除回ActivityA的根实例,并要重新使用该实例(而不是再创建一个)。一要做到这一点最简单的方法就是让网络浏览器中打开另一个活动(不ActivityA),我们称之为 RedirectActivity 。这种新的活动有以下code的的onCreate()

There are multiple ways of getting around this, depending on how you want your app to behave in different circumstances. Let's assume that, if the app is already running, you want the task stack to be cleared back to the root instance of ActivityA and you want to reuse that instance (and not create another one). One of the easiest ways to do this is to have the web browser open another activity (not ActivityA), let's call it RedirectActivity . This new activity has the following code in its onCreate():

super.onCreate(...);
Intent intent = new Intent(this, ActivityA.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intent);
finish();

这code会做下列之一,具体...

This code will do one of the following, depending...


  1. 如果应用程序尚未运行,它会创建ActivityA的新实例。

  2. 如果应用程序已经在运行,它会清除可能对ActivityA的现有实例之上的任何活动的任务堆栈,然后将返回到ActivityA的现有实例。

请注意:有可能是你可以用它来获得类似的行为适当的和其他技术的其他行为

NOTE: There are other behaviours that might be appropriate and other techniques that you can use to get similar behaviour.

这篇关于Android的 - URI方案 - 从打开的应用程序的多个实例停止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆