This little thing started to frustrate me a lot. I thought it was something simle to do, but I think I haven’t understood clearly how android works.
Anyway, the problem is that my android application will have some buttons that will open a date or time picker dialog box when pressed. I know how to implement them directly in the same class as the main activity itself, but I don’t think that’s good solution for maintenance reasons and also if I’m going to develop that application further some day.
I tried to do this by several different ways and failed every time I tried to do it. That left me pretty clueless of what to do and what’s the problem.
Here’s one way I tried to do this:
the main activity (I simplified it and tried to provide only the necessary code, because it’s long). This is following the singleton design pattern.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
dateButton = (Button) findViewById(R.id.editDateButton);
dateButton.setOnClickListener(this);
}
@Override
public void onClick(View view) {
if(view.getId() == R.id.editStartDateButton) {
DatePickers.getInstance().setDate(dateButton);
}
}
The date pickers class (extends Activity; also simplified code and all the unnecessary things removed):
private static DatePickers self = null;
public static DatePickers getInstance() {
if (null == self) {
self = new DatePickers();
}
return self;
}
public void setDate(Button button) {
theButtonUsed = button;
showDialog(DATE_DIALOG_ID);
}
After the showDialog call the class is similar to the example Hello-DatePicker in android developer site.
Also I tried to do it pretty much like in here, but with the differnece that when the dateButton is clicked it starts the new activity that’s the date picker dialog. In this case the datePicker class was like this:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.datePicker);
showDialog(DATE_DIALOG_ID);
}
And the rest as it is in the Hello-DatePicker example in android dev site. Both these solutions lead to a problem where my application crashed and was forced to quit.
I hope you understand what’s the problem and that you could guide me and show what I’m doing wrong. I tried to make this short and provide only the necessary information.
Thanks
-Z
Edit:
As I have mentioned in one of the comments, the real problem with this was that I has simply forgotten to add the new activity to the manifest file. There might have been some other problems that was solved with the accepted answer. I hope this is still helpful to anyone facing similar issues.
I think I can help you out there.
I know my solution isn’t going to follow your singleton approach but it definitely separates DatePicker code from the calling Activity or Activities – which in return becomes modular and clean.
So here it goes:
Below is the designated DatePicker code (I named it DateSelector). You start this activityForResult and it will return a bundle with Day, Month, and Year to the calling Activity.
I’m also going to pasting the simple layout DateSelector uses right below it; it’s just a transparent layout…
Here is the layout DateSelector uses:
Your calling Activity or Activities should launch DateSelector as such:
Starting the DateSelector from the calling Activity:
And finally in your calling Activity just catch it onActivityResult:
I hope this helps, hope you like it,
Best,
-serkan