简体   繁体   English

我如何检查是否开始活动。 安卓

[英]How i check whether to start an activity or not. android

I'm new to Android. 我是Android新手。 My MainActivity is a Login activity. 我的MainActivity是一个Login活动。 I want to check if a user is logged in, then starting the activity again should not show MainActivity, it should directly display the Dashboard Activity. 我想检查用户是否已登录,然后再次启动活动应不显示MainActivity,而应直接显示仪表板活动。 I'm checking the login on the basis of value stored in the shared preferences. 我正在基于共享首选项中存储的值检查登录名。

sharedPreferences = this.getSharedPreferences("LoginDetail", Context.MODE_PRIVATE);
    String id = sharedPreferences.getString("userId","");
    if(!id.equalsIgnoreCase("") && id.length() > 5)
    {
        Intent i = new Intent(MainActivity.this, StudentSignin.class);
        startActivity(i);
        MainActivity.this.finish();
    }

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

I'm Trying to do something like that in onCreate Method. 我正在尝试在onCreate方法中做类似的事情。 If user is not logged in he/she must login to continue; 如果用户未登录,则必须先登录才能继续。 Thanks in advance. 提前致谢。

Set a splash screen to your app, make it the launcher activity, and decide there which activity should be shown. 为您的应用设置启动屏幕,将其设置为启动器活动,然后决定应显示哪个活动。

public class Splash extends AppCompatActivity{

    public void onCreate...{
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        sharedPreferences = this.getSharedPreferences("LoginDetail", Context.MODE_PRIVATE);
        String id = sharedPreferences.getString("userId","");
        if(userLogged(){
            //Go to dashboard
        }else{
            //Go to login screen
        }
    }



}

You can tell if a an Activity is started based on the life cycle state it currently is in, ie onCreate, onStart, onResume, onPause, onStop, onDestory. 您可以根据活动当前处于生命周期的状态(即onCreate,onStart,onResume,onPause,onStop,onDestory)来判断一个Activity是否已启动。 It seems you want some sort of flag to query that indicates if an activity is started, unfortunately android doesn't have that faculty. 看来您想查询某种标志来指示活动是否已开始,但是不幸的是android并没有该能力。 But even better are the life cycles that I mentioned above, with those, you can get a very accurate gauge of where the Activity is ie if it is being created, starting, resuming, pausing, stopping or being destroyed. 但是,我上面提到的生命周期甚至更好,有了这些生命周期,您可以非常准确地了解活动的位置,即活动是在创建,开始,恢复,暂停,停止还是被破坏。 This allows developers to developer very complex app, that don'y only rely on when the activity is started. 这使开发人员可以开发非常复杂的应用程序,该应用程序仅在活动开始时才依赖。

What I recommend you doing is writing a check logged in function and a login function that you should call inside of onStart(). 我建议您做的是编写一个check登录功能和一个应该在onStart()内部调用的登录功能。 This lifecycle function is always called when an Activity becomes visible. 当活动可见时,总是调用此生命周期函数。 The benefit of doing this is, that you can always check if a user logged in state has changed in this activity. 这样做的好处是,您始终可以检查此活动中用户登录状态是否已更改。 The onCreate function only gets called once during an Activity's lifecycle and that is when it is created/recreated. onActivity函数仅在Activity的生命周期中被调用一次,也就是在创建/重新创建时。 This would handle the use case where the user isn't logged in, and they temporarily exit the app (they don't close the activity, but just press the home button) and they resume, they are indefinitely stuck and unable to login until they restart the app (kill the activity and restart it) because the login logic is inside of onCreate and it won't get called again. 这将处理用户未登录,用例暂时退出应用程序(他们不关闭活动,而只是按下主页按钮)的用例,然后他们继续工作,无限期地卡住并且无法登录,直到他们重新启动应用程序(杀死该活动并重新启动它),因为登录逻辑位于onCreate内,并且不会再次被调用。 But putting the logic inside of onStart, which will get executed whenever the activity becomes visible again, the bug will be solved. 但是将逻辑放到onStart内,只要活动再次可见,该逻辑就会执行,该错误将得到解决。

Furthermore, you should combine this logic with @Jonathan Aste's answer. 此外,您应该将此逻辑与@Jonathan Aste的答案结合起来。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM