4

I'm allowing the user to work in both landscape & portrait modes but when I switch between these two modes it just brings the activity to its starting point.

For Example; I enter all the specifications for which I want to view the data in some activity but when I switch the mode from landscape to portrait or portrait to landscape it just brings the activity to its starting point & I've to enter all the specifications again to view my data.

Is there any solution to it or will android always re-create the activity on switching between two modes?

Thanks. :)

DMK
  • 2,368
  • 1
  • 21
  • 34
user1984849
  • 411
  • 7
  • 10
  • Not sure why all the down-votes. I think this is a reasonable question (which has been previously addressed on SO). – Booger Jan 17 '13 at 15:07
  • Fair enough. I like to see all the NULLPointer questions down-voted, but this one seemed reasonable. It obviously doesn't matter too much to me. Cheers. – Booger Jan 17 '13 at 15:10
  • Luksprog: I'm almost done with my app & this is something I just saw as I wasn't caring that much about orientation. :/ – user1984849 Jan 17 '13 at 15:12
  • & I've already searched a lot obviously as I know this is a basic question. But because I didn't find any apt answer for my app. I asked it here. :o – user1984849 Jan 17 '13 at 15:13
  • K.. I went to that link.. But just skipped it thinking that it was waste.. Sorry.. & Thanks a lot Booger. I'm trying to accept your answer but not being able to.. but will accept it as soon as the site will allow me. :) – user1984849 Jan 17 '13 at 15:17

2 Answers2

3

This is the expected behavior by default. It is reasonably easy to handle this situation. Basically you save your Activity state (and data), then repopulate your Activity when the screen is rotated (and the Activity is recreated).

Here is a good SO with the solution:

Handle screen rotation without losing data - Android

also worthwhile to read the docs on this subject (and understand the Activity lifecycle in Android): http://developer.android.com/training/basics/activity-lifecycle/index.html

Community
  • 1
  • 1
Booger
  • 18,137
  • 6
  • 51
  • 64
3

That happens because on orientation change the activity is destroyed and re-created. A way to avoid this could be by adding the following line inside your activity tag in manifest.xml

android:configChanges="orientation|screenSize"

Example :

<activity
     android:name="MyActivity"
     android:configChanges="orientation|screenSize">
</activity>
Abhishek Sabbarwal
  • 3,688
  • 1
  • 23
  • 39
  • Thanks Androider.. this method works too.. It's telling my app that it doesn't need android to interrupt it.. it can handle its data itself.. :) – user1984849 Jan 17 '13 at 15:29