简体   繁体   中英

How to remove an activity from a backstack? startActivityForResult

I have a problem with my simple login app using SharingPreferences. Imagine,that user logs in to application. He has "Log out" button inside his personal account. When user already logged in then he shouldn't return to login activity when he clicks "Back" button on his smartphone, he should return only then he clicks log out button and then he may fill fields with other data(other login,other password).

So, I can't finish() my first activity,because it is possible to return back.

Doesn't work ( it is impossible to return back to login (MainActivity) when I click LOG OUT button.

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

Don't finish activity when you are using startActivityForResult() method just put your code ->

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

in logOut click and on back pressed to get back to the Log In Activity

您必须在进行 SuccessActivity 时完成 LoginActivity 并且在单击 Logout 按钮时您可以再次启动 LoginActivity 在那里您可以将数据从 SuccessActivity 传递到 LoginActivity 并通过检查getIntent.getExtras()是否为空来完成或者你可以检查getIntent.hasExtra()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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