title: How to show AlertDialog in a fragment
body: <p>I'm a beginner in both Java and Android, please help.I want to put the Alert Dialog in a fragment but I ended up copying most of the code in the below class so both classes contains almost the same code. Could you tell me how can I separate the code without copying most of it in my Fragment class? </p>
  
  <p>Thank you!</p>
  
  <pre><code>public class MainActivity extends Activity {
  
  private int mInterval = 1000;
  private Handler mHandler;
  TextView textView;
  boolean mStarted;
  final static String simple_Date_Format = "HH:mm:ss SSS";
  
  public void updateStatus() {
      long currentTimeMillis = System.currentTimeMillis();
      Date date = new Date(currentTimeMillis);
      SimpleDateFormat simpleDateFormat = new SimpleDateFormat(simple_Date_Format);
      String time_now = simpleDateFormat.format(date.getTime());
      textView.setText(time_now);
  }
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  
      long currentTimeMillis = System.currentTimeMillis();
  
      Calendar calendar = Calendar.getInstance();
      calendar.setTimeInMillis(currentTimeMillis);
  
      Date date = new Date(currentTimeMillis);
      SimpleDateFormat simpleDateFormat = new SimpleDateFormat(simple_Date_Format);
      String time_now = simpleDateFormat.format(date.getTime());
      textView = (TextView) findViewById(R.id.textview);
      textView.setText(time_now);
  
      mHandler = new Handler();
      startRepeatingTask();
  
      Button button = (Button) findViewById(R.id.button);
      button.setOnClickListener(new View.OnClickListener() {
  
          @Override
          public void onClick(View v) {
  
              AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                      MainActivity.this);
  
              if (mStarted) {
                  alertDialogBuilder.setTitle(R.string.settings);
                  alertDialogBuilder.setMessage(R.string.stop_message);
  
              } else {
                  alertDialogBuilder.setTitle(R.string.settings);
                  alertDialogBuilder.setMessage(R.string.restart_message);
              }
  
              alertDialogBuilder.setPositiveButton(R.string.click_me,
                      new DialogInterface.OnClickListener() {
  
                          @Override
                          public void onClick(DialogInterface dialog, int which) {
                              if (!mStarted) {
                                  startRepeatingTask();
                              } else {
                                  stopRepeatingTask();
                              }
  
                          }
                      });
  
              alertDialogBuilder.setCancelable(true);
              AlertDialog alertDialog = alertDialogBuilder.create();
              alertDialog.show();
          }
      });
  }
  
  Runnable mStatusChecker = new Runnable() {
      @Override
      public void run() {
  
          updateStatus(); // this function can change value of mInterval.
          mHandler.postDelayed(mStatusChecker, mInterval);
  
      }
  };
  
  void startRepeatingTask() {
      mStatusChecker.run();
      mStarted = true;
  }
  
  void stopRepeatingTask() {
      mHandler.removeCallbacks(mStatusChecker);
      mStarted = false;
  }
  </code></pre>