[英]Android issue when switching from browser to app via intent filter
I have an application that starts up with a login activity, which redirects the user to a browser in order to authenticate to a service (has to be done via the browser, can't use a REST API or curl), and then jumps back to the application with a session ID. 我有一个以登录活动启动的应用程序,该应用程序将用户重定向到浏览器以对服务进行身份验证(必须通过浏览器完成,不能使用REST API或curl),然后跳回带有会话ID的应用程序。 Going from the application to the browser is easy, I just launch it like so: 从应用程序到浏览器很容易,我像这样启动它:
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://domain.com/login/?url=appscheme://loggedin&sessionTransfer=window"));
startActivity(intent);
Once the user has logged in, the browser is supposed to jump back to the application (with a session ID) via an intent filter on the destination activity, declared using android.intent.category.BROWSABLE. 用户登录后,浏览器应该通过目标活动(使用android.intent.category.BROWSABLE声明)上的意图过滤器跳回到应用程序(带有会话ID)。
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.BROWSABLE" />
<category android:name="android.intent.category.DEFAULT" />
<data
android:host="loggedin"
android:scheme="appscheme" />
</intent-filter>
The problem is that instead of resuming execution under the original application instance, it appears to execute everything after the initial login screen under the browser instance. 问题在于,与其在原始应用程序实例下恢复执行,不如在浏览器实例下的初始登录屏幕之后执行所有操作。 This is bad for a few reasons. 这是坏的,原因有几个。 First of all, it means that the Recent Apps menu shows everything the user has been doing as happening in the browser, and the main application only shows a login button. 首先,这意味着“最近使用的应用程序”菜单显示用户在浏览器中所做的所有事情,而主应用程序仅显示一个登录按钮。 Secondly, whenever the app is closed and the browser is opened, the browser tends to reopen the app from the existing tab. 其次,无论何时关闭应用程序并打开浏览器,浏览器都倾向于从现有选项卡中重新打开应用程序。
How can I use the browser and then ensure that when the user is finished logging in via the browser, all activity resumes under the original app instance? 如何使用浏览器,然后确保用户完成通过浏览器的登录后,所有活动都在原始应用实例下恢复?
EDIT: In case this helps to improve clarity, this is the intended flow, including how each activity is supposed to be listed in the Recent Apps list: MyActivity1 (associated with MyApp) -> Browser -> MyActivity2 (associated with MyApp) 编辑:如果这有助于提高清晰度,这是预期的流程,包括应如何在“最近的应用程序”列表中列出每个活动: MyActivity1(与MyApp相关联) -> 浏览器 -> MyActivity2(与MyApp相关联)
And here's what actually happens: MyActivity1 (associated with MyApp) -> Browser -> MyActivity2 (associated with Browser) 这是实际发生的情况: MyActivity1(与MyApp关联) -> 浏览器 -> MyActivity2(与浏览器关联)
So right now, if a user wanted to go check an email and then come back to my app, they would actually have to select Browser from Recent Apps for it to work. 因此,现在,如果用户想查看一封电子邮件然后返回我的应用程序,则实际上他们必须从“最近使用的应用程序”中选择“浏览器”才能正常工作。
Turned out that someone asked a similar question here: Starting a new Android Activity in different task 原来有人在这里问类似的问题: 在不同的任务中启动新的Android活动
To summarize my solution, I moved the intent-filter to the login activity and declared android:launchMode="singleTask". 总结我的解决方案,我将意图过滤器移至登录活动并声明了android:launchMode =“ singleTask”。 That way, the browser reopens the login activity and consistently runs onNewIntent(), where I repackage the browser data into a new intent and open the destination activity as intended. 这样,浏览器将重新打开登录活动,并持续运行onNewIntent(),在这里我将浏览器数据重新打包为新的intent,然后按预期方式打开目标活动。 A little convoluted, but it works! 有点令人费解,但它有效!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.