-2

I have a MainActivity and a CalendarActivity. My application's MainActivity should display always another informations based on the chosen date. If someone change the date in the CalendarActivity CalendarView, another info should appear in MainActivity's TextBox + it should remember what was the last chosen date, even if the user close the application and later opens it.

  1. How am I supposed to get the date from CalendarActivity's CalendarView in the MainActivity?
  2. How am I supposed to save the last chosen date, and when the application starts second time, the last chosen date will be highlighted in the CalendarView?

Any help would be appreciated. Thanks.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Laklam
  • 19
  • 3

2 Answers2

0

I have this in CalendarActivity:

public String getDate() {
    CalendarView simpleCalendarView = (CalendarView) findViewById(R.id.calendarView);
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
    String date = sdf.format(new Date(simpleCalendarView.getDate()));

    return date;
}

What works perfectly. I put this in MainActivity:

public void saveDate() {
        CalendarActivity ca = new CalendarActivity();
        String date = ca.getDate();
        Log.e("DATE DAYA DAYA", date);
}

Button b = (Button) findViewById(R.id.button);
b.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        saveDate();
    }
});

If I press the button I get errors and the app closes.

Laklam
  • 19
  • 3
0

Store Last date selected in SharedPreferences.SharedPreferences store data in key-value format. You can retrieve data stored in SharedPreferences any time. And Data will be saved even you close app. For more information check this link https://developer.android.com/training/data-storage/shared-preferences#java

Avinash Mali
  • 101
  • 1
  • 4