如何动态地选择打开一个应用程序时启动哪个活动应用程序、动态

2023-09-13 02:30:34 作者:余生请指教

我写一个应用程序,您需要先登录到服务使用它之前。从我了解的android到目前为止,你必须选择其中的活动,当您从清单中的启动打开发射。我不知道我现在要启动在编译时哪个活动。我希望用户单击该图标,然后我检查,看看他们是否已经登录,然后决定基础上是否启动登录活动,或者主要的应用程序的活动。有没有办法做到这一点?

I am writing an app that requires you to be logged in to a service before using it. From my understanding of android so far, you have to choose which activity to launch when you open from the launcher in the manifest. I don't know which activity i want to launch at compile time. I want the user to click the icon, then I check and see if they're logged in, then decide based on that whether to launch the login activity, or the main app activity. Is there a way to do this?

推荐答案

没有,因为你必须运行一些code,有没有办法来声明(在清单)这样说。你必须发动根据跟踪,如果用户登录或不通过意向推出什么次活动的活动(清单中设置),然后让本次活动:

No, since you have to run some code, there's no way to declaratively (in manifest) to say this. You have to launch an activity (set in manifest), then have this activity decide based on if the user is logged on or not what second activity to launch via Intent:

final Class<? extends Activity> activityClass;
if(userIsLoggedOn())
    activityClass = LoggedOnActivity.class;
else
    activityClass = LogInActivity.class;

Intent newActivity = new Intent(context, activityClass);
context.startActivity(newActivity);