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

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

问题描述

我正在开发一个具有两个活动的应用程序: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.

是否有任何方法可以根据保存的首选项以编程方式在启动器活动之间进行切换?

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中以编程方式配置启动器活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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