How to save a state in Shared preferences on Android Java? - java

In my application after login I have to save state in shared preference to change the app view on a button click. can anyone help me to save state in shared Preferences and on a button click i need to replace the initial state with the new one.

You can't save a layout. You can save a layout id- but I wouldn't suggest it. Resource ids are not stable across builds, so any update would break it. Your best bet is to create an enum with whatever values you wish it to have, save the enum, and later on convert the string back into an enum when you read it in. Then hold a map<enum, resourceId> in code that maps to the correct resource id.

Related

How do I populate entry fields with previously entered data currently saved in the database?

I'm creating an Android app which will allow the user to create CVs. I want the user to add all the details initially, but then have the option to return and edit if necessary. I want to repopulate the entry fields with the user's details which were entered previously (and saved to the database using SQLite).
I have a SAVE button which sends the data to the DB however I wasn't sure whether to add a LOAD button or whether the data will load automatically when the page is opened. Please advise.
I haven't pasted any code as I'm not sure if it will benefit my question right now.
ListView seems to be the most common method for displaying data however this doesn't help me as I require population of previously entered data to the original entry field.
There shouldn't be any load button in this case. The data should be fetched and populated automatically when the user navigates to the fragment or activity.
ListView can be used to display previously populated data.

Can an ArrayList made using a constructor from a class be saved to SharedPreferences?

So I am making a simple wine list app. There are three main activities.
MainActivity - this is where the list of wines is, along with a FAB that takes you to the AddWineActivity. Each list item on this page only shows a thumbnail, the wine name, price, and rating.
AddWineActivity - has a couple of EditTexts (to get the wine's name, price, and description), a ratingbar (to get the wine rating), and a button that converts the details into strings and puts them into into intent extras.
WineDetailsActivity - This will have a nice page that has all of the details for the wine in the list you clicked on.
I have the app pretty much working how I want it to currently. The only thing is that I need to actually save the list of wines so it wont reset after you go back in to add another wine, or if you leave the app and come back.
Here is the tricky part (which doesn't help since I don't know too much about saving to device anyway yet). I am adding to the arraylist using a Class.
I have a Wine class that has a constructor that looks like this:
public Wine(String mWineName, String mWinePrice, String mWineRating, String mWineDescription, int mWineImageResourceID) {}
So then on the MainActivity, to add to the list it looks like this
wines.add(new Wine(getIntent().getStringExtra("WINENAME"), getIntent().getStringExtra("WINEPRICE"), getIntent().getStringExtra("WINERATING"), getIntent().getStringExtra("WINEDESCRIPTION"), R.drawable.mywinelogo));
What would be the recommended way to add a list that looks like that to the device? Would it still be shared preferences or am I not on the right track?
If more details about the app are wanted, just ask for them and I'll provide them.
Thanks!
What would be the recommended way to add a list that looks like that to the device? Would it still be shared preferences or am I not on the right track?
In the end, you have a tabular data structure: rows (wines) and columns (name, rating, etc.).
When you have a tabular data structure, save it to something that is table-friendly. That could be a SQLite database or some form of file (e.g., JSON, XML).
SharedPreferences is not well-suited for this. You could convert the table to JSON and store it as a string preference, but that's relatively uncommon. IMHO, that would only make sense if most of the rest of your data were more natural for SharedPreferences (e.g., you were using PreferenceScreen to collect them) and wanted to keep everything together.

Looking for a way to save game data in my text based game in android studio (java)

I've scoured for the answer for this. I have a simple text based game. I make a choice with a radio button, and confirm it with a button click. I've set the game to save for when I change to landscape from portrait view.
But I cannot for the life of me find how to save the game when the back button is pressed.
I'd like to have a simple menu on the title screen with three buttons, one of them being "Continue" which would restore the game's saved settings. And obviously, one in the action menu I already have set up which would reset all the game data.
I've tried sharedPreferences etc. if someone can tell me the way to save the game data by using SQL so no user can mess with the data, that'd be better.
Thank you in advance for you help.
on edit I realized my question is not specific enough. I am trying to retain the state of a TextView which I am modifying with user choices, thus changing the text. Every time the app is restarted, the first text loads.
It kind of depends on how much information you want to save, but if it's very little I'd look in to shared preferences which saves key value pares. If it contains a bit more, you should look in to SQLLite which is just a database for your app on the device.
Further reading:
SharedPreferences:
https://developer.android.com/training/basics/data-storage/shared-preferences.html
SQLLite:
https://developer.android.com/training/basics/data-storage/databases.html
Have you tried serialization? Put the informations in a file, like a text file, then simply un-serialize it and take back the informations. You can always encrypt it if you don't want anyone to change the datas, like a real game save file.

Saving Android App Condition

I am pretty new to Android / Java App development and wanted to ask,
the best way to save the app condition.
In my case I have two buttons, which have a default color of red.
When the user clicks on each of them it gets the color of blue.
What's the best way to save this condition?
So that the color will not reset to the color of red when I restart the app.
Try to look here.
Principal data storage options in Android:
Saving key-value pairs of simple data types in a shared preferences
file
Saving arbitrary files in Android's file system
Using databases managed by SQLite

How do I save some textviews and load them from another class?

In my app, i have you put in some information through some edit text. Then you hit this button and it starts another activity that does some calculations and then displays results through text views. Well i want to be able to save all of those text views, and then open them up later. On the home screen i have a load button. When you click it, I want to be able to see the stuff I've saved and be able to open it by clicking on it. I'm new to android, so I'm having a hard time figuring this out. How should I go about doing this?
Use SharedPreferences...Check out..
http://www.vogella.com/articles/AndroidFileBasedPersistence/article.html
http://androiddeveloperspot.blogspot.in/2013/01/sharedpreference-in-android.html
this will help.
I see two questions in your question. The first is how to pass data from one Android activity to another.
The best way to do that is by using the putExtra method of the Intent class and then the getExtras method to extract the data in the receiving activity. Please see this SO quest and answers for more details.
For your second question, how to save data that can be recalled at a later by the main activity, you can use the SharedPreferences APIs. See this web page for more information on that. Basically the shared preferences APIs allow you to save key-value pairs of information which in your case can be field names (keys) and their associated values. For this you would want private preferences and pick a unique name for this preference file.

Categories

Resources