从应用程序的Andr​​oid注销应用程序、Andr、oid

2023-09-12 04:35:30 作者:花落忆流年

我试图从我的应用程序注销,当用户点击Logout.It工作正常的情况下登录后用户不关闭应用程序,如果他doed注销。那么,它正在properly.Back按钮是无效的情况下,做登录后,再次显示登录页面 但是,当登录后,用户关闭应用程序,然后他做注销登录页面没有显示它显示一个空白页给我

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

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

            }
        });

在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

在你的建议的维韦克Bhusal 我试图使用共享preF

After Your Suggestions of Vivek Bhusal i tried to use 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();
    }

在登录页面

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.

推荐答案

我注意到,您的注销按钮并启动登录活动,但没有完成的主页活动。要关闭主页:

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

做完一个活动启动另一个之后。这样,你的任务堆栈将只有一个活动,即没有回到历史。检查示例应用程序发布的维韦克Bhusal。

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.

我建议您阅读这篇 SO质疑的的接受的答案,也这个答案获取有关如何执行登出更多的想法和一些注意事项有 Intent.FLAG_ACTIVITY_CLEAR_TOP ,其中,你的情况,是不是真的有必要。

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.