Search code examples
androidandroid-fragmentsandroid-preferences

PreferenceFragmentCompat not listening to first touch event


I created a PreferenceFragmentCompat settings everything is working fine but I noticed something odd, clicking on setting button first time when preference screen loaded and I touch on some preference it does not respond to my first touch but right after it works normally this is my code

public class SettingsFragment extends PreferenceFragmentCompat implements HomeActivity.Authentication, Preference.OnPreferenceClickListener, SharedPreferences.OnSharedPreferenceChangeListener, ConfirmAlertDialog.OnDialogClickInterface {
@Override
public boolean onPreferenceClick(final Preference preference) {
    final String key = preference.getKey();

    switch (key) {
        case "remove_ads":
            homeActivity.purchaseRemoveAds();
            break;
        case "hide_icon":
            try {
                homeActivity.loadFragment(new HideOptiosTutorial(), getParentFragment().getFragmentManager().beginTransaction());
            } catch (Exception e) {
                e.printStackTrace();
            }
            break;

        case "key_share_app_link":
            shareAppFile();

            break;

        case "move_to_sdcard":

            showMovetoSdDialog();

            break;
        case "button_internal":

            showMovetoInternalDialog();

            break;
        case "back_up_sdcard":
            if (isSDCardUnmounted()) {
                showSDCardMountError();
            } else {

                globalPasswords = getBackupPasswords();

                if (!FileUtilis.isSafAllowed(getActivity())) {
                    showSDCardNotAllowedDialog(983);
                    //
                    return false;
                }

                if (globalPasswords.size() == 0) {
                    showNoBackupFoundMsg();
                } else {
                    showDialogToBackupFromSDCard();
                }
            }
            break;
        case "change_password":
            showChangePinDialog();
            break;

        case "key_rate_us":
            rateApplication();
            break;
        case "app_link":
            SavedAlbumsFragment.showAppLockLink(getActivity());
            break;
        case "view_intruders":
            startActivity(new Intent(homeActivity, ViewIntruders.class));
            break;
        case "theme":
            showSelectTehmeDialog();
            break;

        case "enter_email":

            showChangeEmailDialog();
            break;
    }


    return false;
}
@Override
public void onCreatePreferences(Bundle bundle, String s) {
    addPreferencesFromResource(R.xml.pref_headers);
    Log.e("Mateen","onCreatePreferences");

    homeActivity = ((HomeActivity) getActivity());
    homeActivity.addAuthenticationListner(this);
    localDatabase = LocalDatabase.getInstance(getActivity());
    localPreferences = new LocalPreferences(getActivity());

    getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
    LocalBroadcastManager.getInstance(homeActivity).registerReceiver(broadcastReceiver,new IntentFilter("Remove_Is_Purchased"));

    removeAdsPref=findPreference("remove_ads");
    mCategory = (PreferenceCategory)findPreference("cat_remove_ads");



    if (localPreferences.IsRemovedAdsPurchased())
    {
        mCategory.removePreference(removeAdsPref);
        getPreferenceScreen().removePreference(mCategory);
    }
    else
    {
        removeAdsPref.setOnPreferenceClickListener(this);
    }

    findPreference("move_to_sdcard").setOnPreferenceClickListener(this);
    findPreference("view_intruders").setOnPreferenceClickListener(this);
    findPreference("button_internal").setOnPreferenceClickListener(this);
    findPreference("back_up_sdcard").setOnPreferenceClickListener(this);
    findPreference("change_password").setOnPreferenceClickListener(this);
    findPreference("key_rate_us").setOnPreferenceClickListener(this);
    findPreference("app_link").setOnPreferenceClickListener(this);
    findPreference("hide_icon").setOnPreferenceClickListener(this);
    findPreference("key_share_app_link").setOnPreferenceClickListener(this);
    findPreference("listPref_wrong_tries").setOnPreferenceClickListener(this);
    findPreference("theme").setOnPreferenceClickListener(this);
    findPreference("enter_email").setOnPreferenceClickListener(this);




    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        findPreference("key_cam_short").setVisible(false);
        findPreference("key_vid_short").setVisible(false);
    }

    if (!PreferenceManager.getDefaultSharedPreferences(homeActivity).getBoolean("key_intruder_detection",false))
    {
        findPreference("listPref_wrong_tries").setEnabled(false);
    }
    if (!Reprint.isHardwarePresent()) {
        findPreference("key_fingerprint").setEnabled(false);
    }

    if (!FileUtilis.isSdcardPresent(getActivity())) {
        findPreference("move_to_sdcard").setEnabled(false);
        findPreference("button_internal").setEnabled(false);
        findPreference("back_up_sdcard").setEnabled(false);

        //  findPreference("change_password").setEnabled(false);
        findPreference("always_save_to_sdcard").setEnabled(false);
    }
    //add xml

}

and this is my preference xml:

<android.support.v7.preference.PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.andr]oid.com/apk/res-auto"
app:key="preferenceScreen">

<!-- These settings headers are only used on tablets. -->z
<android.support.v7.preference.PreferenceCategory  android:title="@string/cate_remove_ads"
    android:key="cat_remove_ads">
    <Preference
        android:key="remove_ads"
        android:summary="@string/txt_remove_ads"
        android:title="@string/remove_ads" />
</android.support.v7.preference.PreferenceCategory>

<android.support.v7.preference.PreferenceCategory android:title="@string/storage">

    <Preference
        android:key="move_to_sdcard"
        android:summary="@string/export_sdcard_summary"
        android:title="@string/txt_move_to_external" />

    <Preference
        android:key="button_internal"
        android:summary="@string/export_internal_summary"
        android:title="@string/txt_move_too_internal" />

    <Preference
        android:key="back_up_sdcard"
        android:summary="@string/backup_summary"
        android:title="@string/back_from_sdcard" />

    <android.support.v7.preference.SwitchPreferenceCompat
        android:defaultValue="false"
        android:key="always_save_to_sdcard"
        android:summary="@string/always_on_sd_card"
        android:title="@string/sav_loc" />

</android.support.v7.preference.PreferenceCategory>


<android.support.v7.preference.PreferenceCategory android:title="@string/cate_security">


    <Preference
        android:key="hide_icon"
        android:title="@string/hide_icon" />


    <Preference
        android:key="app_link"
        android:summary="@string/app_link_title"
        android:title="@string/app_link_detail" />

    <Preference
        android:key="change_password"
        android:title="@string/chagne_password" />

    <Preference
        android:key="enter_email"
        android:title="@string/forgot_email_title"
        android:summary="@string/enter_email_to_recover" />


    <android.support.v7.preference.SwitchPreferenceCompat
        android:defaultValue="false"
        android:key="shuffle_pin"
        android:summary="@string/shuffle_summary"
        android:title="@string/shuffle_title" />

    <android.support.v7.preference.SwitchPreferenceCompat
        android:defaultValue="false"
        android:key="key_fingerprint"
        android:summary="@string/summary_fingerprint"
        android:title="@string/fingerprint_auth" />
</android.support.v7.preference.PreferenceCategory>

<android.support.v7.preference.PreferenceCategory android:title="@string/cate_intruder">
    <android.support.v7.preference.SwitchPreferenceCompat
        android:defaultValue="false"
        android:key="key_intruder_detection"
        android:summary="@string/take_intruder_picture_summary"
        android:title="@string/take_intruder_picture" />

    <android.support.v7.preference.ListPreference
        android:defaultValue="3"
        android:entries="@array/wrong_tries"
        android:entryValues="@array/wrong_tries"
        android:summary="%s"
        android:title="@string/wrong_tries"
        android:key="listPref_wrong_tries" />
    <Preference
        android:key="view_intruders"
        android:summary="@string/show_intruder_pic"
        android:title="@string/show_intruder_pic_summary" />
</android.support.v7.preference.PreferenceCategory>

<android.support.v7.preference.PreferenceCategory android:title="@string/cate_others">
    <Preference
        android:key="theme"
        android:summary="@string/chose_theme"
        android:title="@string/select_theme" />


    <android.support.v7.preference.SwitchPreferenceCompat
        android:defaultValue="false"
        android:key="key_cam_short"
        android:summary="@string/cam_short_detail"
        android:title="@string/sec_selfi_shortcut" />

    <android.support.v7.preference.SwitchPreferenceCompat
        android:defaultValue="false"
        android:key="key_vid_short"
        android:summary="@string/vid_short_detail"
        android:title="@string/sec_vid_shortcut" />

    <Preference
        android:key="key_share_app_link"
        android:summary="@string/share_app_link_detail"
        android:title="@string/share_app_link_title" />

    <Preference
        android:key="key_rate_us"
        android:summary="@string/rate_detail"
        android:title="@string/rate_us" />
</android.support.v7.preference.PreferenceCategory>

Please guide me what am I doing wrong? Is it support libs issue or I am doing something wrong?


Solution

  • i solved my problem by removing this line from my fragment layout's xml

            app:layout_scrollFlags="scroll|enterAlways"
    

    from:

      <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        app:layout_scrollFlags="scroll|enterAlways"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
    

    now it is listening to my every damn touch :D Happy coding