Search code examples
androidandroid-intentback-stack

Why My Back Button not working Properly


I have a Two Activities Activity-A and Activity-B, And I pass the Value from Act-A to Act B using an Intent. Everything is working good. When I open my Act-B I get Value from Act-A, Now the Problem is when click the Back Button (Twice) then only i get my Act-A. when i press once same activity it done not Navigate me on my Act-A.

Act-A:

    btn_add_city.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View view) {
                        // Launching Add_City Screen
                        Intent i = new Intent(getApplicationContext(), 
    Acti-B.class);
                        startActivity(i);
passmyValue();

                    }
                });

        public void passmyValue(){

                intent = new Intent(getApplicationContext(),Act-B.class);
                intent.putExtra("name", receive.getText().toString());
                startActivity(intent);
            }

Act-B:

receive = (TextView)findViewById(R.id.userHidden);
receive.setText(getIntent().getStringExtra("name"));

Solution

  • You just new two intents and start two activities, which are Activity-B and Activity-B. Actually you hava three activities in the task stack, they are Act-A, Act-B, Act-B.

    It's no need to start the same activity for twice. Try codes below, I think it helps.

    btn_add_city.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            // Launching Add_City Screen
            // It's no need to start the same activity for twice
            passmyValue();
        }
    });
    
    public void passmyValue(){
        intent = new Intent(getApplicationContext(),Act-B.class);
        intent.putExtra("name", receive.getText().toString());
        startActivity(intent);
    }