Search code examples
androidandroid-activitytimepickerandroid-timepicker

How can I access a component(for example a timepicker) in one activity from another activity in Android?


I want to access a timepicker in an activity from another activity in the same app. For example, if I change the time in the timepicker in one activity, the time in the timepicker in the other activity should also change accordingly. How can I do this?


Solution

  • You can use putExtra to pass data to your new intent.

    int hour = timePicker.getCurrentHour();
    int minute = timePicker.getCurrentMinute();
    String time = hour + ":" + minute;
    
    Intent intent = new Intent(getBaseContext(), NewActivity.class);
    intent.putExtra(time, "timeData");
    startActivity(intent)
    

    To view the time.

    Bundle extras = getIntent().getExtras();
    if (extras != null) {
       String value = extras.getString("timeData");
       //do stuff with the time now...
    }