I am building a memory game app for a java class and i need to save which words are displayed through rotation of the device...the widgets that need to be saved and passed to the new onCreate() method are button widgets that i used setText to display the words that are being shown....can i save and pass the entire state of the application...here is my code that is obviously not working and i was hoping i could get some insight...thanks...here is my code..
private Bundle myBundle = new Bundle();
.....
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_eight);
if(savedInstanceState != null) {
savedInstanceState = myBundle;
}
......
#Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
myBundle = savedInstanceState;
}
You got the semantics of using the Bundle backwards. You don't ever want to save the Bundle (or overwrite it). Just use it to serialize your own state variables into it and out of it. For example, if you want to preserve two integer variables and a string between a rotation (such that you can re-initialize the view with these members later), you could do the following:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_eight);
if(savedInstanceState != null) {
// Restore our member variables that represent activity state right here from the passed in Bundle.
_x = savedInstanceState.getInt("MyApp.X", 0);
_y = savedInstanceState.getInt("MyApp.Y", 0);
_name = savedInstanceState.getString("MyApp.NAME", "player");
// not shown - initializing the view elements with the initialized member variables.
}
......
#Override
public void onSaveInstanceState(Bundle savedInstanceState) {
// save our activity state by adding our member variables that represent activity state into the Bundle.
savedInstanceState.putInt("MyApp.X", _x);
savedInstanceState.putInt("MyApp.Y", _y);
savedInstanceState.putString("MyApp.NAME", _name);
// call the parent method *after* you have saved your state
super.onSaveInstanceState(savedInstanceState);
}
Bundle also supports arrays of simple types.
In method onSaveInstanceState add values which you want to save like below
#Override
public void onSaveInstanceState(Bundle savedInstanceState) {
savedInstanceState.putString("key","your string here");
}
and inside oncreate
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if( savedInstanceState != null ) {
System.out.println(savedInstanceState .getString("key"));
}
}
Related
This is a puzzle app. I have a class of Puzzle that contains the object itself and business logics. In onCreate, I created the object itself, I need to save the instance. Is it a good idea to save the same object I created in onCreate in onSaveInstanceState()? If so, what is the best way to do it? Otherwise, should I save the variables of the object Puzzle in onSaveInstanceState()?
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Read in the arrays
String[] targetWords = getResources().getStringArray(R.array.target_words);
String[] okWords = getResources().getStringArray(R.array.acceptable_words);
// TODO: Now we always create a new puzzle when the Activity is created. You
// will need to restore the existing activity if the Framework has destroyed
// and recreated it.
newPuzzle = new Puzzle(targetWords, okWords);
for (int i = 0; i < newPuzzle.getGuessCount(); i++) {
displayResult(i);
}
}
// save the puzzle obj?
#Override
public void onSaveInstanceState(Bundle outState){
super.onSaveInstanceState(outState);
}
Make your Puzzle Object Parcelable or Serializable, then save it in:
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putSerializable("puzzleObj", newPuzzle);
}
You can retrieve it in onCreate()
if (savedInstanceState != null) {
newPuzzle = (Puzzle) savedInstanceState.getSerializable("puzzleObj");
}
I want to keep the spinner position when the screen is rotated
I've looked at a few answers on here and so far have the following
Aim - Store the position of the spinner
protected void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
flav1Spinner = (Spinner)findViewById(R.id.Combo_InvChoice1);
Integer flav1 = flav1Spinner.getSelectedItemPosition();
savedInstanceState.putInt("cho1", flav1);
}
Aim - Restore position
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calculation);
Locale.setDefault(Locale.US);
if (savedInstanceState != null){
flav1Spinner = (Spinner)findViewById(R.id.Combo_InvChoice1);
Integer flav1 = savedInstanceState.getInt("cho1");
flav1Spinner.setSelection(flav1);
}
However while debugging I can see that the Integer Flav1 is getting a value assigned to it and the digit is being retrieved, however the spinner isn't changing it's position. Any suggestions?
You can do it by onSaveInstanceState(), As below
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("yourSpinner", yourSpinner.getSelectedItemPosition());
// do this for each of your Spinner
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState != null) {
yourSpinner.setSelection(savedInstanceState.getInt("yourSpinner", 0));
// do this for every text views
}
}
Check Saving Activity state, it will help you to get more details.
I have the following (to avoid checking if a fragment is being created newly or just being recreated)
#Override public void onActivityCreated(Bundle args) {
super.onActivityCreated(args);
if (this.getArguments() != null) args = this.getArguments();
// rest of fragment loading goes here
}
Not sure what you are trying to do never seen budle values being overwritten.
If you want some values to be availbale inside fragment pass those values to activity and inside fragment call getActivity().getIntent().getExtra(
Otherwise have a look at code below if want save some fragment data if fragment is destroyed
Inside fragment in onCreate method check if savedInstanceState is null or not
#Override
public void onCreate(Bundle savedInstanceState) {
if (savedInstanceState != null) {
//fragment recreated
}
}
Save data as follows
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
if (jsonObject != null) {
outState.putString(Constants.JSON, jsonObject.toString());
}
}
I use an intent to start another activity to display data in an ArrayList.
After pressing the back button in the action bar, the values of the array list are lost.
How do I fix this?
The code I'm currently using:
#Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
savedInstanceState.putStringArrayList(X_COORDINATE, dataX);
savedInstanceState.putStringArrayList(Y_COORDINATE, dataX);
}
#Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
dataX=savedInstanceState.getStringArrayList(X_COORDINATE);
dataY=savedInstanceState.getStringArrayList(Y_COORDINATE);
}
The first method is how I put my arraylists in to the Bundle, and the second method is how I restore it.
try this:
protected void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
savedInstanceState.putStringArrayList(X_COORDINATE, dataX);
}
And restore the values in onCreate():
public void onCreate(Bundle savedInstanceState) {
if (savedInstanceState!= null){
dataX=savedInstanceState.getStringArrayList(X_COORDINATE);
}
}
the ArrayList is defined on class level.
these are my savedInstance methods:
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putStringArrayList("savedList", list);
}
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
list=savedInstanceState.getStringArrayList("savedList");
}
but still, when i change orientation the ArrayList is blank
When you use onRestoreInstanceState() to restore state, its called after onStart() so you update your list with the saved state after you define your adapter. Your best option is to restore the list in onCreate() the same way you do it on onRestoreInstanceState(). You can also redefine the adapter or call notifyDataSetChanged().
TextView textviewname;
String textname;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//declare the ui like this
textviewname = (TextView) findViewById(R.id.textviewid);
if(savedInstanceState==null){
//the first time the activity was created
//do the normal flow of the code
//Example:getting the intent data for(savedList)or processing it
Intent i = getIntent();
textname = i.getStringExtra("savetext");
//or textname="name_data";
}else{
//this is called when orientation change occur
//get the savedata
list=savedInstanceState.getStringArrayList("savedList");
textname=savedInstanceState.getString("savetext");
}
textviewname.setText(textname);
}
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
//saving data when orientation change trigger
//saving data before ondestroy happens
//onSaveInstanceState is called before going to another activity or orientation change
outState.putStringArrayList("savedList", list);
outState.putString("savetext", textname);
//all imnportant data saved and can be restored
}
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
//this is only called after ondestroy-> oncreate occur
list=savedInstanceState.getStringArrayList("savedList");
textname=savedInstanceState.getString("savetext");
//oncreate->onSaveInstanceState->ondestroy->oncreate->onRestoreInstanceState
}
try to use
list.addAll(savedInstanceState.getStringArrayList("savedList"));
instead
list=savedInstanceState.getStringArrayList("savedList");