SettingsFragment中的选项不执行其任务

时间:2015-12-04 16:32:02

标签: java android

我有备份备注数据库的选项。我添加了在MainActivity中以onOptionsItemSelected方法备份注释的代码,它的工作正常。但是,当我将相同的代码复制到SettingsFragment活动并在我的手机中按下该选项时,它不起作用,并且不会显示任何内容!

另一个问题,我如何在SettingsFragment活动中显示Toast或Snackbar?!

提示:这是我第一次使用“设置”活动。所以,我不知道onPreferenceTreeClick方法是否是使用某些首选项的正确方法。

SettingsFragment活动:

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Environment;
import android.preference.CheckBoxPreference;
import android.preference.Preference;
import android.preference.PreferenceFragment;
import android.preference.PreferenceScreen;
import android.widget.Toast;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.channels.FileChannel;

public class SettingsFragment extends PreferenceFragment implements SharedPreferences.OnSharedPreferenceChangeListener {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.xml.preferences_layout);
}

@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
    PreferenceKeys preferenceKeys = new PreferenceKeys(getResources());

    if (key.equals(preferenceKeys.night_mode_pref_key)) {
        SharedPreferences themePrefs = getActivity().getSharedPreferences(MainActivity.THEME_PREFERENCES, Context.MODE_PRIVATE);
        SharedPreferences.Editor themeEditor = themePrefs.edit();
        themeEditor.putBoolean(MainActivity.RECREATE_ACTIVITY, true);

        CheckBoxPreference checkBoxPreference = (CheckBoxPreference) findPreference(preferenceKeys.night_mode_pref_key);

        if (checkBoxPreference.isChecked()) {
            themeEditor.putString(MainActivity.SAVED_THEME, MainActivity.DARKTHEME);
        } else {
            themeEditor.putString(MainActivity.SAVED_THEME, MainActivity.LIGHTTHEME);
        }
        themeEditor.apply();

        getActivity().recreate();
    }
}

@Override
public void onResume() {
    super.onResume();
    getPreferenceManager().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
}

@Override
public void onPause() {
    super.onPause();
    getPreferenceManager().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
}

@Override
public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
    PreferenceKeys preferenceKeys = new PreferenceKeys(getResources());

    if(preference.equals(preferenceKeys.backup_db_key))
    {
        try {
            File sd = Environment.getExternalStorageDirectory();
            File data = Environment.getDataDirectory();
            if (sd.canWrite()) {
                String currentDBPath = "//data//com.twitter.i_droidi.mynotesdonation//databases//MyNotes"; // Check...!!!
                String backupDBPath = "MyNotes"; // From SD directory.
                File backupDB = new File(data, currentDBPath);
                File currentDB = new File(sd, backupDBPath);

                FileChannel src = new FileInputStream(currentDB).getChannel();
                FileChannel dst = new FileOutputStream(backupDB).getChannel();
                dst.transferFrom(src, 0, src.size());
                src.close();
                dst.close();
                Toast.makeText(getContext(), "Import Successful!",
                        Toast.LENGTH_SHORT).show();

            }
        } catch (Exception e) {

            Toast.makeText(getContext(), "Import Failed!", Toast.LENGTH_SHORT)
                    .show();

        }
    }

    return super.onPreferenceTreeClick(preferenceScreen, preference);
}
}

0 个答案:

没有答案