I am using the Scrollable tabs + Swipe Navigation for my project. I used Shared Preferences to store data of a form. My onClick function for the save button of the page.
public void onClick(View v) {
SharedPreferences.Editor editor3 = settings3.edit();
editor3.putString("address", personal_info_address.getText().toString());
editor3.putString("age", personal_info_age.getText().toString());
editor3.putString("name", personal_info_name.getText().toString());
editor3.putString("diseases", personal_info_diseases.getText().toString());
editor3.commit();
}
I am unable to restore data after I reopen the application. I am able to save my form details in a normal app with the same code, do I need to do something else as I am using the Scrollable tabs + Swipe Navigation.
EDIT:
I implemented the clickListener in the Fragment Class, here is my code:
public class SettingsFragment extends Fragment {
public static EditText txtMessage;
public static EditText emergencyno;
Button button;
Context context;
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
context = getActivity();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.services, container, false);
emergencyno = (EditText) root.findViewById(R.id.settings_number);
txtMessage = (EditText) root.findViewById(R.id.settings_msg);
button = (Button) root.findViewById(R.id.save);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String msg = txtMessage.getText().toString();
String no = emergencyno.getText().toString();
Log.d("HELLO : ", "data1");
SharedPreferences sharedpreferences = context
.getSharedPreferences("MyData", Context.MODE_PRIVATE);
Log.d("HELLO : ", "data2" + context);
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString("message", msg);
editor.putString("number", no);
editor.commit();
Log.d("HELLO : ", "data3" + msg + no);
String message = sharedpreferences.getString("message",
"defValue");
String phone_no = sharedpreferences.getString("number",
"defValue");
txtMessage.setText(message);
emergencyno.setText(phone_no);
Log.d("HELLO : ", "data4" + message + phone_no);
}
});
return root;
}
}
The log gives the correct values which I require. But on restarting the app the data isnt stored. I need to know how to get the Context.
I was able to solve the problem. The getActivity() in this fragment returned the Main Page of my app where I created the fragments so I defined my sharedpreferences function in the Main Page of the app(i.e. in the activity of this Fragment) as follows:
public static void putPref(String key, String value, Context context) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = prefs.edit();
editor.putString(key, value);
editor.commit();
}
public static void putPref(String key, Boolean value, Context context) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = prefs.edit();
editor.putBoolean(key, value);
editor.commit();
}
public static String getPref(String key, Context context) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
return preferences.getString(key, null);
}
public static boolean getPrefBool(String key, Context context) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
return preferences.getBoolean(key, true);
}
And used the putPref function on clicking the button and getPref function on activity created.