문제

My app starts with a log in activity and after user logs in, it redirects to MainActivity. The point is, I want to start the app from MainActivity when the user has already logged in. And Also I want to pressing back from MainActivity doesn't redirect to LoginActivity. I store the log in status in shared prefrences.
For more clarity:
Launcher -> LoginActivity -> MainActivity ---- // when not logged in
Launcher -> MainActivity ---- // when logged in
MainActivity --(pressing back button)--> Launcher

I've searched the web and SO question since this is a straight forward question but seems I don't know the correct phrase to search. Please help me out.

도움이 되었습니까?

해결책 2

Launcher -> LoginActivity -> MainActivity ---- // when not logged in

Launcher -> MainActivity ---- // when logged in

You cannot change the launcher behaviour in this way.

To achieve what you want to do simply check if the user is logged in or not in the onCreate(...) method of the LoginActivity. If the user is logged in than do the following...

startActivity(this, MainActivity.class);
finish();

If the user is not logged in then do what you need to do to log the user in then call the above code.

다른 팁

For bypassing LoginActivity and going to Launcher onBackPressed after your Login to MainActivity, put no history inside your manifest to your LoginActivity

<activity
    android:name="com.your.package.LoginActivity"
    android:noHistory="true" />

Use SharedPreferences to check if logged in or not.

You can set the string on login:

SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
editor.putString("login", "yes");
editor.commit();

You can check login value by loading from SharedPreferences like this:

    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
    // "login","no" means default string value is "no", so if you didn't set yes after login, it will be no as default
    if(preferences.getString("login", "no").equals("yes")){
         //login value is yes, so start mainactivity
    }
    else{
        //login value is no, so start loginactivity
    }

Put a splash screen as your launcher activity which will decide which screen to launch. i.e if logged in go to mainactivity if not then loginactivity . Both mainactivity & login activity will have splash screen as your parent activity.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top