简体   繁体   中英

Orientation changes and connection to Drive

I'm trying to develop an application for Android that fetches all the data from Google Drive. I've been struggling quite long with the device orientation changes and having to reconnect to Drive. I thought I could save the connection/Drive object in onSaveInstanceState, but I couldn't find a method that would qualify a Drive object.

So how should I ideally proceed with this? Is there a way to store it or should I just reconnect on device layout changes, for example in onRestoreInstanceState?

Thank you for your time.

You might want to put the connection inside a retained fragment so that whatever inside could survive orientation change. This blog post could help you. However, you might not want to start and stop downloading data from google drive within that activity. You probably should put it in a service.

Have a look at Services and Bound Services

Run your code in there and bind your Activity to the Service. Remember to start your Service as "START_STICKY" and call startService(...) before binding to it to prevent a shutdown on Unbind.

All you have to do is connect to the Service again on an orientation change, the network connection should be uncontested

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM