Save The value of Variable in Java/Android [duplicate] - java

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Making data persistent in android
Edited:
I am building an android app which works on the principle of a simple counter.
public class TasbeehActivity extends Activity {
/** Called when the activity is first created. */
int count;
ImageButton imButton;
TextView display;
static String ref = "Myfile";
static String key = "key";
SharedPreferences myPrefs;
boolean check = false;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
imButton = (ImageButton) findViewById(R.id.bCount);
display = (TextView) findViewById(R.id.tvDisplay);
myPrefs = getSharedPreferences(ref, 0);
if(check){
String dataReturned = myPrefs.getString(key, "0");
count = Integer.parseInt(dataReturned);
display.setText(""+count);
}
imButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Random rand = new Random();
display.setTextColor(Color.rgb(rand.nextInt(255),
rand.nextInt(255), rand.nextInt(255)));
count++;
display.setText("" + count);
}
});
}
#Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
String data = display.getText().toString(); SharedPreferences.Editor
editor = myPrefs.edit(); editor.putString(key, data);
editor.commit();
check = true;
}}
I want to save the value of count so that when my app restarts after closing it should have the value of count before closing app.
Also when I change orientation of the emulator i.e. from portrait to landscape or vice versa count gets set to 0.
Is there any solution for both of these problems?

You should use SharedPreferences to save your variable, and Override OnConfigurationChange because it's probably calling your oncreate method (don't forget to add android:configChanges="orientation|keyboardHidden|screenSize" in your manifest)
I Hope this will help you.

1. use sharedpreferences for saving or retrieving value of count when app is restarts after closing
2. see handle orientation change in android count gets set to 0 when I change orientation of the emulator i.e. from portrait to landscape or vice versa

Try these....
android:configChanges="orientation" , This will prevent your Activity to again restart when orientation changes.
Use Bundle, but thats only for small amount of data. Serialization and Deserialization will be needed.
You can also use onRetainNonConfigurationInstance(), and getLastNonConfigurationInstance(), for storing and retrieving data.
See this link for further details:
http://developer.android.com/guide/topics/resources/runtime-changes.html

Related

SharedPreferences If/Else Statement does not work correctly when I force close app

Summary of problem: When I force close the app by opening task window and swiping it closed my if/else statement is not working properly. I have the Name Selection Activity as the Default Launcher Activity. The name selection Activity should only pop up if there are no Shared Prefs, meaning the user has not selected a name yet from the spinner. But even after the user has selected a name and it is stored in Share Prefs, when I force close the app I still get returned to the name selection activity
What I have tried I have tried if (stringNamePackage.equals("")) and if (stringNamePackage == "") and if (NAME.equals("")) and if (NAME == "") At first I thought it was because maybe my Shared Prefs was not saving the name correctly but it is not that, the name still appears correctly when I force close it. But when I try to add the if/else statment it always just sends me back to the name selection activity regardless if I have a name saved in Shared Prefs or not. I have spent 4 hours trying to get this to work and I am at my wits end. I also spent hours looking at different stack articles of how to save and retrieve Shared Pref data. I even tried how to check the SharedPreferences string is empty or null *android and it is still not working.
NameSelectionActivity
public class NameSelection extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
Spinner nameSpinner;
String stringNamePackage;
Button bSaveSelection;
Context context;
public ArrayAdapter<CharSequence> adapter;
public static String SHARED_PREFS = "sharedPrefs";
public static String NAME = "name";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_name_selection);
context = this;
nameSpinner = findViewById(R.id.horizonNameSpinner);
stringNamePackage = "";
//Create the list to populate the spinner
List<String> nameList = new ArrayList<>();
nameList.add("01");
nameList.add("02");
nameList.add("03");
//Array Adapter for creating list
//adapter = ArrayAdapter.createFromResource(this, R.array., android.R.layout.simple_spinner_item);
adapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, nameList);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
nameSpinner.setAdapter(adapter);
nameSpinner.setOnItemSelectedListener(this);
//If User has not selected name, start this activity to allow user to pick Name, else, send user to Main Activity
//else start MainActivity
if (stringNamePackage .equals("")) {
Toast.makeText(this, "Welcome, please select Name", Toast.LENGTH_LONG).show();
} else {
Intent sendToMainActIntent = new Intent(this, MainActivity.class);
startActivity(sendToMainActIntent);
}
//Save Selection Button Code
bSaveSelection = findViewById(R.id.bSaveSelection);
bSaveSelection.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(), MainActivity.class);
//Saves name to SharedPrefs
saveName();
//Sends user to MainActivity
startActivity(myIntent);
finish();
}
});
}
//Stores name selected in SharedPreferences
public void saveName() {
SharedPreferences app_preferences = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = app_preferences.edit();
editor.clear();
//Stores selection in stringNamePackage
editor.putString(NAME, stringNamePackage );
editor.commit();
}
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
//Put user selection into String text
String text = adapterView.getItemAtPosition(i).toString();
stringNamePackage = text;
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
}
Main Activity
The purpose of this activity case is to send the user back to the name Selection screen when they hit the "Test" button which will erase the shared prefs and hence, trigger the if/else statement and making the user pick a name. This is how I get and set the Name in Main Activity:
in onCreate
Getting name
SharedPreferences app_preferences = PreferenceManager.getDefaultSharedPreferences(context);
nameSharedPref = app_preferences.getString(NAME, "");
name.setText(nameSharedPref);
Clearing Name
SharedPreferences mPrefs = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = mPrefs.edit();
editor.clear();
editor.commit();
finish();
What I expect: I expect the user to have to pick a name the first time they boot up the app. Then, every time they open the app an if/else statement will check if the user has a name or not in Shared Prefs. If they have a name they will go directly to the Main Activity. If they don't then they will go back to the Name Selection Activity.
To get data and store data to your SharedPreferences you use this:
SharedPreferences sharedPreferences = getSharedPreferences(getPackageName(), Context.MODE_PRIVATE);
PreferenceManager.getDefaultSharedPreferences(context) is used to build "Settings" screens or similar, the first one you use it to store/retrieve arbitrary data not necessarily binded to UI.
Even more if you want to organize your SharedPreferences you could append to getPackageName() different keys like:
getSharedPreferences(getPackageName() + ".booleans", Context.MODE_PRIVATE);
getSharedPreferences(getPackageName() + ".flags", Context.MODE_PRIVATE);
getSharedPreferences(getPackageName() + ".keys", Context.MODE_PRIVATE);
each one of them is different file that stores shared preferences, you can ommit the prepending dot ., but for naming consistency it'll be better to keep it, there is no really need for the extra keys, but if you have some sort of OCD, that might be "relaxing" ;-).
Then you can use your Android Studio's Device Explorer to browse the shared preferences, they are located under "data/data/your.package.name/shared_prefs"

Strange unexplained behaviour in SharedPreferences Android

I'm not new to sharedPreferences, and I decided to use them to store the titles of courses in which users create, and then are loaded into a listView. The course titles are stored in a set which is then put into the sharedPreference. The issue here is when i input the first course(as a user), it gets saved persistently, but adding more courses also saves as well, and works while the app is active, but on app restart, the sharedPreference returns back to the initial value of only one item in the set.
Naturally, i looked up the issue, and was able to solve it by using editor.clear() which i got from this reference https://looksok.wordpress.com/2012/09/21/sharedpreferences-not-saved-after-app-restart/
Notice that the solution was described as "the tricky one". I really want to know what this line meant. I've checked on the issue, but still can't understand why this case is so different, and such abnormalities scare me whenever i use those classes. So here's my code:
// Shared Preferences
SharedPreferences customPrefs;
// Editor for Shared preferences
SharedPreferences.Editor editor;
private CustomCourseDBHelper customDBHelper;
private final static int ADD_CUSTOM_COURSE_REQUEST = 1;
private final static int EDIT_CUSTOM_COURSE_REQUEST = 2;
private final static String TITLE_PREFS = "customCourseTitles";
private final static String TITLE_PREF_LIST = "customCourseList";
private ListView cListView;
private Set<String> allTitles = new LinkedHashSet<>();
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.custom_course_list);
mToolbar = (Toolbar) findViewById(R.id.tool_bar_shadow);
cListView = (ListView) findViewById(R.id.customQuestionList);
addButton = (FloatingActionButton) findViewById(R.id.addButton);
cAdapter = new CustomCourseAdapter(this);
setSupportActionBar(mToolbar);
cListView.setAdapter(cAdapter);
//declare shared prefs
customPrefs = getSharedPreferences(TITLE_PREFS, MODE_PRIVATE);
//set on long press options
optionsList.add("Delete Course");
//set icon for add button
addButton.setImageDrawable(getResources()
.getDrawable(R.drawable.ic_add_white_24dp));
addButton.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
Intent i = new Intent(CustomCourseList.this, AddCustomCourseActivity.class);
startActivityForResult(i, ADD_CUSTOM_COURSE_REQUEST);
}
});
}
And here's where the saving occurs
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
if (resultCode == RESULT_OK && requestCode == ADD_CUSTOM_COURSE_REQUEST){
CustomCourseItem cci = new CustomCourseItem(data);
cAdapter.add(cci);
//save the provided details in a database
customDBHelper = new CustomCourseDBHelper(this, cci.getPath());
customDBHelper.addCustomCourse(cci);
editor = customPrefs.edit();
Set<String> mSet = customPrefs.getStringSet(TITLE_PREF_LIST, null);
if (mSet != null)
allTitles = mSet;
//store the given database names in a set in a shared preference
allTitles.add(cci.getPath());
editor.clear(); //this line here, without it, the sharedPref is not persistent,
//i didn't see any clear reason as to why this occurs, when i read the documentary
editor.putStringSet(TITLE_PREF_LIST, allTitles);
editor.commit();
}
//listen request for edits made to courses
if (resultCode == RESULT_OK && requestCode == EDIT_CUSTOM_COURSE_REQUEST){
if (data.getBooleanExtra("edited", false)){
restructureDbOnEdit();
}
}
}
PS. i omitted some unnecessary code like UI implementations and other non-sharedPreference related stuff.
So even though the app runs well now, any further insight on this issue would be really welcomed.
Well I finally found what went wrong after doing a proper search, check out the answer provided here. Note that this is a bug in android and happens to string sets only. Thanks
Set<String> in android sharedpreferences does not save on force close

How do i store a textview using Shared Preferences

So my question might have been asked many times but i couldnt find an answer anywhere on the internet to it .
. What i want to do is store a textview using sharedprefeences .
In my first class (xp) i,m sending the textview to another class (feedback)
Now the feedback is reciving the textview with no single problem , but never saves it. How can i store that textview in the (feedback) class even after closing the app ??
Here's the class which is intenting the textview
public class Xp extends Activity {
Button accept;
TextView textV;
TextView xbnum;
public static final String PREFS_NAME = "MyPrefsFile";
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.xp);
accept = (Button) findViewById(R.id.accept);
textV = (TextView) findViewById(R.id.textV);
xbnum = (TextView) findViewById(R.id.xpnum);
Bundle extras = getIntent().getExtras();
if (extras != null) {
String value1 = extras.getString("intent_xp");
final String value = extras.getString("intent_extra");
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor edit = settings.edit();
edit.putString(PREFS_NAME, value);
edit.putString(PREFS_NAME,value1);
textV.setText(value);
xbnum.setText(value1);
edit.commit();
accept.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(Xp.this, feedback.class);
i.putExtra("intent_extra", textV.getText().toString());
startActivity(i);
finish();
}
});
}
}
}
And here is the class which will recive the intent and save the textview (As supposed )
public class feedback extends Activity {
TextView test1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.feedback);
test1 = (TextView) findViewById(R.id.test1);
Bundle extras = getIntent().getExtras();
if (extras != null) {
String value = extras.getString("intent_extra");
SharedPreferences settings = getSharedPreferences("intent_pref", 0);
SharedPreferences.Editor edit = settings.edit();
edit.putString("intent_pref",value);
test1.setText(value);
edit.apply();
}
}
}
The class is reciving the text , and everything is okay . Only when i close the app and open it , everything is cleared out ..
May be I'm wrong but it seems that you missunderstood how shared preferences works.
If you want to store several values in a preferences file an access it in others classes you have to create one instance of a SharedPreferences.
In your case it means create in both "feedback" and "Xp" a SharedPreferences instance like this :
SharedPreferences settings = getSharedPreferences("MyPrefsFile", SharedPreferences.MODE_PRIVATE);
and then use this instance to store your datas with an editor.
Be careful if you can't store several value for the same key.
You can also store your value in several files as you are actually doing. But if you want to set your textview with the value of your preferences file you have to get it with the getString("key_of_your_value") method on your shared preferences instance.

Strange behavior when reading from SharedPreferences

when I'm launching my application and the onResume() method is being run, something goes wrong when reading from my SharedPreferences. This is how the code looks.
static double cowCount = 197, income, cowMult = 1;
...
protected void onResume() {
super.onResume();
SharedPreferences sharedPref = getSharedPreferences("com.example.cowcount", Context.MODE_PRIVATE);
cowCount = sharedPref.getFloat("cowCount", 0);
cowMult = sharedPref.getFloat("cowMult", 0);
income = sharedPref.getFloat("income", 0);
}
...
When the code is like this, the application is being frozen. The application consists of a counter, and when I push the button that is supposed to count up, nothing happens.
However, when i comment out the line where i assign a value to the cowMult double from the SharedPreferences, the application doesn't freeze.
cowCount = sharedPref.getFloat("cowCount", 0);
// cowMult = sharedPref.getFloat("cowMult", 0);
income = sharedPref.getFloat("income", 0);
To be clear, the above works fine.
This is the method called when pushing the button (that is supposed to higher the value of cowCount by one):
public void addCow (View view) {
cowCount = cowCount + cowMult;
refresh();
}
...
public void refresh () {
TextView myTextView = (TextView)findViewById(R.id.myText);
myTextView.setText("You Have " + String.valueOf((nf.format(cowCount)) + " Cows!"));
}
Change your code as follows:
static float cowCount, income, cowMult;
...
protected void onResume()
{
super.onResume();
SharedPreferences sharedPref = getSharedPreferences("com.example.cowcount", Context.MODE_PRIVATE);
cowCount = sharedPref.getFloat("cowCount", 197);
cowMult = sharedPref.getFloat("cowMult", 1);
income = sharedPref.getFloat("income", 0);
}
...
The second parameter in SharedPreferences.getFloat() is a default value that the method will return if the key is not found. With the code you've supplied, If you've not saved the values to SharedPreferences properly, those variables will be assigned a value of 0. This is why nothing changes when you press the button; you're adding 0. Check to make sure you are saving to SharedPreferences correctly.
Also, there's no point in initializing the variables when you declare them, as they all get assigned a value in the onResume method, be it a saved value or a default value.
And, as pointed out by Martin, assign your TextView in your onCreate method.
A couple of things are strange with the code you have posted
1 why are you calling
SharedPreferences sharedPref = getSharedPreferences(null, Context.MODE_PRIVATE);
and not
SharedPreferences sharedPref = getSharedPreferences( "com.myname.myapp", Context.MODE_PRIVATE);
2 your onResume should be the following
super.onResume();
SharedPreferences sharedPref = getSharedPreferences(null, Context.MODE_PRIVATE);
cowCount = Double.longBitsToDouble(sharedPref.getLong("cowCount", 0));
cowMult = Double.longBitsToDouble(sharedPref.getLong("cowMult", 0));
income = Double.longBitsToDouble(sharedPref.getLong("income", 0));
calling the super.onResume() first before your code (same with all life cycle methods)
EDIT
3. Why are you not just setting your values to be an int (from what you said above they seem to be) or a float which will give you possibly all the precision then you can just get your values using
getInt(String key, int defValue)
of
getFloat(String key, float defValue)
Seconded Edit
There are a few strange ways you are doing things in the code that I can see. try the following code and let me know if it fixed the problem (though I can't see how the sharedPreferences would be causing it ). i am presuming the addCow method is being called from an onClickListener
//get a reference for your myTextView in the onCreate() method, after declaring your variable
//outside the onCreate method i.e
TextView myTextView;
...
// int onCreate()
myTextView = (TextView)findViewById(R.id.myText);
//you don't need to pass a view parameter, so don't
public void addCow () {
cowCount = cowCount + cowMult;
refresh();
}
...
public void refresh () {
//the way you are getting a string value is also not what I would do either use
//Float.toString(cowCount) or just
myTextView.setText("You Have " + cowCount + " Cows!"));
}
Hope the issue is gone.

issue with unmute button java android

I'm creating a simple click counter android app, sound is played when a button is clicked and the count is also saved when leaving the count screen and then returning to it.
I have encountered a problem with the mute button. When I click it, it mutes the whole application rather than just that particular gui screen (activity).
First issue is that the mute button mutes the sound for the whole app, and I only need to mute for that activity.
Second issue is that when you click mute button and come out of the screen, then go back, then try to unmute - it does not unmute the sound.
Was thinking the resolution to this is that we take the mute button out of the SharedPreferences save instance state - if this is possible...
Here is my code so far, if you can guide me on how to achieve the above that would be great. Thanks.
public class wazeefa extends Activity {
//Count Button
TextView txtCount;
ImageView image;
Button btnCount;
Button wmute;
Button wreset;
public static int count = 0;
SharedPreferences app_preferences;
MediaPlayer mpButtonClick;
AudioManager audioManager;
public static boolean mutestatus = false;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// The activity is being created.
setContentView(R.layout.wazeefa);
audioManager =
(AudioManager) getSystemService(Context.AUDIO_SERVICE);
//SAVE COUNT
app_preferences = this.getSharedPreferences("myPrefscount", MODE_WORLD_READABLE);
count = app_preferences.getInt("count", 0);
txtCount = (TextView) findViewById(R.id.wcount);
txtCount.setText("This app has been started " + count + " times.");
image = (ImageView) findViewById(R.id.imageview);
txtCount = (TextView) findViewById(R.id.wcount);
txtCount.setText("This app has been started " + count + " times.");
//Button SOUND AND COUNT
mpButtonClick = MediaPlayer.create(this, R.raw.bubble);
//RESET Button
wreset = (Button) findViewById(R.id.wreset);
txtCount = (TextView) findViewById(R.id.wcount);
txtCount.setText(String.valueOf(count));
btnCount = (Button) findViewById(R.id.wclick);
wmute = (Button) findViewById(R.id.wmute);
btnCount.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
count++;
if (count > 50) count = 0;
image.setImageResource(R.drawable.duroodimage);
if (count > 0) image.setImageResource(R.drawable.duroodimage);
if (count > 9) image.setImageResource(R.drawable.zikrimage);
if (count > 39) image.setImageResource(R.drawable.duroodimage);
txtCount.setText(String.valueOf(count));
mpButtonClick.start();
}
});
wreset.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
count = 0;
image.setImageResource(R.drawable.duroodimage);
;
txtCount.setText("0");
SharedPreferences.Editor editor = app_preferences.edit();
editor.putInt("count", count);
editor.commit();
}
});
wmute.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (!mutestatus) {
mutestatus = true;
audioManager.setMode(AudioManager.MODE_IN_CALL);
audioManager.setStreamSolo(AudioManager.STREAM_VOICE_CALL, true);
Log.v("'test....", "" + mutestatus);
} else {
mutestatus = false;
audioManager.setMode(AudioManager.MODE_NORMAL);
audioManager.setStreamSolo(AudioManager.STREAM_VOICE_CALL, false);
Log.v("'test....", "" + mutestatus);
}
}
});
}
#Override
protected void onPause() {
super.onPause();
// save count value here
SharedPreferences.Editor editor = app_preferences.edit();
editor.putInt("count", count);
editor.commit();
}
}
You are saving the preferences on the application level, make it Activity specific, i.e. Implement mute functionality for the activity not the application.
Edit
See your objective is some what to mute and unmute (loquent) the audio, Preferences can be saved in three ways.
1) Preferences can be retrieved only by a single activity.
2 )Preferences can be shared and retrieved among all activities within the application.
3)Preferences can be shared and retrieved through all applications on the device.
In your case, Saving Activity-level preferences:
SharedPreferences prefs=getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor=prefs.edit();
editor.putString("pref 1", "some text");
editor.commit();
We get a SharedPreferences object by calling getPreferences(int mode) method which takes an integer value as a parameter, the mode value can be one of the following:
Context.MODE_PRIVATE (0): a file creating mode that makes the created file only accessible by applications with the same user ID (access the file from the same application context, will desctribe later).
Context.MODE_WORLD_READABLE (1): file mode makes the file readable from other applications.
Context.MODE_WORLD_WRITEABLE (2): file mode allows other applications to write to the file.
Then we get an instance of SharedPreferences.Editor and write the preference value with editor.putString(String key, String value) method.
shared preferences allows you to insert preferences using the following methods:
editor.putBoolean(String key, boolean value).
editor.putFloat(String key,float value).
editor.putInt(String key, int value).
editor.putLong(String key, long value)
editor.putString(String key, String value)
Then we call edit.commit() to save the preferences to the file. commit returns a boolean indicating the result of saving, true if successful and false if failed.
Reading preferences values:
To read preferences values:
SharedPreferences prefs=getPreferences(Context.MODE_PRIVATE);
String val=prefs.getString("pref 1", "some text");
We use sharedpreferences.getString(String key, String defaultValue) (or get boolean/float/int) to return the value stored with a specific key or defaultValue if not found.
Source

Categories

Resources