Skip to content
Snippets Groups Projects
Commit 74605c7e authored by COUCHET Thibaud's avatar COUCHET Thibaud
Browse files

Intent implicite pour DatePicker fix

parent 8c5d96bb
Branches
No related tags found
No related merge requests found
...@@ -29,7 +29,7 @@ ...@@ -29,7 +29,7 @@
<activity android:name=".DisplayUserActivity" android:exported="true" /> <activity android:name=".DisplayUserActivity" android:exported="true" />
<activity android:name=".DatePickerActivity" android:exported="true"> <activity android:name=".DatePickerActivity" android:exported="true">
<intent-filter> <intent-filter>
<action android:name="android.intent.action.PICK" /> <action android:name="fr.imt_atlantique.myfirstapplication.ACTION_PICK_DATE" />
<category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.DEFAULT" />
</intent-filter> </intent-filter>
</activity> </activity>
......
...@@ -41,14 +41,12 @@ public class DatePickerActivity extends AppCompatActivity { ...@@ -41,14 +41,12 @@ public class DatePickerActivity extends AppCompatActivity {
int day = datePicker.getDayOfMonth(); int day = datePicker.getDayOfMonth();
int month = datePicker.getMonth() + 1; int month = datePicker.getMonth() + 1;
int year = datePicker.getYear(); int year = datePicker.getYear();
LocalDate date = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
date = LocalDate.of(year, month, day);
}
fr.imt_atlantique.myfirstapplication.DatePicker dateObject = new fr.imt_atlantique.myfirstapplication.DatePicker(date);
Intent resultIntent = new Intent(); Intent resultIntent = new Intent();
resultIntent.putExtra("date", dateObject); resultIntent.putExtra("day", day);
resultIntent.putExtra("month", month);
resultIntent.putExtra("year", year);
setResult(RESULT_OK, resultIntent); setResult(RESULT_OK, resultIntent);
finish(); finish();
}); });
...@@ -58,17 +56,21 @@ public class DatePickerActivity extends AppCompatActivity { ...@@ -58,17 +56,21 @@ public class DatePickerActivity extends AppCompatActivity {
setResult(RESULT_CANCELED); setResult(RESULT_CANCELED);
finish(); finish();
}); });
} }
private void getValues() { private void getValues() {
Intent intent = getIntent(); Intent intent = getIntent();
if (intent != null) { if (intent != null) {
fr.imt_atlantique.myfirstapplication.DatePicker date = intent.getParcelableExtra("date"); int day = intent.getIntExtra("day", -1);
if (date != null) { int month = intent.getIntExtra("month", -1);
this.date = date.getDate(); int year = intent.getIntExtra("year", -1);
if (day != -1 && month != -1 && year != -1) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
datePicker.updateDate(this.date.getYear(), this.date.getMonthValue() - 1, this.date.getDayOfMonth()); date = LocalDate.of(year, month, day);
datePicker.updateDate(year, month - 1, day);
} }
} }
} }
......
...@@ -38,7 +38,7 @@ import java.util.Set; ...@@ -38,7 +38,7 @@ import java.util.Set;
public class MainActivity extends AppCompatActivity { public class MainActivity extends AppCompatActivity {
String firstName, lastName, birthCity; String firstName, lastName, birthCity;
String birthDate = getString(R.string.pick_date_hint); String birthDate;
int birthDept = 0; int birthDept = 0;
ArrayList<String> phoneNumbersList = new ArrayList<>(); ArrayList<String> phoneNumbersList = new ArrayList<>();
...@@ -59,7 +59,7 @@ public class MainActivity extends AppCompatActivity { ...@@ -59,7 +59,7 @@ public class MainActivity extends AppCompatActivity {
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom); v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
return insets; return insets;
}); });
birthDate = getString(R.string.pick_date_hint);
findAllViewById(); findAllViewById();
birthDateButton.setOnClickListener(view -> showDatePicker()); birthDateButton.setOnClickListener(view -> showDatePicker());
restorePreferences(); restorePreferences();
...@@ -347,15 +347,22 @@ public class MainActivity extends AppCompatActivity { ...@@ -347,15 +347,22 @@ public class MainActivity extends AppCompatActivity {
} }
public void showDatePicker() { public void showDatePicker() {
Intent intent = new Intent(MainActivity.this, DatePickerActivity.class); Intent intent = new Intent("fr.imt_atlantique.myfirstapplication.ACTION_PICK_DATE");
if (!birthDate.equals(getString(R.string.pick_date_hint))) { if (!birthDate.equals(getString(R.string.pick_date_hint))) {
LocalDate date = null; int day = 0, month = 0, year = 0;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
date = LocalDate.parse(birthDate, DateTimeFormatter.ofPattern("d/M/yyyy")); LocalDate date = LocalDate.parse(birthDate, DateTimeFormatter.ofPattern("d/M/yyyy"));
day = date.getDayOfMonth();
month = date.getMonthValue();
year = date.getYear();
} }
fr.imt_atlantique.myfirstapplication.DatePicker dateObject = new fr.imt_atlantique.myfirstapplication.DatePicker(date);
intent.putExtra("date", dateObject); // Instead of sending Parcelable, send date as primitives
intent.putExtra("day", day);
intent.putExtra("month", month);
intent.putExtra("year", year);
} }
datePickerLauncher.launch(intent); datePickerLauncher.launch(intent);
...@@ -365,10 +372,13 @@ public class MainActivity extends AppCompatActivity { ...@@ -365,10 +372,13 @@ public class MainActivity extends AppCompatActivity {
new ActivityResultContracts.StartActivityForResult(), new ActivityResultContracts.StartActivityForResult(),
result -> { result -> {
if (result.getResultCode() == RESULT_OK && result.getData() != null) { if (result.getResultCode() == RESULT_OK && result.getData() != null) {
fr.imt_atlantique.myfirstapplication.DatePicker date = result.getData().getParcelableExtra("date"); int day = result.getData().getIntExtra("day", -1);
if (date != null) { int month = result.getData().getIntExtra("month", -1);
int year = result.getData().getIntExtra("year", -1);
if (day != -1 && month != -1 && year != -1) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
birthDate = date.getDate().format(DateTimeFormatter.ofPattern("d/M/yyyy")); birthDate = LocalDate.of(year, month, day).format(DateTimeFormatter.ofPattern("d/M/yyyy"));
} }
birthDateButton.setText(birthDate); birthDateButton.setText(birthDate);
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment