Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Reset Activity onClick

I have an activity and on that activity there are a couple of buttons that do different things with numbers, etc. At a certain point though I would like to be able to have the user reset/(restart?) the activity back to the initial state without having the user have to hit the back button or restart the app.

I want to create a reset button. I know how to make the button itself, but I do not the details of how to reset the activity.

@Override
public void onClick(View v) {
    switch (v.getId()) {
    case R.id.button1:
        // do stuff
    break;
    case R.id.button2:
        // do stuff
        break;      
    case R.id.button3:
        // do stuff
        break;
    case R.id.reset:
        // what goes here?
    default:
        break;
    }
}

How is this done?

like image 222
lord_sneed Avatar asked Dec 11 '12 02:12

lord_sneed


1 Answers

This will restart your activity.

@Override
public void onClick(View v) {
    switch (v.getId()) {
    case R.id.button1:
        // do stuff
    break;
    case R.id.button2:
        // do stuff
        break;      
    case R.id.button3:
        // do stuff
        break;
    case R.id.reset:
         Intent intent = getIntent();
         finish();
         startActivity(intent);    default:
        break;
    }
}

You can add the following to get rid of the fancy animations.

overridePendingTransition(0, 0);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
like image 118
Lazy Ninja Avatar answered Oct 20 '22 08:10

Lazy Ninja