How to use saved instance state and restore instance state with spinners in in Android?
I have a spinner in my activity with a list of options,
When the user selects an option, I have used OnItemSelected adapter to get the position value of the selected option and within that i have added some If Else Statements. The If statement, actually generates a random string from the predefined database and displays on TextView.
Now i will be switching back to another activity and again come back to this activity. Now the problem is by default the first option of the spinner is only selected, I need to update with the last selected option of the user and when the activity starts again that option's functions must be run.
I did some searches too and found that this can be done by using onSaveInstanceState
and onRestoreInstanceState
now :
- How do I use these two and where should i put them, inside the onItemSelected(adapter) or Outside?
- How do I update(or store) the new position value selected by the user to the random generated code?
For clarity please check my code:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_scheduledactivity);
/**displayed when the activity is launched (this should be
updated with the user selected option)**/
Resources res = getResources();
myString = res.getStringArray(R.array.normal);
String q = myString[rgenerator.nextInt(myString.length)];
TextView tv = (TextView) findViewById(R.id.text1);
tv.setText(q);
//initiating the spinner
Spinner s = (Spinner) findViewById( R.id.spinner);
s.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){
@Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
if (pos == 1){
//displayed on the time when user selects the option
Resources res = getResources();
myString = res.getStringArray(R.array.hardships);
String q = myString[rgenerator.nextInt(myString.length)];
TextView tv = (TextView) findViewById(R.id.text1);
tv.setText(q);
}
else {
//do nothing
}
EDITED QUESTION:
I have edited the question for more clarity. Basically what I am trying to do is, I want to save the position selected by they user, with the spinner. When the user clicks "Thankyou" Button
the activity will finish and will start after few minutes called by a Broadcast receiver from a different class. When the activity starts again the last selected option (previous activity) of the user must be as default in this activity.
I think if you look at the code the problem is in save and restore instance state.
Please have a look at my code:
import java.util.Random;
import android.app.Activity;
import android.content.res.Resources;
import android.os.Bundle;
import an开发者_运维知识库droid.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
public class MyScheduledActivity extends Activity {
private String[] myString;
private static final Random rgenerator = new Random();
protected int mPos;
protected String mSelection;
String situation[] = {"Normal","Hardship","Sadness","Exam"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_scheduledactivity);
if (mPos == 0) {
Resources res = getResources();
myString = res.getStringArray(R.array.normal);
String q = myString[rgenerator.nextInt(myString.length)];
TextView tv = (TextView) findViewById(R.id.text1);
tv.setText(q);
}
//selection of the spinner
Spinner s = (Spinner) findViewById( R.id.spinner);
// Application of the Array to the Spinner
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, situation);
spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
s.setAdapter(spinnerArrayAdapter);
s.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){
@Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
MyScheduledActivity.this.mPos = pos;
MyScheduledActivity.this.mSelection = parent.getItemAtPosition(pos).toString();
//display toast
Toast.makeText(parent.getContext(), "The Situation is " +
parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show();
if (mPos == 0){
Resources res = getResources();
myString = res.getStringArray(R.array.hardships);
String q = myString[rgenerator.nextInt(myString.length)];
TextView tv = (TextView) findViewById(R.id.text1);
tv.setText(q);
}
if (mPos == 1){
Resources res = getResources();
myString = res.getStringArray(R.array.hardships);
String q = myString[rgenerator.nextInt(myString.length)];
TextView tv = (TextView) findViewById(R.id.text1);
tv.setText(q);
}
else {
//do nothing
}
}
@Override
public void onNothingSelected(AdapterView<?> parentView) {
//do something else
return;
}
});
//dismiss button - Thank You
Button buttonDismiss = (Button)findViewById(R.id.dismiss);
buttonDismiss.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
finish();
}});
}
@Override
protected void onSaveInstanceState(Bundle outState) {
outState.putStringArray(mSelection, situation);
super.onSaveInstanceState(outState);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
situation = savedInstanceState.getStringArray("mSelection");
super.onRestoreInstanceState(savedInstanceState);
}
}
This is how the activity looks
In your present Activity override a method called onSaveInstanceState()
and put your data in bundle.
and override another method onRestoreInstanceState()
and get those values from the bundle....
精彩评论