title: Logout from the application Android
body: <p>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 </p>
  
  <p><strong>Code</strong> </p>
  
  <pre><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;
      }
  }
  </code></pre>
  
  <p><strong>OnClick of logout</strong></p>
  
  <pre><code>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);
  
              }
          });
  </code></pre>
  
  <p><strong>IN the LoginActivity</strong></p>
  
  <pre><code>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);
  
      }
  </code></pre>
  
  <p>Please suggest me what i have done wrong in this</p>
  
  <p>After Your Suggestions  of <strong>Vivek Bhusal</strong> i tried to use sharedpref </p>
  
  <p><strong>HomePage logout Clicked</strong></p>
  
  <pre><code>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();
      }
  </code></pre>
  
  <p>On the <strong>Login Page</strong></p>
  
  <pre><code>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);
          }
      }
  </code></pre>
  
  <p>Still the same issue showing a blank screen when i again restart the app and then do the logout.</p>