Search code examples
androidandroid-activityandroid-service

Reloading of data on screen rotation


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!


Solution

  • Try this way working with me

    <activity
            android:name="yourActivity"
            android:configChanges="keyboardHidden|orientation|screenSize"/>