您可以通过在活动开始和停止之间保持某种状态来处理此问题。
请参阅此示例代码mIsResolving
中变量的使用。只需保存在被调用时是否已经存在待处理的对话框并在中恢复,如果是,请注意不要再次调用API,为intent接收到一次清除状态。onSaveInstanceState()
onCreate()
onActivityResult()
private void resolveResult(Status status, int requestCode) {
// We don't want to fire multiple resolutions at once since that can result
// in stacked dialogs after rotation or another similar event.
if (mIsResolving) {
Log.w(TAG, "resolveResult: already resolving.");
return;
}
if (status.hasResolution()) {
try {
status.startResolutionForResult(MainActivity.this, requestCode);
mIsResolving = true;
...
@Override
protected void onCreate(Bundle savedInstanceState) {
...
if (savedInstanceState != null) {
mIsResolving = savedInstanceState.getBoolean(KEY_IS_RESOLVING);
}
...
@Override
protected void onSaveInstanceState(Bundle outState) {
...
outState.putBoolean(KEY_IS_RESOLVING, mIsResolving);
...
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
...
mIsResolving = false;
...
这是许多应用程序的常见缺陷,因此我们将研究是否可以在 Play Services 层中支持此状态,但目前,为活动使用布尔值是维护分辨率状态的当前和一般建议。