ResultReceiver不survire到屏幕旋转 [英] ResultReceiver doesn't survire to screen rotation
问题描述
我在执行中的Android REST客户端。
我已经看到了使用服务
执行连接到服务器和 ResultReceiver
的实例的被通知操作完成。我打电话从一个片段的服务,如果我尝试旋转屏幕的服务运行时,在getActivity()方法 ResultReceiver
返回null,原因可能是片段不是在布局了。
I am implementing a REST client in Android.
I have seen an example of using a Service
to perform the connection to the server and the ResultReceiver
to be notified of the operation completion. I am calling the service from a fragment and, if I try to rotate the screen while the service is running, the getActivity() method in ResultReceiver
returns null because probably that fragment is not in layout anymore.
在片段中的回调方法:
@Override
public void onReceiveResult(int resultCode, Bundle resultData) {
Response response = (Response) resultData
.getSerializable(RestService.RESULT);
if (resultCode == RestService.SUCCESS
&& response != null) {
if (getActivity() != null) {
recommendationResponse = response;
getLoaderManager().restartLoader(0, new Bundle(),
Fragment.this);
}
}
}
在 getActivity()
返回null。
这正常吗?我可以用什么方法,让即使是在屏幕旋转通知?本地广播?
The getActivity()
returns null.
Is this normal? What approach could I use to allow notification even on screen rotation? Local Broadcast?
推荐答案
我使用的是的BroadcastReceiver
使用注册 LocalBroadcastManager
和它工作正常。这不是那么简单。有没有更好的解决办法存在?
I am using a BroadcastReceiver
registered using LocalBroadcastManager
and it is working properly. It wasn't so simple. Does a better solution exist?
这篇关于ResultReceiver不survire到屏幕旋转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!