-1

I'm trying to create an app in Android, using Eclipse ADT and I use a HTC phone. Here's the problem: after running the app on the phone from Eclipse, I add elements to my ListView and everything is fine, BUT when I turn the device in landscape mode the hole app seems like it restart, there's no more records inside. It's like new. Any ideas why and how I can solve this problem? Please don't tell me to deactivate the screen rotate option from phone settings.

Michael Yaworski
  • 13,410
  • 19
  • 69
  • 97

3 Answers3

3

This is the way android handles orientation changes. It reloads your whole activity. The normal way to handle this situation is to save the state of your activity in onPause() and then retrieve it back in onCreate().

Here is more information on the android activity lifecycle:

http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle

EDIT:

You should implement onPause() anyway, because it will also be called if the phone rings in the middle of running your activity. In this case, when the user comes back from the call your activity will possibly be reloaded from scratch again and the user will lose their state.

Daniel Gabriel
  • 3,939
  • 2
  • 26
  • 37
  • I could be wrong, but I don't think the phone ringing will destroy the activity. `onPause()` will be called, but the user shouldn't lose their state (I *think*). Also, you said "loose", but it's supposed to be "lose". – Michael Yaworski Dec 11 '13 at 00:50
  • 1
    It depends on the length of the call. If it's a quick call the activity may still be running. If it's longer, the activity may be destroyed. The OS decides this, and I'm not sure what it bases the decision on. Maybe memory? CPU usage? Thanks for catching the typo, I fixed it. – Daniel Gabriel Dec 11 '13 at 00:53
  • 1
    `"your activity will be reloaded from scratch again and the user will lose their state."` That's not correct - switching apps doesn't automatically kill the previous foreground app. It is *recommended* to save persistent data in `onPause()` for Activity state, override `onSaveInstanceState()` (depending on need) – A--C Dec 11 '13 at 00:58
  • onSaveInstanceState() isn't guaranteed to be called either, only onPause(). If the state of the activity must be maintained under all circumstances, then you must store it in your onPause() method. (http://developer.android.com/reference/android/app/Activity.html#onPause()) – TheIT Dec 11 '13 at 02:02
1

There is a similar question here. Basically you application is restarted. You can either force your application to portrait or follow the steps here to find out how to handle it properly.

MrEngineer13
  • 38,642
  • 13
  • 74
  • 93
1

I made answer over here about this sort of thing.

Please don't tell me to deactivate the screen rotate option from phone settings.

I'm not sure if you meant the following, but here's a solution:

Add android:configChanges="orientation|screenSize" to your <activity tag, which is in your AndroidManifest.xml, like so:

<activity
    android:name="activity_name"
    android:label="@string/app_name"
    android:configChanges="orientation|screenSize" />

This will prevent the activity from being destroyed when the orientation is changed, like it usually would. There are other ways to fix this as well. Please leave a comment if this is not your desired solution. I can make up another.

Community
  • 1
  • 1
Michael Yaworski
  • 13,410
  • 19
  • 69
  • 97
  • From the [dev docs](http://developer.android.com/guide/topics/manifest/activity-element.html#config): *Using [the configChanges] attribute should be avoided and used only as a last resort.* – A--C Dec 11 '13 at 01:05
  • @A--C But for what reason? It's a good thing to note, but I can't see a real reason for it. It works for me. I don't want my activity being destroyed (simpler that way). – Michael Yaworski Dec 11 '13 at 01:10
  • http://developer.android.com/guide/topics/resources/runtime-changes.html#HandlingTheChange All of it is useful, but the second-last paragraph gives an answer to your question. – A--C Dec 11 '13 at 01:20
  • For the sake of brevity, declaring configChanges means that the system will no longer automatically resolve resource qualifiers relating to the registered config changes. Ie, if you have different images to display for different screen sizes, then you must load them manually instead of making use of resource qualifiers. – TheIT Dec 11 '13 at 01:32
  • @TheIT Of course; but that is entirely up to the author. I don't have any images (or anything of the sort), so there is no downside to me using my answer as a solution. I don't need to handle anything differently when the orientation is shifted. – Michael Yaworski Dec 11 '13 at 01:37
  • 1
    I suggest that you clearly state your assumptions. After all, you are answering someone's question, not solving your own problem. In particular, this method is viable if you don't need to support multiple orientations and screen sizes or are willing to manage the changes manually. For most purposes however, @Daniel Gabriel's solution is more correct. – TheIT Dec 11 '13 at 01:52
  • @TheIT there are still multiple ways to handle configuration changes (that are easier IMO anyways). Either way, I did state that my solution would prevent the activity from being recreated. You said for *most purposes*, his solution is more correct. I disagree. I don't see a lot of people doing any changes when the orientation changes and it's definitely more complicated to save all of the values needed, rather than just have it never be destroyed. You could just set up a simple activity root view that will change the image rotation. Both answers, IMO, are equally *correct*: it depends. – Michael Yaworski Dec 11 '13 at 02:13
  • I appreciate what you're saying, but given that this question is regarding content of his list view when an orientation occurs and not resources, it would seem reasonable to assume that he needed to support orientation changes (you can't know if those different orientations require different resources). Additionally, your solution wouldn't solve the general case of his activity being recreated (and therefore data lost) due to other factors. – TheIT Dec 11 '13 at 02:23
  • @TheIT you could be right. We're getting out of my league here though (I don't know anything about list views or other factors that go into this) so I guess we'll leave it at that. Thanks for the feedback. – Michael Yaworski Dec 11 '13 at 02:53