13

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.

Developer
  • 6,292
  • 19
  • 55
  • 115

8 Answers8

27

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.

Community
  • 1
  • 1
Rafa Viotti
  • 9,998
  • 4
  • 42
  • 62
0
  if (!AppState.getSingleInstance().isLoggingOut()) {

do you think this statement correct?? because i saw when this condition is true you finish the activity i think that means logout but isLoggingOut should be true however you check if it is false you call finish. am i right??

GrIsHu
  • 29,068
  • 10
  • 64
  • 102
  • this i have done for the case if the user press the back button of the device. – Developer Oct 01 '13 at 06:33
  • i am not sure if i could make myself clear. should it be like this? _if(AppState.getSingleInstance().isLoggingOut()){......_ if not, sorry i have no idea how to help you. – budaancamanyak Oct 03 '13 at 13:52
0

I had similar problem in one of my application; But i got the solution. The problem is, the boolean you have defined isloggingout in your class, it resets after you close your application. Its a variable which stores your value until your application is open. As soon as you close your application it gets reset and your application wont work.. I will suggest you to use Sharedpreference to store that value..

Good luck

Updates with example application:

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>

Try this example and hope this will help you

user98239820
  • 1,411
  • 2
  • 16
  • 30
  • if u can suggest me the changes and explain it more will be thank full to you – Developer Oct 07 '13 at 05:11
  • i have tried and posted the code see that still the same issue – Developer Oct 07 '13 at 07:25
  • I couldn't comment in your post, but what the is the use of SharedPreferences myPrefs = getSharedPreferences("Activity", MODE_PRIVATE); SharedPreferences.Editor editor = myPrefs.edit(); editor.clear(); editor.commit(); also have you checked your sharedpreference value at the top of login page. – user98239820 Oct 07 '13 at 09:00
  • i am deleting the login id and password of the user that i have saved at the time of login in sharedpref.So onclick of logout i am deleting these details – Developer Oct 07 '13 at 09:05
  • i don't want any back history on click of back button .this is the first requirement . – Developer Oct 07 '13 at 10:00
  • no I have tried by your way also it's not working fine same issue a blank page it;s not working it is showing all the – Developer Oct 08 '13 at 09:47
  • Did you try my answer? Let me know if it helped you, please. – Saran Oct 24 '13 at 18:42
0

You can use the SharedPreferences class in android. It can use to save the value and load it again. This is the docs and tutorial.

RussVirtuoso
  • 900
  • 1
  • 9
  • 20
0

The onNewIntent() should be used in singleTop activities. Beside that, I don't see a startActivityForResult() call to Login Page/Activity.

Blank page means your activity has not setup its view (with setContentView()).

I would suggest moving your "logging out" checks and setContentView() to onResume() of the Login Page/Activity.

Saran
  • 3,845
  • 3
  • 37
  • 59
0

This code is used for logout from app

        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();
Bhavinkumar Patel
  • 515
  • 1
  • 12
  • 28
0

case R.id.drawer_item_logout: //name of logout button

                    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;

[1]

Omal Perera
  • 2,971
  • 3
  • 21
  • 26
0

Your finish() call missing when you click on the logout button

Use This

finish();
Phil Dukhov
  • 67,741
  • 15
  • 184
  • 220