【问题标题】:Logout from the application Android从应用程序 Android 注销
【发布时间】:2013-09-30 11:05:07
【问题描述】:

当用户单击注销时,我试图从我的应用程序中注销。如果用户在登录后没有关闭应用程序,如果他注销了,它工作正常。然后它工作正常。返回按钮将无效以防万一登录后再次显示登录页面 但是当用户登录后关闭应用程序然后他注销登录页面没有显示它正在向我显示一个空白页面

代码

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

    }

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

根据您的 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();
    }

登录页面

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

当我再次重新启动应用程序然后注销时仍然显示空白屏幕。

【问题讨论】:

    标签: android android-intent android-activity


    【解决方案1】:

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

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

    从登录页面打开它:

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

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

    我建议阅读此SO questionaccepted answerthis answer,以获取有关如何执行注销的更多想法以及Intent.FLAG_ACTIVITY_CLEAR_TOP 的一些注意事项,在您的情况下,这并不是真正必要的。

    【讨论】:

    【解决方案2】:

    你可以在android中使用SharedPreferences类。它可以用来保存值并再次加载。这是docstutorial

    【讨论】:

      【解决方案3】:
        if (!AppState.getSingleInstance().isLoggingOut()) {
      

      你认为这个说法正确吗?因为我看到当这个条件为真时你完成了我认为这意味着注销但 isLoggingOut 应该为真但是你检查它是否为假你调用完成的活动。我说的对吗?

      【讨论】:

      • 如果用户按下设备的后退按钮,我已经这样做了。
      • 我不确定我是否可以说清楚。应该是这样吗? if(AppState.getSingleInstance().isLoggingOut()){...... 如果没有,抱歉我不知道如何帮助你。
      【解决方案4】:

      我在我的一个应用程序中遇到了类似的问题;但我得到了解决方案。问题是,您在类中定义的布尔值是注销,它会在您关闭应用程序后重置。它是一个变量,用于存储您的值,直到您的应用程序打开。一旦你关闭你的应用程序,它就会被重置,你的应用程序将无法工作。我建议你使用 Sharedpreference 来存储该值。

      祝你好运

      更新示例应用程序:

      MainActivity.java

         public class MainActivity extends Activity {
      
          SharedPreferences SM;
          @Override
          protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.loginpage);
      
              SM = getSharedPreferences("userrecord", 0);
               Boolean islogin = SM.getBoolean("userlogin", false);
               if(islogin){
                  Intent intent = new Intent(MainActivity.this, Dashboard.class);
                  startActivity(intent);
                  finish();
                  return;
               }
      
              Button login = (Button) findViewById(R.id.login);
              final EditText ETuser = (EditText) findViewById(R.id.editText1);
              final EditText ETpass = (EditText) findViewById(R.id.editText2);
      
              login.setOnClickListener(new View.OnClickListener() {
      
                  @Override
                  public void onClick(View arg0) {
                      String user = ETuser.getText().toString();
                      String pass = ETpass.getText().toString();
                      if(user.equalsIgnoreCase("admin") && pass.equalsIgnoreCase("admin")){
                          Editor edit = SM.edit();
                          edit.putBoolean("userlogin", true);
                          edit.commit();
      
                          Intent intent = new Intent(MainActivity.this, Dashboard.class);
                          startActivity(intent);
                          finish();
                      }else{
                          Toast.makeText(getApplicationContext(), "Username/Password Invalid", Toast.LENGTH_LONG).show();
                      }
                  }
              });
          }
      
      }
      

      Dashboard.java

      public class Dashboard extends Activity{
      
          @Override
          protected void onCreate(Bundle savedInstanceState) {
              // TODO Auto-generated method stub
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);
      
              Button logout = (Button) findViewById(R.id.logout);
              logout.setOnClickListener(new View.OnClickListener() {
      
                  @Override
                  public void onClick(View arg0) {
      
                      SharedPreferences SM = getSharedPreferences("userrecord", 0);
                      Editor edit = SM.edit();
                      edit.putBoolean("userlogin", false);
                      edit.commit();
      
                      Intent intent = new Intent(Dashboard.this, MainActivity.class);
                      startActivity(intent);
                      finish();
      
                  }
              });
      
          }
      
      }
      

      loginpage.xml

      <?xml version="1.0" encoding="utf-8"?>
      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="vertical"
          android:padding="10dp" >
      
          <EditText
              android:id="@+id/editText1"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:ems="10" >
      
              <requestFocus />
          </EditText>
      
          <EditText
              android:id="@+id/editText2"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:ems="10"
              android:inputType="textPassword" />
      
          <Button
              android:id="@+id/login"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="login" />
      
      </LinearLayout>
      

      activity_main.xml

      <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:tools="http://schemas.android.com/tools"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:paddingBottom="@dimen/activity_vertical_margin"
          android:paddingLeft="@dimen/activity_horizontal_margin"
          android:paddingRight="@dimen/activity_horizontal_margin"
          android:paddingTop="@dimen/activity_vertical_margin"
          tools:context=".MainActivity" >
      
          <TextView
              android:id="@+id/textView1"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="Hello User" />
      
          <Button
              android:id="@+id/logout"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_alignLeft="@+id/textView1"
              android:layout_below="@+id/textView1"
              android:layout_marginLeft="50dp"
              android:layout_marginTop="57dp"
              android:text="Logout" />
      
      </RelativeLayout>
      

      试试这个例子,希望对你有帮助

      【讨论】:

      • 如果您能建议我进行更改并进行更多解释,将非常感谢您
      • 我已经尝试并发布了代码,发现仍然是同样的问题
      • 我无法在您的帖子中发表评论,但是 SharedPreferences 的用途是什么 myPrefs = getSharedPreferences("Activity", MODE_PRIVATE); SharedPreferences.Editor 编辑器 = myPrefs.edit();编辑器.clear(); editor.commit();您还检查了登录页面顶部的共享偏好值。
      • 我正在删除我在登录 sharedpref 时保存的用户的登录 ID 和密码。所以单击注销我正在删除这些详细信息
      • 我不想在点击后退按钮时返回任何历史记录。这是第一个要求。
      【解决方案5】:

      onNewIntent() 应该用于singleTop 活动。 除此之外,我没有看到对登录页面/活动的startActivityForResult() 调用。

      空白页表示您的活动尚未设置其视图(使用setContentView())。

      我建议将您的“注销”检查和setContentView() 移至登录页面/活动的onResume()

      【讨论】:

        【解决方案6】:

        此代码用于从应用程序注销

                Intent logoutintent = new Intent(this, LoginActivity.class);
                startActivity(logoutintent);
                SharedPreferences loginSharedPreferences;
                loginSharedPreferences = getSharedPreferences(
                        LoginActivity.MyPREFERENCES, Context.MODE_PRIVATE);
                Editor editor = loginSharedPreferences.edit();
                editor.putString("UniqueId", "");
                editor.commit();
                finish();
        

        【讨论】:

          【解决方案7】:

          case R.id.drawer_item_logout: //注销按钮的名称

                              AlertDialog.Builder builder=new AlertDialog.Builder(Home.this); //Home is name of the activity
                              builder.setMessage("Do you want to exit?");
                              builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                                  @Override
                                  public void onClick(DialogInterface dialog, int id) {
          
                                      finish();
                                      Intent i=new Intent();
                                      i.putExtra("finish", true);
                                      i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); // To clean up all activities
                                     //startActivity(i);
                                      finish();
          
                                  }
                              });
          
                              builder.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
                                  @Override
                                  public void onClick(DialogInterface dialog, int id) {
                                      dialog.cancel();
                                  }
                              });
          
                                  AlertDialog alert=builder.create();
                              alert.show();
          
          
                              break;
          

          【讨论】:

            【解决方案8】:

            单击注销按钮时您的finish() 呼叫丢失

            使用这个

            finish();
            

            【讨论】:

              猜你喜欢
              • 2014-04-16
              • 1970-01-01
              • 1970-01-01
              • 2013-12-31
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2016-02-07
              • 2016-06-03
              相关资源
              最近更新 更多