0

I am working on a bus booking app.

This is my Home page:

Anywhere click on JournyDate layout will open this page:

Basically, I need to set selected date on JournyDate layout.

For Calendar I am using this library.

Home Fragment

datelayout.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(getContext(), DateChooser.class);
            startActivityForResult(intent, 11);
        }
    });

 @Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK && requestCode == 11) {
        Date date = new Date(data.getExtras().getString("Date"));
        txtDate.setText((CharSequence) date);
    }
}

DateChooser Activity

public class DateChooser extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_date_chooser);

    MaterialCalendarView calendarView = (MaterialCalendarView) findViewById(R.id.calendarView);

    CalendarDay calendarDay = calendarView.getCurrentDate();
    calendarView.setCurrentDate(calendarDay);
    Date date = calendarDay.getDate();

    calendarView.setOnDateChangedListener(new OnDateSelectedListener() {
        @Override
        public void onDateSelected(@NonNull MaterialCalendarView widget, @NonNull CalendarDay date, boolean selected) {
            Intent result = new Intent();
            result.putExtra("Date", date);
            setResult(RESULT_OK, result);
            finish();
        }
    });
}
}

Error

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.smiles.bus, PID: 19691
              java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=65547, result=-1, data=Intent { (has extras) }} to activity {com.smiles.bus/com.smiles.bus.MainActivity}: java.lang.IllegalArgumentException: The string argument is null
                  at android.app.ActivityThread.deliverResults(ActivityThread.java:4998)
                  at android.app.ActivityThread.handleSendResult(ActivityThread.java:5041)
                  at android.app.ActivityThread.access$1600(ActivityThread.java:229)
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1875)
                  at android.os.Handler.dispatchMessage(Handler.java:102)
                  at android.os.Looper.loop(Looper.java:148)
                  at android.app.ActivityThread.main(ActivityThread.java:7325)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
               Caused by: java.lang.IllegalArgumentException: The string argument is null
                  at java.util.Date.parse(Date.java:384)
                  at java.util.Date.<init>(Date.java:157)
                  at com.smiles.bus.Home.onActivityResult(Home.java:129)
                  at android.support.v4.app.FragmentActivity.onActivityResult(FragmentActivity.java:156)
                  at android.app.Activity.dispatchActivityResult(Activity.java:7165)
                  at android.app.ActivityThread.deliverResults(ActivityThread.java:4994)
                  at android.app.ActivityThread.handleSendResult(ActivityThread.java:5041) 
                  at android.app.ActivityThread.access$1600(ActivityThread.java:229) 
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1875) 
                  at android.os.Handler.dispatchMessage(Handler.java:102) 
                  at android.os.Looper.loop(Looper.java:148) 
                  at android.app.ActivityThread.main(ActivityThread.java:7325) 
                  at java.lang.reflect.Method.invoke(Native Method) 
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 
Matheus Lacerda
  • 5,983
  • 11
  • 29
  • 45
  • Possible duplicate of [How do I pass data between Activities in Android application?](https://stackoverflow.com/questions/2091465/how-do-i-pass-data-between-activities-in-android-application) – AskNilesh Apr 25 '18 at 10:35
  • Please Read Question first then mark it as duplicate. I dont want to use intent. @NileshRathod –  Apr 25 '18 at 10:37
  • 1
    I think, You need to use `startActivityForResult()` and get result from DateChooser. [Try this](https://stackoverflow.com/questions/10407159/how-to-manage-startactivityforresult-on-android) – Jyoti JK Apr 25 '18 at 10:38
  • @AjayGohel please check the ans carefully in that question link https://stackoverflow.com/a/26484522/7666442 – AskNilesh Apr 25 '18 at 10:39
  • no shared preference will also not work here. Is there is any way i can pass Date using Bundle? –  Apr 25 '18 at 10:43
  • If it is possible through bundle then can u pls give me example what should write in 'calendarView.setOnDateChangedListener' and how to receive it ? –  Apr 25 '18 at 10:45
  • What is the reason for wanting to do it without an `Intent`? You have to use an `Intent` to start `DateChooser` anyway. – Mike M. Apr 25 '18 at 10:50
  • I am starting DateChooser with intent. I want to close this DateChooser and set selected date in Home fragment. So i am using finish() so DateChooser will close. Reason I am not using intent to open Home fragment is the user have to select cities again –  Apr 25 '18 at 10:54
  • Then you mean to say that you don't want to start the home `Activity` again. That's not the only thing `Intent`s are for. They are also used to pass data back to a previous `Activity`. Look at [the post Jyoti JK linked above](https://stackoverflow.com/questions/10407159/how-to-manage-startactivityforresult-on-android) to learn how to use `startActivityForResult()`. – Mike M. Apr 25 '18 at 10:57
  • But then i have to use intent again if i will use startActivityForResult –  Apr 25 '18 at 11:01
  • 1
    `startActivityForResult()` is used in the home `Activity`, not in `DateChooser`. Please try to read through that post, and consult the relevant documentation. This is all very basic stuff. – Mike M. Apr 25 '18 at 11:03

4 Answers4

0

If you want the DateChooser to be launched as an activity start it with:

Intent intent = new Intent(this, DateChooser.class);
startActivityForResult(intent, REQUEST_DATE);

and implement a reaction on the result:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  if (resultCode == RESULT_OK && requestCode == REQUEST_DATE) {
     Date date = new Date(data.getExtras().getString(EXTRA_DATE));
     ...
  }
}

Return the result in DateChooser as following:

Intent result = new Intent();
result.putExtra(EXTRA_DATE, date);
setResult(RESULT_OK, result);
finish();

Define EXTRA_DATE as string constant and REQUEST_DATE as int constant.

I have not tested correct conversion of the date value. Should just be an inspiration.

Norbert
  • 1,204
  • 1
  • 9
  • 13
0

Use this . we can pass data to anywhere without using intent and ActivityResult

otto

sample sample project

Ramkumar.M
  • 681
  • 6
  • 21
0

when you select any date take that date into String variable like below code ..

convert date object string like this..

String selectedDate=date.toString();

       Intent result = new Intent(currentclassName.this,NextActivity.class);// if you finish then no need to define parameters into intent.
        result.putExtra("Date", selectedDate);
        setResult(RESULT_OK, result);
        finish();

if pass into fragment then used argument to pass data.

0

Make use of DialogFragment instead of an Activity.

Try to add this MyDatePickerFragment within your MainActivity or whatever.

public class MyDatePickerFragment extends DialogFragment {

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        final Calendar c = Calendar.getInstance();
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH);
        int day = c.get(Calendar.DAY_OF_MONTH);
        DatePickerDialog datePickerDialog = new DatePickerDialog(getActivity(), dateSetListener, year, month, day);
        datePickerDialog.getDatePicker().setMinDate(System.currentTimeMillis());
        return datePickerDialog;
    }

    private DatePickerDialog.OnDateSetListener dateSetListener =
            new DatePickerDialog.OnDateSetListener() {
                public void onDateSet(DatePicker view, int year, int month, int day) {
                    String selectedDate = view.getDayOfMonth() + "/" + (view.getMonth() + 1) + "/" + view.getYear();
                    txtDate.setText(selectedDate);
                }
            };
}

And call the fragment from your onclick()

DialogFragment newFragment = new MyDatePickerFragment();
newFragment.show(getFragmentManager(), "date picker");
Archana
  • 597
  • 4
  • 18