0

I'm developing an android app, and not understanding the back button.

There is an Activity (say A1) from which by clicking a button, user goes to another Activity (say A2). Once the user has finished with A2 activity, he clicks back-button, to go back to previous activity A1. All the docs say, A1 will onResume() at this point.

And it does. However, if I am in A2, and change the screen orientation (from landscape to portrait or vice versa), then something very different happens. The A2 activity lays itself out again, into the different screen orientation as expected. When I press BACK now, the Activity A2 lays itself out again (no change to screen orientation). Pressing BACK again, again causes Activity A2 to lay itself out again. A THIRD press on back takes you back to Activity A1.

What am I doing wrong here, what am I missing? Thanks

Peter

Goofy
  • 6,098
  • 17
  • 90
  • 156
Peter vdL
  • 4,953
  • 10
  • 40
  • 60

2 Answers2

0

My question was not phrased completely correctly. I slightly simplified the case. I am using a Spinner, not a Button, to transfer to the next Activity.

Spinner (and Gallery) have a gross bug, not mentioned in the docs - the OnItemSelectedListener event handler is called when a user physically clicks the spinner control, and also when a spinner is first laid out by the framework code. Your spinner handling code must therefore determine if an event was triggered by a user selection or by the spinner being laid out. The easiest way to do this is to make the first item in a Spinner always be "no selection made yet", and to ignore all events on that selection. See Android Spinner selection and similar posts.

In my case, the orientation change caused the spinner to get laid out again, and I thus got two events from it, the first the layout event, the second from the previously selected entry. And that caused a bogus second activity to be started, and that meant that 3 presses of the back button were needed to "get back" to the first Activity. It was actually going back on the first press, then the spinner fired a layout event and a regular event putting me in the second Activity twice. That wasn't seen on the screen, but was seen using log messages.

When you change orientation, the current Activity is destroyed, and a new Activity created/started. When you change orientation and press the back key, the previous Activity is popped from the top of the paused stack, destroyed, and a new version of that Activity created/started.

When you change screen orientations, the Activity in the old orientation is never kept. It will be destroyed immediately, or if it is lower down the Paused stack, it will be destroyed when it comes to the top.

Community
  • 1
  • 1
Peter vdL
  • 4,953
  • 10
  • 40
  • 60
-1

you're not handling configuration changes. Check out this link it may help you.

When you change your orientation from portrait to landscape or landscape to portrait and if you are not handling configuration changes, then the activity is recreated.

Sudarshan Bhat
  • 3,772
  • 2
  • 26
  • 53
  • so are you saying the stack of paused activities looks like this? (Arggh, can't put newlines in comments) TOP of stack - Activity A2 in landscape - Activity A2 in portrait END. That seems very counterintuitive. – Peter vdL Feb 15 '12 at 15:07
  • Your back stack will not know whether your activity is in portrait or landscape. So it is like TOP of stack -> Activity A2 -> Yet another Activity A2 -> END of stack. – Sudarshan Bhat Feb 16 '12 at 13:14
  • It seemed counter-intuitive because the suggestion advanced by Enigma was wrong. It was not Enigma's fault -- I did not provide enough information in the way I phrased the question. But the fact remains, a change in config causes the old Activity to be destroyed, not kept on the stack. – Peter vdL Feb 21 '12 at 05:26