lord_sneed
lord_sneed

Reputation: 824

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?

Upvotes: 5

Views: 13887

Answers (1)

Lazy Ninja
Lazy Ninja

Reputation: 22527

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);

Upvotes: 12

Related Questions