PreferenceFragment doesn't get onActivityResult call from in app billing request

前端 未结 2 913
深忆病人
深忆病人 2021-01-24 19:15

I have a preference screen that presents the user with a checkbox to disable ads. When the user clicks this for the first time, they are presented with an In App Billing purcha

2条回答
  •  温柔的废话
    2021-01-24 19:39

    The original start request must come from the Fragment in order for Android to deliver the result back to the same Fragment. If the Activity starts the request, the result doesn't inherently get handed to every Fragment that may be attached to the manager. In addition to this, you have to ensure that if onActivityResult() is overridden in the top-level Activity, that you call super.onActivityResult() as well, or the message won't get delivered to the Fragment.

    The problem with IAB is your given a PendingIntent instead of a standard Intent to fire, and there is no method on Fragment to trigger the initial action even if you can move your code there. To keep things as they are, you may have to do a bit of a swizzle. One thing you could do is make use of a custom interface inside the onAttach() method of your Fragment and use it to allow the Fragment to hand itself up to the Activity. Something like:

    public class SyncPreferencesActivity extends PreferenceActivity
            implements SyncPreferencesFragment.OnAttachCallback {
        SyncPreferencesFragment mTargetFragment;
    
        @Override
        public void onAttachSyncFragment(SyncPreferencesFragment fragment) {
            mTargetFragment = fragment;
        }
    }
    

    ...with some additions to the corresponding Fragment...

    public class SyncPreferencesFragment extends PreferenceFragment {
        public interface OnAttachCallback {
            public void onAttachSyncFragment(SyncPreferencesFragment fragment);
        }
    
        @Override
        public void onAttach(Activity activity) {
            try {
                OnAttachCallback callback = (OnAttachCallback) activity;
                callback.onAttachSyncFragment(this);
            } catch (ClassCastException e) {
                throw new ClassCastException("You Forgot to Implement the Callback!");
            }
        }
    }
    

    With something like this, at least you have a reference to the instance so you can forward the result or anything else that might be necessary. If you want to be super clean, you could also implement a matching "detach" that clears the reference in the Activity.

提交回复
热议问题