1

At the moment I'm developing an android app which makes use of a log-in activity and once the user is logged in, a main activity. The app users a background service (IntentService) to communicate with the server while the user is logging in. The service then stores the fetched user details in a database so that the user doesn't need to log in every time he/she starts the app.

Once the user enters the main activity, the activity fetches some data, via the service, from the server. This data is then displayed (programmatically) on the screen. I think this is quite comparable with how the (well known) Wordfeud app works. First it logs you in, then it fetches the games you are playing from the server and then displays them.

My problem is that if I rotate my screen, the main activity is destroyed and restarted causing the oncreate() function to be called again. In my current design, this means that the data is fetched from the server again, which is quite unnecessary. I only want the data to be fetched again if the user has closed the app and starts to use it again at a later time.

I'm wondering what would be the best idea to fix this? The easy way would be to disallow the rotation of the screen, but this is not very user friendly in my opinion. Another way would be to fetch all the data while the app is communicating with the server to log in the user, and add a reload/fetch button to the screen so the user can fetch the data manually. The downside to this approach would be that if the user reopens the app after a few hours, the data is/could be outdated. This is not so user friendly as well.

Maybe there are other solutions for this problem that I'm not aware of, like the ability to bypass (parts of) the oncreate() function when the screen rotates or something like that.

I'm very curious about if there is a solution to this and if so, what it would be!

Koningh
  • 628
  • 2
  • 8
  • 22
  • possible duplicate of [Activity restart on rotation Android](http://stackoverflow.com/questions/456211/activity-restart-on-rotation-android) – Bryan Herbst Sep 24 '13 at 18:36

5 Answers5

1

Take a look at Forcing Android to not redraw activity on orientation change. If you need to adjust anything on rotation you can override the Activity.onConfigurationChanged method.

To refetch the data when the user comes back to the app override the onResume method and put your logic in there. It's very useful to familiarize yourself with the Activity lifecycle.

Community
  • 1
  • 1
hamidp
  • 156
  • 1
  • 5
  • Yes! I noticed the neat Activity lifecycle flow graph while I was searching for the reason why my code re-executed when the orientation changed. So looking at the graph, OnResume() will also be called if the screen rotation changes. Wouldn't this still happen even if I would use android:configChanges? – Koningh Sep 23 '13 at 12:41
0

Try using android:configChanges="orientation|keyboardHidden|screenSize" in android-manifest

Caution:

Beginning with Android 3.2 (API level 13), the "screen size" also changes when the device switches between portrait and landscape orientation. Thus, if you want to prevent runtime restarts due to orientation change when developing for API level 13 or higher (as declared by the minSdkVersion and targetSdkVersion attributes), you must include the "screenSize" value in addition to the "orientation" value. That is, you must decalare android:configChanges="orientation|screenSize". However, if your application targets API level 12 or lower, then your activity always handles this configuration change itself (this configuration change does not restart your activity, even when running on an Android 3.2 or higher device).

Source : Documentation

Hence, also add "|screenSize" to configChanges if your application targets API 13 and above

General Grievance
  • 4,555
  • 31
  • 31
  • 45
Augustus Francis
  • 2,694
  • 4
  • 22
  • 32
-1

As you rotate your phone, the oncreate() of the activity is called again. Inorder to overcome this problem add this line in manifest activity tag

android:configChanges="orientation"

then later ovverride onConfigurationChanged in your activity. Then run your app now oncreate will not be called when you rotate the screen where as onConfigurationChanged will be called.

I think this may solve your problem

NARESH REDDY
  • 682
  • 4
  • 11
-1

You should add to your manifest something like this:

<activity android:name=".activity.YourActivity" android:configChanges="orientation"/>

Then on orientation change your activity will not be restarted - onCreate will not be called, but you can handle orientation change event using method onConfigurationChanged(Configuration newConfig)

TommyNecessary
  • 1,377
  • 1
  • 13
  • 19
-1

Try this way working with me

<activity
        android:name="yourActivity"
        android:configChanges="keyboardHidden|orientation|screenSize"/>
Biraj Zalavadia
  • 28,348
  • 10
  • 61
  • 77
  • Using the android:configChanges sounds like a good option although the Android API Guide states that _"This technique should be considered a last resort when you must avoid restarts due to a configuration change and is not recommended for most applications."_ source: [link](http://developer.android.com/guide/topics/resources/runtime-changes.html). If I use configChanges, does this mean that I need to implement a lot of extra code or should I leave the onConfigurationChanged() empty since I don't need to redraw anything? – Koningh Sep 23 '13 at 12:24
  • 1
    NO extra code need.This option will not reload the activity just refresh views.onConfigurationChanged() is not mandatory to override – Biraj Zalavadia Sep 23 '13 at 12:29
  • Just tried it and it works! No extra code needed indeed. I used the onConfigurationChanged example in the android API Guide to check how it works but my app still behaves normally once the overridden function is removed. Thanks! – Koningh Sep 23 '13 at 12:54
  • 1
    This is the WRONG solution and should not be used. This will only prevent activity recreation for the three flags shown (hardware keyboard hidden, orientation change, screen size change). – Kevin Coppock Sep 24 '13 at 20:23
  • If you just want to make something happen only on the first instance of onCreate(), just do a check for `savedInstanceState == null`. If it's null, it's the first creation. Otherwise, it's due to a configuration change. – Kevin Coppock Sep 24 '13 at 20:25
  • @kcoppock Are there more situations where the activity will be recreated other then those three? Otherwise I don't see why this solution is wrong. Besides that, your answer does sound like a good one as well! Will try that soon. – Koningh Sep 25 '13 at 10:15
  • @Koningh yup, there are. See this page: http://developer.android.com/guide/topics/manifest/activity-element.html. Every option under the configChanges attribute will cause a restart. – Kevin Coppock Sep 25 '13 at 16:26