0

Very simple for experts but i dont know how to search it. So my question is

Which method will create when back arrow is pressed(not menu/soft back button) go to home/parent activity?? i am talking about green circle button not red circle button

Is it onStop(),onPause() or onRestart()??

I am confused with all, though i have read the method but i cannot link it with my project.

Basically user on A activity perform some task and when user go back to parent activity then again perform some other task and when again come to A activity then some task also have to perform?

So which method suits?

Nilesh Indalkar
  • 310
  • 4
  • 16
  • Try to clarify a bit more your case. Add also what have you tried. You may need useful the [activity lifecycle](https://developer.android.com/guide/components/activities/activity-lifecycle.html). Also what do you mean with "back arrow is pressed(not menu/soft back button)"? Are you refering to [up navigation](https://developer.android.com/training/implementing-navigation/ancestral.html)? – Alberto S. May 25 '17 at 15:26
  • i have edited my post – Nilesh Indalkar May 25 '17 at 15:37

3 Answers3

2

Edit

As OP edited the question

So it is about "What is the difference between Action Bar back button and Navigation Back button"

ActionBar "back" button (Green circled) is an "Up" button and it would take you to one level back on your app's navigation hierarchy.

The back button (Red circled) takes you to the last place you were at.

So, Whenever you push back button (< Up) current activity gets destroyed and onDestroy is called and onCreate will be called for the activity that will come up on stack.

Refer this link for more info: http://developer.android.com/design/patterns/navigation.html


Following methods are called in serial, after pressing back button.

onPause()
onStop()
onDestroy() //Activity destroyed.

These methods are called when an activity is launched.

onCreate()
onStart()
onResume()

For more info refer:

https://developer.android.com/guide/components/activities/activity-lifecycle.html

Community
  • 1
  • 1
Mayank
  • 46
  • 4
0

As obvious as it sounds the method that is called when the back button is pressed is onBackPressed():

@Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

Since my Activity implement a navigation Drawer it does manage it and if it's open and you press the back button, it will close

Then of course as the activity ends the onPause() and onStop() methods are called.

When the activity is resumed again the onResume() method will be called

Daniele
  • 4,163
  • 7
  • 44
  • 95
0

The lifecycle methods which will be called when pressed back button are

  • onPause()

  • onStop()

So the current activity will still in stack uncless it is cleared. So again if the user goes to the activity it will call the onRestart() method.

nitish29
  • 327
  • 1
  • 7