android - SupportLifecycleFragmentImpl 使用谷歌对话框启用位置时自动添加片段
问题描述
SupportLifecycleFragmentImpl
使用谷歌对话框启用位置时自动添加片段。
当调用位置并显示打开位置的对话框时,它会自动添加片段。当调用 getFragmentManager().getFragments() 时,它会显示一个它自动添加的带有标签 SupportLifecycleFragmentImpl 的新片段。这会在后台堆栈中产生问题。
这个标签添加
爪哇代码
public void enableLoc() {
activity.setFinishOnTouchOutside(true);
final int REQUEST_LOCATION = 199;
GoogleApiClient googleApiClient = new GoogleApiClient.Builder(activity)
.addApi(LocationServices.API)
.addConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() {
@Override
public void onConnected(Bundle bundle) {
Log.e("location", "Connect");
}
@Override
public void onConnectionSuspended(int i) {
Log.e("location", "fail");
//googleApiClient.connect();
}
})
.addOnConnectionFailedListener(new GoogleApiClient.OnConnectionFailedListener() {
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
Log.d("location", "Location error " + connectionResult.getErrorCode());
}
}).build();
googleApiClient.connect();
LocationRequest locationRequest = LocationRequest.create();
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
locationRequest.setInterval(30 * 1000);
locationRequest.setFastestInterval(5 * 1000);
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
.addLocationRequest(locationRequest);
builder.setAlwaysShow(true);
SettingsClient client = LocationServices.getSettingsClient(activity);
Task<LocationSettingsResponse> task = client.checkLocationSettings(builder.build());
task.addOnSuccessListener(activity, new OnSuccessListener<LocationSettingsResponse>() {
@Override
public void onSuccess(LocationSettingsResponse locationSettingsResponse) {
// All location settings are satisfied. The client can initialize
// location requests here.
// ...
Log.d("location_enable", "enable");
}
});
task.addOnFailureListener(activity, new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
if (e instanceof ResolvableApiException) {
// Location settings are not satisfied, but this can be fixed
// by showing the user a dialog.
try {
// Show the dialog by calling startResolutionForResult(),
// and check the result in onActivityResult().
ResolvableApiException resolvable = (ResolvableApiException) e;
resolvable.startResolutionForResult(activity,
REQUEST_LOCATION);
} catch (IntentSender.SendIntentException sendEx) {
// Ignore the error.
}
}
}
});
}
解决方案
我刚刚遇到了类似的问题 - 我尝试GeofencingClient
从位置服务初始化一个对象。当我使用活动时:
geofencingClient = LocationServices.getGeofencingClient(activity);
相同的SupportLifecycleFragmentImpl
内容被添加到片段返回堆栈中(尽管它在任何时候都不可见,因为我自己管理权限请求)。
当我切换到使用应用程序上下文初始化客户端时,问题就解决了:
geofencingClient = LocationServices.getGeofencingClient(getApplicationContext());
推荐阅读
- azure - 可以使用 Azure Kubernetes Service 或 Azure Service Fabric 来部署和管理本地 kubernetes 集群吗?
- asp.net-core - ASPNET CORE InvalidOperationException:无法重定向到授权端点,配置可能丢失或无效
- android - Android Studio API 29 及更高版本的模拟器立即崩溃
- javascript - 无法读取未定义 Javascript 的属性“拆分”
- flutter - Flutter中,如何在屏幕底部放置一个ListView
- terminal - 奇怪的终端提示
- c# - 是否有一种干净的方法可以从在 WSL 下运行的 dotnet 核心应用程序访问 AppData?
- apache-kafka - 需要从供应商的 Kafka 主题中将数据带回家/本地
- c# - 使用 Enterprise SingleSignon 页面和 Microsoft SignInManager 在 ASP.NET Core 中进行身份验证/授权
- javascript - 使用 id、来自 sqlite 的数据和验证实现自动完成