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

前端 未结 2 912
深忆病人
深忆病人 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:27

    Here is a simpler way that I am using to get a reference to a Preference Fragment from a Preference Activity:

    private WeakReference<GeneralPreferenceFragment> mGeneralFragment;
    
    @Override
    public void onAttachFragment(Fragment fragment) {
        super.onAttachFragment(fragment);
    
        // "id" might be arbitrary and "tag" can be null
        Log.d(TAG, "onAttachFragment: "+fragment.getId()+","+fragment.getTag());
        // however class can be used to identify different fragments
        Log.d(TAG, "onAttachFragment: "+fragment.getClass()+","+(fragment instanceof GeneralPreferenceFragment));
    
        if(fragment instanceof GeneralPreferenceFragment) {
            mGeneralFragment=new WeakReference<>((GeneralPreferenceFragment)fragment);
        }
    }
    
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    
        Fragment f=mGeneralFragment.get();
    
        if(f!=null) {
            // can now use findPreference, etc
        }
    }
    

    This method is convenient because it doesn't require the use of interfaces or modifications to the fragment classes.

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题