android - SupportLifecycleFragmentImpl 在使用谷歌对话框启用位置时自动添加 fragment

标签 android android-fragments google-api location back-stack

SupportLifecycleFragmentImpl automatically add fragment when enable location using google dialog.

当调用位置时,当它显示打开位置的对话框时,它会自动添加 fragment 。调用 getFragmentManager().getFragments() 时,它会显示一个自动添加的带有标签 SupportLifecycleFragmentImpl 的新 fragment 。这会在后台堆栈中产生问题。

enter image description here

此标签添加

enter image description here

java代码

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 对象。当我使用 Activity 时:

geofencingClient = LocationServices.getGeofencingClient(activity); 

完全相同的 SupportLifecycleFragmentImpl 被添加到 fragment 返回堆栈中(尽管它在任何时候都不可见,因为我自己管理权限请求)。

当我切换到使用应用程序上下文初始化客户端时,问题就解决了:

geofencingClient = LocationServices.getGeofencingClient(getApplicationContext());

关于android - SupportLifecycleFragmentImpl 在使用谷歌对话框启用位置时自动添加 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59965704/

相关文章:

android - 如何在 ionic 中监听 Android 硬件后退按钮

android透明 Activity 在左右都有边距

Android networkinfo 始终返回 true,即使互联网不可用

ios - 尝试在我的应用程序中集成 Google 登录时找不到 "GoogleService-Info.plist"

android - 未调用android服务的onCreate

java - 我的 android 应用程序在我加载对话框时第二次崩溃

android - Proguard 和 getSimpleName()

java - 在 DialogFragment 的 onClick 方法中调用 MainActivity 的方法

java - Android 应用程序中的 Google 翻译 API

c# - 使用 C# 验证 Google id token