"The Definitive Guide to Android Programming (Third Edition)" Chapter 12 Challenge Exercise Demo

"The Definitive Guide to Android Programming (Third Edition)" Chapter 12 Challenge Exercise Demo

The time is too tight, which makes it a bit slow to write, now to make up:

The second challenge exercise in Chapter 12 is indeed a bit difficult, but fortunately, it is easy to write if you understand it. Follow the steps in the book:

1. Change onCreateDialog(Bundle) to onCreateView():

public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {


        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH);
        int day = calendar.get(Calendar.DAY_OF_MONTH);

        View view = inflater.inflate(R.layout.dialog_date, container, false);

        mDatePicker = (DatePicker) view.findViewById(R.id.dialog_date_picker);
        mDatePicker.init(year, month, day, null);

        Ok = (Button) view.findViewById(R.id.date_ok);
        Ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int year = mDatePicker.getYear();
                int month = mDatePicker.getMonth();
                int day = mDatePicker.getDayOfMonth();
                Date date = new GregorianCalendar(year, month, day).getTime();
                /**
                 * Pass the value of data to the intent so that it can be passed back to CrimeFragment
                 */
                Intent intent = new Intent();
                intent.putExtra(EXTRA_DATE, date);
                getActivity().setResult(Activity.RESULT_OK, intent);
                getActivity().finish();
            }
        });

        return view;
    }

2. Create an activity hosting fragment:

public class DateActivity extends SingleFragmentActivity {

    /**
     * Inherit SingleFragmentActivity to host DatePickerFragment()
     * @return
     */
    protected Fragment createFragment(){
        return new DatePickerFragment();
    }

}

3. Open the activity with startActivityForResult():

mDateButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                /**
                 * Change the code to the following code to start the activity with startActivityForResult(intent, REQUEST_DATE);
                 * instead of starting a dialog
                 */
                Intent intent = new Intent(getActivity(), DateActivity.class);
                startActivityForResult(intent, REQUEST_DATE);
            }
        });

And receive the returned data with onActivityResult():

public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode != Activity.RESULT_OK) {
            return;
        }
        /**
         * Receive the data value returned by the activity
         */
        if (requestCode == REQUEST_DATE) {
            Date date = (Date) data.getSerializableExtra(DatePickerFragment.EXTRA_DATE);
            mCrime.setDate(date);
            updateDate();
        }
    }

In fact, the main steps are not too many. The significance of this question is to understand the concepts at the end of this chapter. The project source code: Demo

If there are any deficiencies, please advise :)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325586883&siteId=291194637