I have an AppCompatActivity that has 3 tabs using FragmentTabHost. One of the tabs uses LocationServices. I would like to have the smoothest possible user experience:

If the LocationService is off in the android system, and only if the user chooses the tab that needs the Location I would like to display the AlertDialog to let the user turn on the Location in the system settings.

I have a helper class that is supposed to do all this and it does work in 3 other places in my app. In those 3 places it works "directly" in the Activity, however in this place it needs to work "within" the Fragment of the tab.

The problem is that if I have the line:

builder.enableAutoManage(activity, 0, this);

then builder.build() throws an exception: IllegalStateException: Recursive entry to executePendingTransactions

Any idea how can I achieve my goal?

Here are some related code fragments:

public class CityPreferences extends AppCompatActivity {
    private FragmentTabHost mTabHost;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
        mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
        mTabHost.addTab(
                mTabHost.newTabSpec("available_cities")
                        .setIndicator(getString(R.string.tab_all_cities))
                , AvailableCityFragment.class, null);
        mTabHost.addTab(
                mTabHost.newTabSpec("nearby_cities")
                        .setIndicator(getString(R.string.tab_nearby_cities))
                , NearbyCityFragment.class, null);
    }
}

In NearbyCityFragment I have this 1 line of code:

class NearbyCityFragment extends Fragment {
...
    LocationServiceHelper.getInstance().startOrDisplayDialog(getActivity());

(I tried it in onAttach, onStart, onResume)

And here's my helper class' function:

public class LocationServiceHelper implements
    GoogleApiClient.OnConnectionFailedListener,
    GoogleApiClient.ConnectionCallbacks {

public boolean startOrDisplayDialog(@NonNull final FragmentActivity activity) {
    final boolean servicesConnected = GooglePlayServicesHelper.checkOrDisplayDialog(activity);
    if (servicesConnected) {
        final boolean isEnabled = isLocationEnabledInSystem(activity);
        if (isEnabled) {
            if (null == mGoogleApiClient) {
                mContext = activity;
                mActivity = activity;
                final GoogleApiClient.Builder builder = new GoogleApiClient.Builder(mContext)
                        .addApi(LocationServices.API)
                        .addConnectionCallbacks(this)
                        .addOnConnectionFailedListener(this);

                // the next line seems to cause the problem:
                builder.enableAutoManage(activity, 0, this);

                mGoogleApiClient = builder
                        .build();
            }
            return start();
        } else {
            final Dialog dialog = getLocationDisabledDialog(activity);
            GooglePlayServicesHelper.showDialog(dialog, activity);
        }
    }
    return false;
}

And finally the exception:

06-10 10:23:04.831  26725-26725/com.fletech.android.redalert.debug E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.fletech.android.redalert.debug, PID: 26725
    java.lang.IllegalStateException: Recursive entry to executePendingTransactions
            at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1473)
            at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:490)
            at com.google.android.gms.common.api.g.a(Unknown Source)
            at com.google.android.gms.common.api.GoogleApiClient$Builder.gI(Unknown Source)
            at com.google.android.gms.common.api.GoogleApiClient$Builder.build(Unknown Source)
            at com.fletech.android.redalert.helper.LocationServiceHelper.startOrDisplayDialog(LocationServiceHelper.java:113)
            at com.fletech.android.redalert.city.NearbyCityFragment.onAttach(NearbyCityFragment.java:44)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:907)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1138)
            at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:740)
            at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1501)
            at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:458)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5257)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
1

There are 1 answers

0
Troy Perales On BEST ANSWER

I believe you must use a unique clientId each time you enable auto manager. From the documentation:

clientId - A non-negative identifier for this client. At any given time, only one auto-managed client is allowed per id. To reuse an id you must first call stopAutoManage(FragmentActivity) on the previous client.