如何从后台删除活动? startActivityForResult

时间:2019-01-28 04:57:36

标签: java android

使用SharingPreferences的简单登录应用程序出现问题。 想象一下,该用户登录到应用程序。他的个人帐户中有“注销”按钮。当用户已经登录时,当他单击智能手机上的“返回”按钮时,他不应返回登录活动;只有在单击“退出”按钮后,他才应返回登录状态,然后可以用其他数据(其他登录名,其他密码)填充字段)。

因此,我无法完成()我的第一个活动,因为可以返回。

不起作用(单击“退出”按钮后无法返回登录(MainActivity)。

        signInButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            sharedPreferencesEditor.putString("user_login",login.getText().toString());
            sharedPreferencesEditor.putString("user_password",password.getText().toString());
            sharedPreferencesEditor.apply();
            Log.d("MyLog_name",sharedPreferences.getString("user_login",""));
            Log.d("MyLog_pw",sharedPreferences.getString("user_password",""));


            Log.d("MyTag",login.getText().toString());
            Log.d("MyTag",password.getText().toString());


            if(login.getText().toString().equals(getString(R.string.user_name)) && password.getText().toString().equals(getString(R.string.user_pw))){
                Intent intent = new Intent(MainActivity.this, SuccessActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivityForResult(intent,1);
                finish();
            } else {
                Toast.makeText(getApplicationContext(),"Wrong data(очищаю пароль)",Toast.LENGTH_SHORT).show();
                password.setText("");
            }

        }
    });



@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if(requestCode == 1 && resultCode == RESULT_OK && data!=null){
        String savedUser = data.getStringExtra("username");
        Toast.makeText(getApplicationContext(),"Farewell, " + savedUser,Toast.LENGTH_SHORT).show();
    }else{
        Toast.makeText(getApplicationContext(),"Error(Back button was pressed, no prohibition of back stack)",Toast.LENGTH_SHORT).show();
    }
}

SuccessActivity.java

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

    welcome = (TextView)findViewById(R.id.welcome);
    logOut = (Button) findViewById(R.id.logOut);
    sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());

    savedUserName = sharedPreferences.getString("user_login","");

    welcome.setText("Welcome, " + savedUserName);
    logOut.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent();
            intent.putExtra("username",savedUserName);
            setResult(RESULT_OK, intent);
            finish();
        }
    });
}

2 个答案:

答案 0 :(得分:0)

当您使用startActivityForResult()方法完成活动时,只需将代码->

/////////
Intent intent = new Intent();
intent.putExtra("username",savedUserName);
setResult(RESULT_OK, intent);
finish();
///////////

在注销中单击,然后按一次返回到“登录活动”

答案 1 :(得分:0)

您必须在完成SuccessActivity的同时完成LoginActivity,然后单击Logout按钮,然后才能再次启动LoginActivity。在那儿,您可以将数据从SuccessActivity传递到LoginActivity,并通过检查getIntent.getExtras()是否需要是否为null或您可以检查getIntent.hasExtra()