如何在 android 中以编程方式配置 Launcher 活动? [英] How can I configure Launcher activity programmatically in android?

查看:16
本文介绍了如何在 android 中以编程方式配置 Launcher 活动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发具有两个活动的应用程序:LoginActivityMainActivity.当用户第一次打开应用程序时,他将登录并将他的凭据(用户名和令牌)保存在 Preferences 中.

I am working on an app with two activities : LoginActivity and MainActivity. When the user first opens the app he will login and his credentials (username and token) are saved in Preferences.

现在,如果用户再次打开应用程序,则 MainActivity 应该启动.我试图在 Application 类中的这些活动之间切换,并从清单中删除了 LAUNCHER_ACTIVITY 的意图过滤器,但它不起作用.

Now, if the user opens the app again then MainActivity should start. I tried to switch between these activities in Application class and removed intent-filter for LAUNCHER_ACTIVITY from manifest, but it doesn't work.

有没有办法根据保存的首选项以编程方式在 Launcher Activity 之间切换?

Is there any way of switching between Launcher Activities programmatically on basis of saved preferences?

推荐答案

长话短说,您无法更改默认启动的 Activity.更新:有一个替代方案,如 CommonsWare 在另一个答案中所述.

Long story short, you cannot change the Activity that is launched by default. Update: There is an alternative as described by CommonsWare in another answer.

但是,有一些合理的变通方法.在您的 MainActivity 中,您可以检查用户是否登录并立即将他们重定向到 LoginActivity.这有一个额外的好处,即在您登录后自动返回到 MainActivity.

However, there are reasonable work arounds. In your MainActivity you can check whether the user is logged in and immediately redirect them to the LoginActivity. That has the added benefit of automatically returning to the MainActivity after you have logged in.

或者,您始终可以先转到 LoginActivity,如果用户已经登录,则将它们发送到 MainActivity(重写 Intent 历史记录以删除返回 LoginActivity 或设置 noHistory 清单中的标志).

Alternatively, you can always go first to the LoginActivity, and if the user is already logged in, send them to the MainActivity (rewrite the Intent history to remove the return to LoginActivity or set the noHistory flag in the manifest).

这篇关于如何在 android 中以编程方式配置 Launcher 活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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