0

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.

Abhinav Raja
  • 349
  • 1
  • 5
  • 25

3 Answers3

1

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);
}
ElDuderino
  • 3,253
  • 2
  • 21
  • 38
  • but wouldn't this take the value of 'i' as zero which is what it is initialized to. – Abhinav Raja Mar 26 '14 at 12:13
  • I has the state that it got last assigned to. You increase i every 3 seconds, if I see your code correctly. So after 12 seconds i schould be 4. – ElDuderino Mar 26 '14 at 12:15
  • OK lemme try it then. – Abhinav Raja Mar 26 '14 at 12:19
  • like i said its taking the value of i as zero and setting the first word again on mode change. – Abhinav Raja Mar 26 '14 at 12:46
  • Try my example. It works. You have to put in if(savedInstanceState!=null) current = savedInstanceState.getInt("current"); in OnCreate(), like I said in my example. – ElDuderino Mar 26 '14 at 12:52
  • i tried yours. doesn't work. first nothing comes up. its a blank screen and then after like a half min first word comes and stays like that. then when i change to landscape same thing happens (first blank screen for half a min and then first word). – Abhinav Raja Mar 26 '14 at 13:32
  • If it doesn't work you are doing it wrong....you need a layout file named R.layout.activity_main and in there a textView with @+id/tv. And you have to put the MainActivity into your manifest. – ElDuderino Mar 26 '14 at 13:47
  • for that i made changes in my program accordingly. what is ActionBarActivity that your class is extending. its giving me an error there. – Abhinav Raja Mar 26 '14 at 14:14
  • Import ActionBarActivity or just change it to Activity, that doesn't matter. – ElDuderino Mar 26 '14 at 14:37
  • yeah its working now. earlier i hadn't written onDestroy(). but working now. only small mistake that its starting from the second word. that current++ needs to be after tv.setText(wordList[current]);. thank you for your time. – Abhinav Raja Mar 26 '14 at 17:17
  • but how do i pause and start playing again. will handler.removemessage(0) and handler.sendmessagedelayed() work for pause/play? – Abhinav Raja Mar 26 '14 at 17:22
  • Just try it out :) I think it should work, but I never used it before. Until now I always used handler.postDelayed(runnable, millis) which works fine for a delayed execution. Your sleep method is basically not necessary, but I guess you can do it that way.... – ElDuderino Mar 26 '14 at 17:48
0

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

Community
  • 1
  • 1
Android
  • 8,995
  • 9
  • 67
  • 108
  • this will give me the word to display. i don't want the word. i want the position of that word in the array. because the word keeps changing at 3 sec. so when the state changes it has to set the word which was already there and then start changing it from there. – Abhinav Raja Mar 26 '14 at 12:17
0
protected void onSaveInstanceState(Bundle bundle) {
   bundle.putInt("Myint", i)
    super.onSaveInstanceState(bundle);
}