0

I'm wondering how to refresh an activity. Not like restart it, but refresh it. What is happening is I have it so a button lights up, and after it is clicked it waits 5 seconds then resets the buttons and has another one light up, the only problem is the other buttons are not lighting up, as it stays stuck on the same button. I don't know if this is a problem with the code, or I'm not calling a refresh, etc. Any help is good! Thanks.

if(selected == target) {
    Toast.makeText(getApplicationContext(), "Good job!", Toast.LENGTH_SHORT).show();
    view.getBackground().setColorFilter(Color.GREEN, PorterDuff.Mode.MULTIPLY);
    } else {
        Toast.makeText(getApplicationContext(), "Wrong!", Toast.LENGTH_SHORT).show();
        view.getBackground().setColorFilter(Color.RED, PorterDuff.Mode.MULTIPLY);
    }
h.postDelayed(task, millisDelay);

then my task is this:

private Runnable task = new Runnable() { 
    public void run() {
      ResetButtons();
      int rnd = new Random().nextInt(buttons.length);
      i = rnd;
      buttons[i].getBackground().setColorFilter(Color.YELLOW, PorterDuff.Mode.MULTIPLY);
    }
};
Tyson
  • 85
  • 7

1 Answers1

0

Try this:

public void onClick (View v){
    Intent intent = getIntent();
    finish();
    startActivity(intent);
}

(OR)

This is what I do to reload the activity after changing returning from a preference change.

@Override
protected void onResume() {

   super.onResume();
   this.onCreate(null);
}

This essentially causes the activity to redraw itself.

Shailendra Madda
  • 20,649
  • 15
  • 100
  • 138
  • How do I do this without restarting the entire activity? Just updating the UI so it updates smoothly instead of the page getting closed and opened again? – Tyson Jan 24 '14 at 04:35
  • the view.invalidate() only updates the button when it is pushed, i need it to be updating as i push it so it automatically changes a different color. I think I need to use ASync but I have no idea where to start on that. – Tyson Jan 24 '14 at 05:29
  • restarting the activity lifecycle is not a good solution for trying to force a redraw of ui components – Ryan Chipman Jan 24 '14 at 05:35