-1

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"));
AndroidBoy
  • 65
  • 8
  • 2
    How is that possible? Did you override `onBackPressed`? Is there something else (e.g. keyboard) that gets closed after first back pressed, and then the activity on the second..? – Vucko Jul 29 '16 at 09:33
  • Try to override(super only) your onBackPressed and brakepoint there to see if the method is invoked. – nutella_eater Jul 29 '16 at 09:35
  • The problem is, In my Activity-A contains Button when i press that button it will open Activity-B. in same page i have Intent – AndroidBoy Jul 29 '16 at 09:36
  • 2
    could you maybe show us some more code from activity B – Boe-Dev Jul 29 '16 at 09:36
  • In my Activity-B nothing more codes, just print that value @Boe-Dev – AndroidBoy Jul 29 '16 at 09:39
  • @Vucko I never use onBackPressed and no keyboard open/close in second activity – AndroidBoy Jul 29 '16 at 09:42
  • Please post the whole codes for Activity-A and Activity-B. And do your activities extend a super class? – DysaniazzZ Jul 29 '16 at 09:42
  • Back button navigation feature is the default behavior of android. If it is not working as default then there was some issues on your onBackPressed() or finish() methods – LvN Jul 29 '16 at 09:42
  • 1
    [Visit here ...](http://stackoverflow.com/questions/4038479/android-go-back-to-previous-activity) [and here ...](http://stackoverflow.com/questions/4038479/android-go-back-to-previous-activity) This source may help you !!! – Satan Pandeya Jul 29 '16 at 09:45
  • problem is passmyValue(); because i call two times in my Activity-A. I hope this is the problem – AndroidBoy Jul 29 '16 at 09:47

5 Answers5

2

So i think you must be able to play with ANDROID INTENTS. And this might be the Similar Question to you. i hope it helps you if not please feel free to reply. :) :)

Community
  • 1
  • 1
Niroj
  • 1,114
  • 6
  • 29
1

Your problem is in this snippet of code:

// Launching Add_City Screen
Intent i = new Intent(getApplicationContext(), Acti-B.class);
startActivity(i);
passmyValue();

You starting Activity-B two time, first time without any extras and second time inside passmyValue().

As your Activit-B, I think, uses default launch mode, then you have 2 instances of Activity-B. So your back stack will looks like: Activity-A -> Activity-B -> Activity-B. Thus, you need to press back twice to return to Activity-A.

Just remove those lines:

Intent i = new Intent(getApplicationContext(), Acti-B.class);
startActivity(i);
Michael Spitsin
  • 2,539
  • 2
  • 19
  • 29
1

In this piece of code:

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

                    }

Remove the first intent because you're starting the Activity twice. This in turn means that your Activity B is started once without the intent extra, and on top of that with it. Pressing back closes the first one, and once more closes the second instance. Simply remove the lines and start the B activity like this:

@Override
   public void onClick(View view) {
   passmyValue();
}
Vucko
  • 7,371
  • 2
  • 27
  • 45
1

In onClick() ,you are starting activity-B by

Intent i = new Intent(getApplicationContext(), 
    Acti-B.class);
                        startActivity(i);

and you are again calling startActivity(intent) in PassMyValue method where intent is same as i. So you are starting Activity-B 2 times. You need to fix it by removing

Intent i = new Intent(getApplicationContext(), 
    Acti-B.class);
                        startActivity(i);
0

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);
}
DysaniazzZ
  • 825
  • 15
  • 29