Say ActivityA with two buttons. Each of these button will open ActivityB but with a different fragment respectively. Both fragment contain an EditText. If I want to switch fragment while in ActivityB, I need to return to ActivityA and press the other button.
Now what I want to do is to save the value entered in each EditText when I switch fragment or close the app and repopulate the value in the right EditText when I re-open the fragment.
It seems to do it by it self when I open the SettingActivity then come back, but not if I destroy the activity. In the end I want the fragment to re-open just as I left it. Thank you.
You must save value and then restore them.A good way is Shared Preferences.
1-save:
SharedPreferences sharedpreferences = getSharedPreferences("mypref", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString("var1", edittext.getText().toString());
editor.commit();
2-restore:
String s = sharedpreferences.getString("var1","DEF");
Use SharedPreference to store the value
When you call your new activity use this
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
SharedPreferences.Editor editor = settings.edit();
editor.putString("userEmail", youredittext.getText().toString());
editor.commit();
When you want to retrieve the stored value in any of your activities, get it like this
SharedPreferences getPrefs = PreferenceManager
.getDefaultSharedPreferences(getBaseContext());
String mUserEmail = getPrefs.getString("userEmail", null);
Yes if you have more fields or like you want to maintain relation for data.. in that case you also can use SQLite. Below is post on SQLite
http://www.kpblogs.com/mobile-development/sqlite-android-tutorial-with-crud-operations/
Related
Hello I am developing a simple project with android studio and I wanted to know how can I change the starting activity of the application via Java Code. I know how to do it with the androidmanifest.xml but I want the user to insert some data in the starting activity and then the next time the user loads the application the main activity pops up directly and not the starting activity again.
Thank you.
Use shared preference, with this you can save limited information. depending on what your user has selected you can save it and start an activity from it. the information remains even after the application is closed.
e.g. so you create a start activity that checks what settings have been selected and depending on that it starts the required activity with an intent.
search for shared preference and intent.
You should save a flag in Shared preference. For example in a first activity that launch save this:
SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit();
editor.putString("name", "isNew");
editor.apply();
And when you want to Retrieve data from preference:
SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE);
String name = prefs.getString("name", "default value");//"default value defined" is the default value.
Then Check if (name == isNew) then start your MainActivity class
I have Activity for ListPreference and i can change any values in it , but my question i want to change a value in ListPreference first time without the user enters to activity that there is ListPreference .
First time a user enters to an app i want to save default value depend on location the user.
I found the solution
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(getString(R.string.settings_method_key),repear);
editor.commit();
I'm using the library android material app rating (https://github.com/stepstone-tech/android-material-app-rating)
and I don't know how to save in SharedPreferences that the user has already done the review or rating.
I can save information in SharedPreferences if it's a TextView, but in this case I have no idea what to do.
Can you help me?
Example of how to save the info whether the user has rated you app:
SharedPreferences sharedPreferences = getSharedPreferences("MyShared", MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean("HasUserRating", true);
editor.apply();
Read back from SharedPreferences that the user has or has not rated:
SharedPreferences sharedPreferences = getSharedPreferences("MyShared", MODE_PRIVATE);
boolean hasRated = sharedPreferences.getBoolean("HasUserRating", false);
(Note: This will return false if the SharedPreferences key "HasUserRating" is not present.)
Use the callbacks within RatingDialogListener to save a value to SharedPreferences. Check for the existence of that SharedPreferences value prior to displaying the dialog.
I have many buttons in a page, now I what want do is, every account can select one button, when they select that button, that button will be disabled and when account is logged out, open application again ,the button should be still disabled. If anyone know the answer, please kindly help me.
Thanks.
I am using android studio and my login and register using SQLite.
When you click logout button, please save a variable to SharePreference:
SharedPreferences sharedpreferences = getSharedPreferences("Name", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putBoolean("isLogout", true);
editor.commit();
Now application will keep variable isLogout = true.
Next time when come to that screen, you just need to get this variable to check:
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
\\the true which is second parameter is default value that you want if isLogout variable is null
if( prefs.getBoolean("isLogout", true)){
button.disable
};
You should use Shared Preferences in the application for storing button visibility value. When the button is clicked then give your preference some value and when logging out of the application simply clear that shared preference value so that button will not be disabled any more.
Put this code inside click listener of button
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences((MY_PREFS_NAME, MODE_PRIVATE).edit();
SharedPreferences.Editor editor = preferences.edit();
editor.putString("value","buttondisabled");
editor.apply();
Put this code in the onCreate method of your activity
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
String name = preferences.getString("value", "");
if (value.equals("buttondisabled")
{
yourbutton.setVisibilty(View.VISIBLE)
}
and when logging out of the application simply write these lines:
Editor editor = getSharedPreferences("MY_PREFS_NAME", Context.MODE_PRIVATE).edit();
editor.clear();
editor.commit();
Thats it :)
In MainActivity.java I write
SharedPreferences pref = getApplicationContext().getSharedPreferences("My_Pref" , 0);
Then I create object of Editor to put data in it
Editor edit = pref.edit();
Then I put data
edit.putString("1","Hello");
edit.commit(); / edit.apply();
In Second.java, I get preferences:
SharedPreferences pref = getPreferences(0);
then I try to receive data like
pref.getString("1",null);
and set it to text of textview. But this does not work.
Also, how do I access Preferences and editor in other java classes properly? I cannot understand the concept.
You are writing to and reading from different preference files. Use the same file and it should work.
To get an instance of SharedPreferences you do this:
1) In activity MainActivity:
SharedPreferences pref =
getApplicationContext().getSharedPreferences("My_Pref" , 0);
2) In activitySecond:
SharedPreferences pref =
getPreferences(0);
The first form opens preference file "My_Pref", the second form opens a file named after your activity class, i.e.: "Second". So they are reading and writing in different files.
I always use this form to open a preferences file:
SharedPreferences prefs =
PreferenceManager.getDefaultSharedPreferences(this);
Try this way
public void saveToSharedPrefrence(Context context, String word) {
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
Editor editor = sharedPreferences.edit();
if (sharedPreferences.contains("history")) {
preExistRemove(word, context);
} else {
editor.putString("history", word.trim());
editor.commit();
}
}
Check this for more details
Get the context of your activity
Context shrdContext= ActivityClass.getContextOfApplication();
Now pass the context to get your shared preferences in your another class
SharedPreferences myPrefs= PreferenceManager.getDefaultSharedPreferences(shrdContext);
You have 3 ways to access preferences for a Android application.
The first you used is
SharedPreferences pref = getApplicationContext().getSharedPreferences("My_Pref" , 0); is the first one. With this you can read and write to a custom-named shared preference file. In your case the name of your file would be My_Pref.
This one is useful if you want to have differents preferences differents domain as it allow you to create many shared preferences with differents names. (ex : preferences for timezone, preference for your user).
The second getPreferences(int) allow to access preferences for an Activity and is bind closely to the activity calling it. The file created is named using the Activity name. In your case Second.
The third method PreferenceManager.getDefaultSharedPreferences(Context) create a shared preference file like the first method, but this time the file is named using your application package name. This is the best methods to use shared preferences, if you intend to have only one shared preference file.
In your initial question you wrote a data in one file and tried to read in an other file, which result in an error. That's why like Rob Meeuwisse write you must use PreferenceManager.getDefaultSharedPreferences(Context)