Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8418049
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T02:12:41+00:00 2026-06-10T02:12:41+00:00

I know this has been asked many times, but I can’t find an answer

  • 0

I know this has been asked many times, but I can’t find an answer to suit my problem.

So I have an activity which has a button where its’ text is the current time (the time the activity was accessed). I can click on the button to show a TimePickerDialog where I can change the time, click the ‘Set’ button and the button updates with the new time. So far, so good, so basic.

My problem is that if I launch the TimePicker, change the time, click the Cancel button and then launch the TimePicker again, the displayed time is the time I cancelled instead of the original time from the button that launched it. If I cancel the TimePicker, I want it to forget any previous data.

Here is my code:

protected Dialog onCreateDialog(int id) {
    switch (id) {
    case TIME_DIALOG_ID:
        recordTime = new TimePickerDialog(this,mTimeSetListener, mHour, mMinute, true);
        recordTime.setOnCancelListener(new OnCancelListener(){

            @Override
            public void onCancel(DialogInterface arg0) {

final Calendar c = Calendar.getInstance();
                mHour = c.get(Calendar.HOUR_OF_DAY);
                mMinute = c.get(Calendar.MINUTE);
                recordTime.dismiss();
            }
        });

        recordTime.setOnDismissListener(new OnDismissListener(){
            @Override
            public void onDismiss(DialogInterface arg0) {
                recordTime.cancel();
                recordTime.dismiss();
            }

        });
        return recordTime;

    }
    return null;
}


// the callback received when the user sets the time in the dialog
    private TimePickerDialog.OnTimeSetListener mTimeSetListener =
        new TimePickerDialog.OnTimeSetListener() {
            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                mHour = hourOfDay;
                mMinute = minute;
                updateTimeDisplay();
            }
        };
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T02:12:43+00:00Added an answer on June 10, 2026 at 2:12 am

    If I cancel the TimePicker, I want it to forget any previous data.

    Each time a Dialog is opened with showDialog() it passes through onPrepareDialog(), simply reset the time here. If the user clicked Set the new time is saved in mHour and mMinute, if the dialog is cancelled for any reason this restores the original data:

    @Override
    protected void onPrepareDialog(int id, Dialog dialog, Bundle args) {
        switch (id) {
        case TIME_DIALOG_ID:
            recordTime.updateTime(mHour, mMinute);
        }
    }
    

    You can easily call this in the OnCancelListener as well:

    recordTime.setOnCancelListener(new OnCancelListener() {
        @Override
        public void onCancel(DialogInterface dialog) {
            recordTime.updateTime(mHour, mMinute);
        }
    });
    

    Also your original OnCancel and OnDismiss listeners seem redundant. What are you trying to do with them?

    You should know that onDismiss() is called if the user clicks Set, Cancel, or uses the back button. If the user clicks Set you don’t want to call the onCancel() method is onDismiss(). Don’t treat onCancel() and onDismiss() as the same methods.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know this has been asked many times, but I cant find answer to
I know this question has been asked many times before but I can't find
I know this has been asked many times before, but i still can't seem
I know this has been asked many times but i'm yet to find a
I know this has been asked thousands of times but I just can't find
I know this question has been asked many times, but my problem is a
I know this has been asked a bunch of times but I can't find
I know this question has been asked many times on SO, but I can't
I know this question has been asked many times, but I have not been
I know this question has been asked many times on here but I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.