简体   繁体   English

方向更改和与云端硬盘的连接

[英]Orientation changes and connection to Drive

I'm trying to develop an application for Android that fetches all the data from Google Drive. 我正在尝试开发适用于Android的应用程序,该应用程序将从Google云端硬盘中获取所有数据。 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. 我以为可以将连接/驱动器对象保存在onSaveInstanceState中,但找不到适合驱动器对象的方法。

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? 有没有办法存储它,还是我应该在设备布局更改时重新连接,例如在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. 但是,您可能不想在该活动中开始和停止从Google驱动器下载数据。 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. 请记住,将服务作为“ START_STICKY”启动,并在绑定到服务之前调用startService(...),以防止在取消绑定时关闭。

All you have to do is connect to the Service again on an orientation change, the network connection should be uncontested 您所要做的就是在方向改变时再次连接到服务,网络连接应该不受争议

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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