-1

I have an Activity called A and some event on Activity A calls Activity B. Now when I use the up-navigation in the Activity B, it comes back to Activity A but also calls the onCreate method. If I use the hardware/soft keys for the back, it does not call the onCreate and just comes back to Activity A where it was left. How do I make the up-navigation behave the same way?

My onCreate makes network calls asynchronously. Making another call just wastes data and time. This is the code I use to launch activity B from activity A.

Intent intent = new Intent(MainActivity.this, newsIndexActivity.class);
                    intent.putExtra("CityName", cityName.getText());
                    startActivity(intent);
Samarth Agarwal
  • 2,044
  • 8
  • 39
  • 79

4 Answers4

5

Up and back navigation are not the same, if you want your Up navigation button to behave like back button you should do something like this

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            this.finish();
            return true; 
    }
    return super.onOptionsItemSelected(item);
}

If possible you should take a look to this link:

There you can have more information about this.

KiroHertz
  • 51
  • 3
1

By declaring parent activities’ launchMode as singleTop, we prevent the system from creating a new activity each time we press Up.

Ajay Deepak
  • 471
  • 5
  • 9
0

Without any code seems difficult to resolve, but the most probably cause is that you are calling to startActivity. Just close your current activity using finish() and using the backstack it should behave as expected.

zozelfelfo
  • 3,776
  • 2
  • 21
  • 35
-3

Just call finish() or onBackPressed() method in your button callback listener.

finish();
super.onBackPressed();
Dhrumil Shah - dhuma1981
  • 15,166
  • 6
  • 31
  • 39