0

I have 2 activities in my Android app: A and B. A is the parent of B, as defined in the Android manifest. When stepping through my app with the debugger I've found that when I click on Up in the child activity, B, the onCreate() method of A gets called. But where does control pass to when the user clicks on Back in activity B? Which method of its parent activity, A, gets called?

I've read lots of stuff about navigation in Android, Back vs Up, etc., but I can't find an answer to this simple question. (I would like the Back button to behave like Up in my app but I can't get A's screen to update correctly when the user clicks on Back in B.)

snark
  • 2,462
  • 3
  • 32
  • 63

2 Answers2

4

At least onResume() will be called when activity A becomes active again.

laalto
  • 150,114
  • 66
  • 286
  • 303
  • Thanks - you're right. I overrode all the lifecycle methods in A and B (and onBackPressed()) to show toasts and this is what I found: – snark Dec 19 '13 at 20:42
  • 3
    1) Click on app icon: A.onCreate -> A.onStart -> A.onResume 2) Click on button in A to go to B: A.onPause -> B.onCreate -> B.onStart -> B.onResume -> A.onStop 3a) Click on Up in B: B.onPause -> A.onCreate -> A.onStart -> A.onResume -> B.onStop OR 3b) Click on Back in B: B.onBackPressed -> B.onPause -> A.onRestart -> A.onStart -> A.onResume -> B.onStop So to handle both Up and Back the same way I could update my display for A in either A.onStart or A.onResume. – snark Dec 19 '13 at 20:52
  • 1
    Oops - I'd missed out onDestroy. So it's (abbreviating the method names): (1) Click on app icon: `A.create/A.start/A.resume` (2) Click on btn in A to go to B: `A.pause/B.create/B.start/B.resume/A.stop` (3a) Click on Up in B: `B.pause/A.destroy/A.create/A.start/A.resume/B.stop/B.destroy` OR (3b) Click on Back in B: `B.pause/A.restart/A.start/A.resume/B.stop/B.destroy` (4) Click on Back in A: `A.pause/A.stop/A.destroy` – snark Dec 20 '13 at 17:51
0

override onBackPressed method of activity A it will called when you press Back on Activity B

 @Override
public void onBackPressed()
{
     // code here to show dialog
     super.onBackPressed();  // optional depending on your needs
}
Baby Groot
  • 4,637
  • 39
  • 52
  • 71
  • Thanks. Actually `onBackPressed()` is called in activity B (not A) when I press Back whilst in B. – snark Dec 19 '13 at 20:40
  • i think you are not using super.onBackPressed(); in B onBackPressed method.its working fine for me. – hradesh kumar Dec 20 '13 at 06:24
  • I see what's gone wrong here. When I said activity A is the parent of activity B I meant that A was defined as the parent of B in the AndroidManifest.xml, so that when I press Up in B I will navigate to A. I didn't mean that B inherits from, or extends, A. In my app both A and B extend android.app.Activity and are not related to each other. Too many meanings for 'parent' and 'child' here; sorry for the confusion! – snark Dec 20 '13 at 13:57