Android活动启动模式和深度链接问题 [英] Android activity launch mode and deeplink issue

查看:67
本文介绍了Android活动启动模式和深度链接问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用有2个活动。
应用程序的根活动是MainActivity。如果凭据丢失或无效,则用户将在完成MainActivity的同时将其重定向到LoginActivity。



如果我将主活动的启动模式设置为singleTask,并且我位于LoginActivity的内部片段=>最小化应用程序->从启动器图标启动应用程序=> MainActivity将启动(因为它是singleTask活动),并重定向到LoginActivity,但当然也重定向到堆栈中的第一个片段。 / p>

所需的行为(如从最近的项目重新启动时发生的那样)是它将保留当前活动的实例并在LoginActivity的堆栈中显示正确的片段



当然可以通过将启动模式设置为singleTop轻松解决,但是会发生另一个问题! DeepLink麻烦!



如果我的应用程序在后台打开,并且在singleTop模式下单击浏览器或电子邮件中的链接-该应用程序将作为子任务打开的应用程序(如果您单击最近,则您将看到您的应用程序打开了两次-一次是以前存在的常规实例,另一次则包含在浏览器/电子邮件中)。
这当然是非常不便的,可以通过-您知道-将启动模式设置为singleTask来解决!



对此问题的任何帮助将是最多的

 <!-主要活动->。 
< activity
android:name =。MainActivity
android:label = @ string / app_name
android:launchMode = singleTask
android: screenOrientation = portrait>
< intent-filter>
< action android:name = android.intent.action.MAIN />
< category android:name = android.intent.category.LAUNCHER />
< / intent-filter>
< intent-filter>
< action android:name = android.intent.action.VIEW />

<类别android:name = android.intent.category.DEFAULT />
< category android:name = android.intent.category.BROWSABLE />
< data
android:host = somehost
android:scheme = myscheme />
< / intent-filter>
< / activity>

<!-登录活动->
<活动
android:name = LoginActivity
android:launchMode = singleTask
android:screenOrientation = portrait />


解决方案

感谢orelzion的技巧,我设法解决了这个问题:



我确实创建了一个新的DeepLinkActivity,它启动了MainActivity(我将启动模式从 singleTask更改为 singleTop),并带有以下标志:

 Intent。FLAG_ACTIVITY_REORDER_TO_FRONT| Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK 

需要所有标志来解决尝试时出现的一些问题解决我的问题。


My app has 2 activities. The root activity of the app is the MainActivity. If credentials are missing or invalid the user will be redirected to the LoginActivity while finishing the MainActivity.

If i set the launch mode of the main activity to singleTask and I am in an inner fragment of the LoginActivity => minimize the app -> launch the app from the launcher icon => The MainActivity launches (since it is a singleTask activity) and redirects to the LoginActivity but of course to the first fragment in the stack.

The wanted behavior (as is happening when re-launching from recents) is that it would retain the instance of the current activity and display the correct fragment in the stack of the LoginActivity.

This could of course be solved easily by setting the launch mode to singleTop but then a different issue happens! DeepLink trouble!

If my app is open in the background and I click on a link from a browser or email when in singleTop mode - the app will be opened as a subtask of the forwarding app (if you click recents you will see your app opened twice - once as a regular instance that was there before and one enclosed within the browser/email). This of course is tremendously inconvenient and can be solved by - well you know - setting the launch mode to singleTask!

Any help on this issue will be most appreciated.

   <!-- Main Activity -->
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:launchMode="singleTask"
        android:screenOrientation="portrait">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        <intent-filter>
            <action android:name="android.intent.action.VIEW" />

            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE" />
            <data
                android:host="somehost"
                android:scheme="myscheme" />
        </intent-filter>
    </activity>

    <!-- Login Activity -->
    <activity
        android:name="LoginActivity"
        android:launchMode="singleTask"
        android:screenOrientation="portrait" />

解决方案

Well thanks to the tip by orelzion I managed to solve this issue:

I did create a new DeepLinkActivity which started the MainActivity (which i changhed the launch mode from "singleTask" to "singleTop") with the following flags:

Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK

All flags are needed in order to take care of a few issues that came up while trying to fix my problem.

这篇关于Android活动启动模式和深度链接问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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