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.
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>
Example:
User opens the application, from Activity A goes to Activity B .
User opens website, and gets redirected back to app via URI.
Application is now on Activity A.
User presses back button, destroying Activity A.
Activity B is now present from first instance.
Thanks for your time and help!
答案 0 :(得分:2)
假设您没有为活动B设置android:taskAffinity
,这是正确的做法。
launchMode="singleInstance"
,Android也会忽略它,因为它与ActivityA具有相同的taskAffinity
)如果您考虑到这一点,Android无法打开ActivityA的现有实例,因为ActivityB涵盖了它。
有多种方法可以解决此问题,具体取决于您希望应用在不同情况下的行为方式。让我们假设,如果应用程序已在运行,您希望将任务堆栈清除回ActivityA的根实例,并且您希望重用该实例(而不是创建另一个实例)。最简单的方法之一是让Web浏览器打开另一个活动(而不是ActivityA),让我们称之为RedirectActivity
。此新活动的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();
此代码将执行以下操作之一,具体取决于......
注意:还有其他可能适合的行为以及可用于获得类似行为的其他技术。