Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Logout from the application Android

I have tried to logout from my app when the user clicks on Logout.It is working fine in case the user after login without closing the app if he doed logout .Then it is working properly.Back button will be ineffective in case to show again the login page after doing login But when the user after login closes the application and then he does logout the login page is not showing it is showing a blank page to me

Code

public class AppState {
    private static AppState singleInstance;

    private boolean isLoggingOut;

    private AppState() {
    }

    public static AppState getSingleInstance() {
        if (singleInstance == null) {
            singleInstance = new AppState();
        }
        return singleInstance;
    }

    public boolean isLoggingOut() {
        return isLoggingOut;
    }

    public void setLoggingOut(boolean isLoggingOut) {
        this.isLoggingOut = isLoggingOut;
    }
}

OnClick of logout

logout.setOnClickListener(new OnClickListener() {
    @Override
            public void onClick(View arg0) {
                SharedPreferences myPrefs = getSharedPreferences("MY",
                        MODE_PRIVATE);
                SharedPreferences.Editor editor = myPrefs.edit();
                editor.clear();
                editor.commit();
                AppState.getSingleInstance().setLoggingOut(true);
                Log.d(TAG, "Now log out and start the activity login");
                Intent intent = new Intent(HomePage.this,
                        LoginPage.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);

            }
        });

IN the LoginActivity

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == MAIN_ACTIVITY_REQUEST_CODE) {

            if (!AppState.getSingleInstance().isLoggingOut()) {
                finish();
            } else {
                AppState.getSingleInstance().setLoggingOut(false);
                super.onActivityResult(requestCode, resultCode, data);
            }
        } else {
            super.onActivityResult(requestCode, resultCode, data);
        }
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);

    }

Please suggest me what i have done wrong in this

After Your Suggestions of Vivek Bhusal i tried to use sharedpref

HomePage logout Clicked

logout.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                SharedPreferences myPrefs = getSharedPreferences("Activity",
                        MODE_PRIVATE);
                SharedPreferences.Editor editor = myPrefs.edit();
                editor.clear();
                editor.commit();
                //AppState.getSingleInstance().setLoggingOut(true);
                setLoginState(true);
                Log.d(TAG, "Now log out and start the activity login");
                Intent intent = new Intent(HomePage.this,
                        LoginPage.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);

            }
        });

private void setLoginState(boolean status) {
        SharedPreferences sp = getSharedPreferences("LoginState",
                MODE_PRIVATE);
            SharedPreferences.Editor ed = sp.edit();
            ed.putBoolean("setLoggingOut", status);
            ed.commit();
    }

On the Login Page

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        SharedPreferences sp = getSharedPreferences("LoginState",
                MODE_PRIVATE);
        boolean stateValue  = sp.getBoolean("setLoggingOut", false);
        if (requestCode == MAIN_ACTIVITY_REQUEST_CODE) {

            if (!stateValue) {
                finish();
            } else {
                //AppState.getSingleInstance().setLoggingOut(false);
                updateLoginState(false);
                super.onActivityResult(requestCode, resultCode, data);
            }
        } else {
            super.onActivityResult(requestCode, resultCode, data);
        }
    }

Still the same issue showing a blank screen when i again restart the app and then do the logout.

like image 215
Developer Avatar asked Sep 30 '13 11:09

Developer


People also ask

How do I logout of an app?

Depending on your device, you can sign out of the Android App by tapping the 'three vertical dots' icon at the top-right corner of the Home or Profile screens or by tapping your device's Settings button when viewing the Home or Profile screens of the app.

How do you exit apps on Android?

Close one app: Swipe up from the bottom, hold, then let go. Swipe up on the app. Close all apps: Swipe up from the bottom, hold, then let go.

How do I keep my Android apps from logging in?

When users log in to your application, store the login status into sharedPreference and clear sharedPreference when users log out. Check every time when the user enters into the application if user status from shared Preference is true then no need to log in again otherwise direct to the login page.


1 Answers

I noticed that your logout button does start the Login activity but does not finish the Home Page activity. To close the Home Page:

Intent intent = new Intent(HomePage.this, LoginPage.class);
startActivity(intent);
finish()  // This call is missing.

To open it, from the Login Page:

Intent intent = new Intent(LoginPage.this, HomePage.class);
startActivity(intent);
finish()

Finish one activity after you start another. This way your task stack will have only one activity, i.e., no back history. Check the example app posted by Vivek Bhusal.

I suggest reading this SO question, the accepted answer, and also this answer for more ideas on how to perform logout and some caveats with Intent.FLAG_ACTIVITY_CLEAR_TOP, which, in your case, is not really necessary.

like image 120
Rafa Avatar answered Sep 28 '22 06:09

Rafa