登出。按下后退按钮,再次登录

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了登出。按下后退按钮,再次登录相关的知识,希望对你有一定的参考价值。

从这个页面我退出了。它正在正确注销,但是当按下后退按钮时,它会再次登录。我已经给出了一个正确的意图功能,但它并没有按照我的命令行事。请告诉我这个问题的解决方案。

欢迎页面代码: -

public class Welcome extends AppCompatActivity {

    Button __btnlogout;
    Button btn1;
    Button btn2;
    DatagramSocketThread mDatagramSocketThread;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome);

        __btnlogout = (Button)findViewById(R.id.btnLogout);


        __btnlogout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SharedPreferences pref= PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
                SharedPreferences.Editor editor = pref.edit();
                editor.putBoolean("isLogin", false);
                editor.commit();
                Intent intent = new Intent(Welcome.this,
                        login.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);

            }
        });




        btn1 = (Button)findViewById(R.id.btn11);
        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.e("MainActivity", "22");

                mDatagramSocketThread = new DatagramSocketThread();
                mDatagramSocketThread.start();
                Intent intent = VpnService.prepare(getApplicationContext());
                if (intent != null) {
                    startActivityForResult(intent, 0);
                } else {
                    onActivityResult(0, RESULT_OK, null);
                }
            }
        });
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            Log.e("MainActivity", "23");

            Intent intent = new Intent(Welcome.this, MyClass.class);
            Log.e("MainActivity", "24");

            startService(intent);
        }

        btn2 = (Button)findViewById(R.id.btn22);
        btn2.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                Log.e("MainActivity", "25");

                Intent intent;
                intent = new Intent(Welcome.this, MyClass.class);
                Log.e("MainActivity", "26");

                stopService(intent);
            }
        });
    }
}

我想在退出时甚至在按下后退按钮后进入登录页面。

登录页面代码: -

public class login extends AppCompatActivity {
    SQLiteDatabase db;
    SQLiteOpenHelper openHelper;
    Button __btnLogin;
    EditText __txtEmail,__txtPass;
    Cursor cursor;




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);





        Log.d("login","13");

        openHelper = new DatabaseHelper(this);
        db=openHelper.getReadableDatabase();
        __btnLogin = (Button)findViewById(R.id.btnLogins);


        __txtEmail = (EditText)findViewById(R.id.txtEmails);
        __txtPass = (EditText)findViewById(R.id.txtPasss);


        SharedPreferences pref= PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
        SharedPreferences.Editor editor = pref.edit();
        editor.putBoolean("isLogin", true);
        editor.commit();






        Log.d("login","14");

        __btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String email = __txtEmail.getText().toString();
                String pass = __txtPass.getText().toString();




                if (pass == "" || email == "") {

                    Toast.makeText(getApplicationContext(),"No Entry", Toast.LENGTH_LONG).show();

                }



                Log.d("login","15");

                cursor = db.rawQuery("SELECT * FROM "+ DatabaseHelper.TABLE_NAME + " WHERE " + DatabaseHelper.COL_5 + " =? AND " + DatabaseHelper.COL_4 + " =? ", new String[]{email,pass});
                Log.d("login","16");

                if(cursor!=null) {
                    Log.d("login","17");

                    if (cursor.getCount()>0) {
                        Log.d("login","18");
                        //cursor.moveToNext();
                        Log.d("login","19");
                        startActivity(new Intent(login.this, Welcome.class));
                        Toast.makeText(getApplicationContext(), "Login Successfully", Toast.LENGTH_LONG).show();
                    }

                    else {
                        Log.d("login","20");
                        Toast.makeText(getApplicationContext(),"Error", Toast.LENGTH_LONG).show();
                        Log.d("login","21");
                    }
                }



            }
        });




    }
}

谢谢

答案

退出时,你需要finish()所有以前的Activity。尝试下面的代码你可以发射标志Intent.FLAG_ACTIVITY_NEW_TASK

Intent intent = new Intent(login.this, Welcome.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
finish();

关于你的问题,你说我想在登出时甚至在按下后退按钮后进入登录页面。 这不是任何应用程序的正确行为。当您从堆栈上的最后一个活动返回时,该应用程序应该关闭。所以不要在Login Activity中禁用onBackPressed()

另一答案

您可以在登录页面上覆盖onBackPressed,然后当用户单击后退时,您可以按照以下方式处理:

@Override
public void onBackPressed()
{
     //super.onBackPressed();  // disable this
}
另一答案

我认为您不应该正确清除共享首选项,或者在呈现欢迎活动之前检查共享首选项值。然后你应该在finish();之后添加startActivity()

SharedPreferences pref= PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = pref.edit();
editor.putBoolean("isLogin", false);
editor.commit();
Intent intent = new Intent(Welcome.this,login.class);         
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
finish();

要防止意外登录,您应该检查之前设置的sharedPreferences值。后退按钮禁用不是一个好选择。

另一答案

您必须在登录时完成欢迎活动,因此除非再次登录,否则用户将无法再次返回此活动。你必须添加finish();注销

Intent intent = new Intent(Welcome.this,login.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();

并且在您登录页面中,您还可以处理按下后退按钮时要执行的操作,退出应用程序或执行其他操作,通过覆盖onBackPressed();方法。

@Override
public void onBackPressed()
{
  super.onBackPressed();
}
另一答案

成功登录后更改了isLogin Flag,更新您的登录活动代码,

public class login extends AppCompatActivity {
    SQLiteDatabase db;
    SQLiteOpenHelper openHelper;
    Button __btnLogin;
    EditText __txtEmail,__txtPass;
    Cursor cursor;




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);





        Log.d("login","13");

        openHelper = new DatabaseHelper(this);
        db=openHelper.getReadableDatabase();
        __btnLogin = (Button)findViewById(R.id.btnLogins);


        __txtEmail = (EditText)findViewById(R.id.txtEmails);
        __txtPass = (EditText)findViewById(R.id.txtPasss);




        Log.d("login","14");

        __btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String email = __txtEmail.getText().toString();
                String pass = __txtPass.getText().toString();




                if (pass == "" || email == "") {

                    Toast.makeText(getApplicationContext(),"No Entry", Toast.LENGTH_LONG).show();

                }



                Log.d("login","15");

                cursor = db.rawQuery("SELECT * FROM "+ DatabaseHelper.TABLE_NAME + " WHERE " + DatabaseHelper.COL_5 + " =? AND " + DatabaseHelper.COL_4 + " =? ", new String[]{email,pass});
                Log.d("login","16");

                if(cursor!=null) {
                    Log.d("login","17");

                    if (cursor.getCount()>0) {
                        Log.d("login","18");
                        //cursor.moveToNext();
                        Log.d("login","19");
  SharedPreferences pref= PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
        SharedPreferences.Editor editor = pref.edit();
        editor.putBoolean("isLogin", true);
        editor.commit();

                        startActivity(new Intent(login.this, Welcome.class));
                        Toast.makeText(getApplicationContext(), "Login Successfully", Toast.LENGTH_LONG).show();
                    }

                    else {
                        Log.d("login","20");
                        Toast.makeText(getApplicationContext(),"Error", Toast.LENGTH_LONG).show();
                        Log.d("login","21");
                    }
                }



            }
        });




    }
}
另一答案

如果单击“注销”按钮,则导航到登录活动,并听取登录活动的后退

意图...完成();

onBackPressed();

以上是关于登出。按下后退按钮,再次登录的主要内容,如果未能解决你的问题,请参考以下文章

如何禁用在android片段类中按下的后退按钮

导航抽屉,处理后退按钮以转到以前的片段?

如果在 WebView 片段中按下后退按钮,如何返回上一页?

使用导航控制器按下后退按钮后如何防止前一个片段显示?

片段里面的片段

使用导航控制器按下后退按钮后,如何防止先前的片段出现?