Question

i have a set of words in an array and i am displaying them one by one like a slideshow using Handlers. the problem is when i switch mode (land to portrait or vice verse) it starts again from the first word.

i know i have to save the variables in onSaveInstanceState() but the problem is the value of the variable 'i' which i want to save is in a different method (updateUI()).

this is my activity:

public class WatSlideShow extends Activity {

RefreshHandler refreshHandler = new RefreshHandler();

TextView watwords;
int i = 0;

String wat1[] = { "SICK", "FUTURE", "PROBLEM", "DECEIVE", "SUFFER",
        "FAITH", "PROTECT", "SUICIDE", "STEP", "WNJOY", "FAIL" };

class RefreshHandler extends Handler {
    @Override
    public void handleMessage(Message msg) {
        // TODO Auto-generated method stub
        WatSlideShow.this.updateUI();
    }

    public void sleep(long delayMillis) {
        this.removeMessages(0);
        sendMessageDelayed(obtainMessage(0), delayMillis);
    }
};

public void updateUI() {

    if (i < wat1.length) {
        watwords.setText(wat1[i]);
        refreshHandler.sleep(3000);
        i++;

    }


}

protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.watslideshow);
    updateUI();
}

how should i save the value of 'i' (which is the word being displayed) in onSaveInstanceState().

protected void onSaveInstanceState(Bundle outState) {
    // TODO Auto-generated method stub
    super.onSaveInstanceState(outState);
    int currentI =  ? // how to get value of 'i' here
           outstate.putInt("Myint", currentI)
}   

and then i can use this in oncreate():

int myInt = savedInstanceState.getInt("MyInt");

sorry but my java concepts not strong yet. still learning.

Was it helpful?

Solution

You declared i as a class member variable of your activty, you can access it anywhere in your activity.

protected void onSaveInstanceState(Bundle outState) {
    outstate.putInt("Myint", i)
    super.onSaveInstanceState(outState);
}  

Update

public class MainActivity extends Activity {

private String[] wordList = { "SICK", "FUTURE", "PROBLEM", "DECEIVE", "SUFFER","FAITH", "PROTECT", "SUICIDE", "STEP", "WNJOY", "FAIL" };
private TextView tv;
private int current = 0;
private Handler handler = new Handler();

public void updateUI() {

    current++;
    if (current == wordList.length) current=0;
    tv.setText(wordList[current]);


    handler.postDelayed(new Runnable() {

        @Override
        public void run() {
            updateUI();
        }
    }, 3000);

}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    if(savedInstanceState!=null) current = savedInstanceState.getInt("current");
    tv = (TextView) findViewById(R.id.tv);
    updateUI();
}

protected void onSaveInstanceState(Bundle outState) {
    outState.putInt("current", current);
    super.onSaveInstanceState(outState);
}   

@Override
public void onDestroy() {
    super.onDestroy();
    handler.removeCallbacksAndMessages(null);
}

OTHER TIPS

Lets assume your textview is named as MyTextView in your layout xml file. Your activity will need the following:

private TextView mTextView; private static final String KEY_TEXT_VALUE = "textValue";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       mTextView = (TextView) findViewById(R.id.main);
       if (savedInstanceState != null) {
          String savedText = savedInstanceState.getString(KEY_TEXT_VALUE);
          mTextView.setText(savedText);
       }

    @Override
    protected void onSaveInstanceState (Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putString(KEY_TEXT_VALUE, mTextView.getText());
    }

For more detail see this

protected void onSaveInstanceState(Bundle bundle) {
   bundle.putInt("Myint", i)
    super.onSaveInstanceState(bundle);
}  
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top