How to separate the main activity and date selection to your own classes

This little thing started to upset me a lot. I thought it was something similar, but I think I did not understand how the android works.

In any case, the problem is that my Android application will have some buttons that will open the date or time dialog box when pressed. I know how to implement them directly in the same class as the main activity itself, but I do not think that this is a good solution for maintenance reasons, and also if I intend to develop this application again.

I tried to do this in several different ways and failed every time I tried to do it. This left me pretty elusive about what to do and what the problem is.

Here is one way I tried to do this:

The main action (I simplified it and tried to provide only the necessary code, because it is long). This matches 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); } } 

Date picker class (extends activity, code is also simplified and all unnecessary things are deleted):

 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 calling showDialog, the class is similar to the Hello-DatePicker example on the Android developer site.

I also tried to do this to a large extent, as in here , but with differnece, when a dateButton button is pressed, a new action begins, which is the date selection 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 everything else, as in the Hello-DatePicker example, on the Android developer site. Both of these solutions lead to a problem when my application crashed and was forced to stop working.

I hope you understand what the problem is, and that you can guide me and show what I'm doing wrong. I tried to do this briefly and provide only the necessary information.

Thanks -Z

Edit:

As I mentioned in one of the comments, the real problem was that I just forgot to add new activity to the manifest file. Perhaps there were some other problems that were resolved with the accepted answer. I hope that this will still help anyone who encounters similar problems.

+4
source share
1 answer

I think I can help you there.

I know that my solution will not follow your singleton approach, but it definitely separates the DatePicker code from the calling Activity or Activity - which in turn becomes modular and clean.

So, here it is: Below is the assigned DatePicker code (I named it DateSelector). You start this actionForResult and it returns a packet with day, month and year to the calling activity.

I am also going to insert a simple layout that DateSelector uses right below it; it's just a transparent layout ...

 package com.cyberfabric.historicise.activities; import java.util.Calendar; import com.cyberfabric.historicise.R; import android.app.Activity; import android.app.DatePickerDialog; import android.app.Dialog; import android.content.Intent; import android.os.Bundle; import android.view.Window; public class DateSelector extends Activity{ private final static int DIALOG_DATE_PICKER = 0; private int setYear; private int setMonth; private int setDay; @Override public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); getWindow().requestFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.date_picker); Calendar today = Calendar.getInstance(); setYear = today.get(Calendar.YEAR); setMonth = today.get(Calendar.MONDAY); setDay = today.get(Calendar.DAY_OF_MONTH); showDialog(DIALOG_DATE_PICKER); } @Override protected Dialog onCreateDialog(int id){ switch(id){ case DIALOG_DATE_PICKER: return new DatePickerDialog(this, mDateSetListener, setYear, setMonth, setDay); } return null; } private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener(){ @Override public void onDateSet(android.widget.DatePicker view, int year, int month, int day) { setYear = year; setMonth = month; setDay = day; returnDate(); } }; /* * Package up the data and return it back to the calling intent */ private void returnDate(){ Intent intent = getIntent(); // calling/parent intent // Bundle bundle = new Bundle(); bundle.putInt("year", setYear); bundle.putInt("month", setMonth); bundle.putInt("day", setDay); intent.putExtra("set_date", bundle); setResult(Activity.RESULT_OK, intent); finish(); } } 

Here is the DateSelector layout uses:

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@color/transparent" /> 

In your activity or actions you should run DateSelector as such:

 // some global variable declared in order to start activityForResult and to catch // it back on onActivityResult private final static int REQUEST_GET_DATE = 3; 

Starting DateSelector from the caller:

 Intent dp = new Intent(EventForm.this, DateSelector.class); EventForm.this.startActivityForResult(dp, REQUEST_GET_DATE); 

And finally, in your calling activity, just catch it onActivityResult:

 protected void onActivityResult(int requestCode, int resultCode, Intent data){ super.onActivityResult(requestCode, resultCode, data); switch(requestCode){ case REQUEST_GET_DATE: if(resultCode == Activity.RESULT_OK){ Bundle setDate = data.getBundleExtra("set_date"); int setDay = setDate.getInt("day"); int setMonth = setDate.getInt("month"); int setYear = setDate.getInt("year"); System.out.println(setDay + " " + setMonth + " " + setYear); } break; } } 

Hope this helps, hope you enjoy

Best

-serkan

+1
source

Source: https://habr.com/ru/post/1381097/


All Articles