Fragment中的.commit()或.apply()之后不保存SharedPreferences

时间:2015-11-30 22:23:44

标签: java android sharedpreferences

Framgent Class

public class FragmentSignUp1 extends Fragment {

    private EditText editTextEmail;
    private EditText editTextUsername;

    UserSignUpPreferences userSignUpPreferences;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_signup1, container, false);
        userSignUpPreferences = new UserSignUpPreferences(this.getActivity());

        //  Creating references to Views
        editTextEmail = (EditText) view.findViewById(R.id.editTextEmail);
        editTextUsername = (EditText) view.findViewById(R.id.editTextUsername);

        // Setting custom font on elements
        return view;
    }

//  Restore data if activity has been destroyed
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        if (savedInstanceState == null) {

        } else {
            editTextEmail.setText(savedInstanceState.getString("email"));
            editTextUsername.setText(savedInstanceState.getString("username"));
        }
    }

//  Save data if activity is destroyed
    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putString("email", editTextEmail.getText().toString());
        outState.putString("username", editTextUsername.getText().toString());
    }

//  Save data if activity get paused
    @Override
    public void onPause() {
        super.onPause();
        Log.d("fragment_signup1", editTextEmail.getText().toString());
        userSignUpPreferences.editor.putString(editTextEmail.getText().toString(), userSignUpPreferences.USER_EMAIL)
        .putString(editTextUsername.getText().toString(), userSignUpPreferences.USER_USERNAME)
        .apply();
        Log.d("fragment_signup1", userSignUpPreferences.preferences.getString(userSignUpPreferences.USER_EMAIL, "null"));
    }

//  Retrieve data when activity is resumed
    @Override
    public void onResume() {
        super.onResume();
        Log.d("fragment_signup1", userSignUpPreferences.preferences.getString(userSignUpPreferences.USER_EMAIL, "null"));
        editTextEmail.setText(userSignUpPreferences.preferences.getString(userSignUpPreferences.USER_EMAIL, "2"));
        editTextUsername.setText(userSignUpPreferences.preferences.getString(userSignUpPreferences.USER_USERNAME, ""));
    }
}

UserPreferences类

public class UserPreferences {

    public SharedPreferences.Editor editor;
    public SharedPreferences preferences;

    public UserPreferences(Context context, String prefFileName) {
        preferences = context.getSharedPreferences(prefFileName, Context.MODE_PRIVATE);
        editor = preferences.edit();
    }
}

UserSignUpPreferences类

public class UserSignUpPreferences extends  UserPreferences{

    private final static String USER_SEARCH_SETTINGS = "UserSearchSettingsPrefFile";
    public final String USER_EMAIL = "email";
    public final String USER_USERNAME = "username";

    public UserSignUpPreferences(Context context) {
        super(context, USER_SEARCH_SETTINGS);
    }
}

所以问题是......它没有保存当前值..为什么?

1 个答案:

答案 0 :(得分:0)

应用值时,参数会反转。 putString()的第一个参数是用于查找的键,第二个参数是与键关联的值。

在这里反转它们应该有效:

// SELECT * FROM Sections where Fk == 0;
sectionsList = new RushSearch().whereEqual("Fk", 0).find(Sections.class);
Log.d("ELEMENTI",""+sectionsList.size());
drawerListViewAdapter = new DrawerListViewAdapter(sectionsList, getApplicationContext());

// Adapter che si occupa di popolare il drawer
drawerListView.setAdapter(drawerListViewAdapter);