从Android应用程序注销

Dev*_*per 10 android android-intent android-activity

当用户点击Logout时,我试图从我的应用程序注销.如果用户登录后没有关闭应用程序,如果他进行了注销,那么它正常工作.然后它正常工作.如果再次显示,返回按钮将无效登录后的登录页面但是当用户登录后关闭应用程序然后他登出登录页面时没有显示它向我显示一个空白页面

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

点击注销

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);

            }
        });
Run Code Online (Sandbox Code Playgroud)

在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);

    }
Run Code Online (Sandbox Code Playgroud)

请告诉我我在这方面做错了什么

在你对Vivek Bhusal的建议后,我尝试使用sharedpref

主页注销单击

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();
    }
Run Code Online (Sandbox Code Playgroud)

登录页面上

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);
        }
    }
Run Code Online (Sandbox Code Playgroud)

当我再次重启应用程序然后进行注销时,显示空白屏幕仍然是同样的问题.

Raf*_*afa 21

我注意到您的注销按钮确实启动了登录活动,但没有完成主页活动.关闭主页:

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

要从登录页面打开它:

Intent intent = new Intent(LoginPage.this, HomePage.class);
startActivity(intent);
finish()
Run Code Online (Sandbox Code Playgroud)

启动另一个活动后完成一个活动.这样,您的任务堆栈将只有一个活动,即没有回溯历史记录.查看Vivek Bhusal发布的示例应用程序.

我建议阅读这个问题,接受的答案,以及关于如何执行注销以及一些注意事项的更多想法的答案Intent.FLAG_ACTIVITY_CLEAR_TOP,在您的情况下,这不是必要的.