Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In AddPeriod.java you create a TimePickerDialog.OnTimeSetListener but as far as I can see that listener isn't used.</p> <p>In TimePickerFragment.java you implement the TimePickerDialog.OnTimeSetListener interface and pass the fragment instance itself as a listener to the TimePickerDialog but you do nothing in the implemented onTimeSet() method in TimePickerFragment.java. Are you saying that onTimeSet() in TimePickerFragment.java isn't called? Have you set a breakpoint?</p> <p>If you manage to get the onTimeSet() called in your fragment I assume what you're really asking for is how to pass this time back to the activity. If I'm correct I'd say that this is a perfect place to use an event bus, for instance using <a href="http://square.github.io/otto/" rel="nofollow">Otto</a>.</p> <p>UPDATE: Based on your comments to my questions I assume that what you really want is a way to get the picked time in TimePickerFragment back to AddPeriod. The better approach is in my opinion to use an event bus (as mentioned above). This will decouple your fragment from your activity. BUT to give you an answer that works with minimal changes to your code I suggest that you pass a listener to the fragment and use that listener when you create your dialog:</p> <p>TimePickerFragment.java:</p> <pre><code>public class TimePickerFragment extends DialogFragment { private TimePickerDialog.OnTimeSetListener listener; public TimePickerFragment(TimePickerDialog.OnTimeSetListener listener) { super(); this.listener = listener; } @Override public Dialog onCreateDialog(Bundle savedInstanceState) { // Use the current time as the default values for the picker final Calendar c = Calendar.getInstance(); int hour = c.get(Calendar.HOUR_OF_DAY); int minute = c.get(Calendar.MINUTE); // Create a new instance of TimePickerDialog and return it return new TimePickerDialog(getActivity(), listener, hour, minute, DateFormat.is24HourFormat(getActivity())); } } </code></pre> <p>As you can see I've added a constructor that takes a TimePickerDialog.OnTimeSetListener argument. This listener is used when the TimePickerDialog is created. Now in our AddPeriod activity we pass the mTimeSetListener to the fragment when it is created:</p> <pre><code>public class AddPeriod extends SherlockFragmentActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.add_period); } public void showTimePickerDialog(View v) { DialogFragment newFragment = new TimePickerFragment(mTimeSetListener); newFragment.show(getSupportFragmentManager(), "timePicker"); } TimePickerDialog.OnTimeSetListener mTimeSetListener = new TimePickerDialog.OnTimeSetListener() { @Override public void onTimeSet(android.widget.TimePicker view, int hourOfDay, int minute) { Log.i("",""+hourOfDay+":"+minute); } }; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload