简体   繁体   English

从应用程序 Android 注销

[英]Logout from the application Android

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<\/strong>代码<\/strong>

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

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, ie, no back history. 这样,您的任务堆栈将只有一个活动,即没有回溯历史记录。 Check the example app posted by Vivek Bhusal. 查看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. 我建议阅读这个SO问题接受的答案 ,以及关于如何执行注销的更多想法的答案以及使用Intent.FLAG_ACTIVITY_CLEAR_TOP一些注意事项,在您的情况下,这不是必需的。

  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. 因为我看到当这个条件成立时你完成了活动我认为这意味着注销但是isLoggingOut应该是真的,但你检查它是否是假你叫完成。 am i right?? 我对吗??

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. 问题是,您在类中定义了bologout的布尔值,它会在您关闭应用程序后重置。 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.. 一旦你关闭你的应用程序它就会被重置,你的应用程序将无法工作..我建议你使用Sharedpreference来存储该值。

Good luck 祝好运

Updates with example application: 使用示例应用程序更新:

MainActivity.java 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 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 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 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 试试这个例子,希望这会对你有所帮助

You can use the SharedPreferences class in android. 您可以在android中使用SharedPreferences类。 It can use to save the value and load it again. 它可以用来保存值并再次加载它。 This is the docs and tutorial . 这是文档教程

The onNewIntent() should be used in singleTop activities. onNewIntent()应该在singleTop活动中使用。 Beside that, I don't see a startActivityForResult() call to Login Page/Activity. 除此之外,我没有看到对Login Page / Activity的startActivityForResult()调用。

Blank page means your activity has not setup its view (with setContentView() ). 空白页表示您的活动尚未设置其视图(使用setContentView() )。

I would suggest moving your "logging out" checks and setContentView() to onResume() of the Login Page/Activity. 我建议将“注销”检查和setContentView()到Login Page / Activity的onResume()

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

case R.id.drawer_item_logout: //name of logout button 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;

[1]

Your finish call missing when you click on the logout button当您单击注销按钮时,您的完成呼叫丢失

Use This用这个

finish();结束();

"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM