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

前端 未结 2 916
深忆病人
深忆病人 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 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.

提交回复
热议问题