How to Disable Crashlytics/Fabric at Runtime when User Changes Preferences

梦想与她 提交于 2019-12-07 03:13:21

问题


I am using com.crashlytics.sdk.android:crashlytics:2.3.2@aar version of crashlytics and i disable crash reporting if user opt out.

I tried this solution but still it is not working, crash reports are still being sent to Fabric.

I am doing it as:

Preference errorReportingEnabled = findPreference(MatlistanPrefs.BUGREPORTS_SEND_AUTOMATICALLY);
    errorReportingEnabled.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
        @Override
        public boolean onPreferenceChange(Preference preference, Object newValue) {

            Boolean value = (Boolean) newValue;

            Fabric.with(DataCollectionSettingsActivity.this, new Crashlytics.Builder().
                    core(new CrashlyticsCore.Builder().disabled(!value).build())
                    .build());
            return true;
        }
    });

Is there any working solution for this problem?

Thanks.


回答1:


Fabric on initialization creates a singleton instance and returns same instance whenever you call Fabric.with(...). So, your code inside onPreferenceChangehas no effect on Fabric class.

The only solution to this problem can be if library itself provide methods for enabling or disabling crashlytics. So, up-till now (crashlytics:2.5.2) there is no solution to enable/disable crashlytics at run-time. You have to do it at startup like this:

Fabric.with(this, new Crashlytics.Builder()
    .core(new CrashlyticsCore.Builder()
    .disabled(true).build()).build());


来源:https://stackoverflow.com/questions/32168754/how-to-disable-crashlytics-fabric-at-runtime-when-user-changes-preferences

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!