Skip to content

Commit 341104e

Browse files
Use getOnBackPressedDispatcher().addCallback in CustomizeThemeActivity.
1 parent 5fe3041 commit 341104e

File tree

5 files changed

+55
-60
lines changed

5 files changed

+55
-60
lines changed

app/src/main/java/ml/docilealligator/infinityforreddit/activities/BaseActivity.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -523,4 +523,8 @@ public void copyLink(String link) {
523523
Toast.makeText(this, R.string.copy_link_failed, Toast.LENGTH_SHORT).show();
524524
}
525525
}
526+
527+
public void triggerBackPress() {
528+
getOnBackPressedDispatcher().onBackPressed();
529+
}
526530
}

app/src/main/java/ml/docilealligator/infinityforreddit/activities/CommentActivity.java

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,19 @@ public void onSelectGiphyGif() {
389389
getOnBackPressedDispatcher().addCallback(this, new OnBackPressedCallback(true) {
390390
@Override
391391
public void handleOnBackPressed() {
392-
handleBackPress();
392+
if (isSubmitting) {
393+
promptAlertDialog(R.string.exit_when_submit, R.string.exit_when_edit_comment_detail, false);
394+
} else {
395+
if (binding.commentCommentEditText.getText().toString().isEmpty()) {
396+
commentActivityViewModel.deleteCommentDraft(parentFullname, () -> {
397+
setEnabled(false);
398+
triggerBackPress();
399+
return Unit.INSTANCE;
400+
});
401+
} else {
402+
promptAlertDialog(R.string.save_comment_draft, R.string.save_comment_draft_detail, true);
403+
}
404+
}
393405
}
394406
});
395407
}
@@ -479,7 +491,7 @@ public boolean onCreateOptionsMenu(Menu menu) {
479491
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
480492
int itemId = item.getItemId();
481493
if (itemId == android.R.id.home) {
482-
getOnBackPressedDispatcher().onBackPressed();
494+
triggerBackPress();
483495
return true;
484496
} else if (itemId == R.id.action_preview_comment_activity) {
485497
Intent intent = new Intent(this, FullMarkdownActivity.class);
@@ -600,21 +612,6 @@ protected void onActivityResult(int requestCode, int resultCode, @Nullable Inten
600612
}
601613
}
602614

603-
private void handleBackPress() {
604-
if (isSubmitting) {
605-
promptAlertDialog(R.string.exit_when_submit, R.string.exit_when_edit_comment_detail, false);
606-
} else {
607-
if (binding.commentCommentEditText.getText().toString().isEmpty()) {
608-
commentActivityViewModel.deleteCommentDraft(parentFullname, () -> {
609-
finish();
610-
return Unit.INSTANCE;
611-
});
612-
} else {
613-
promptAlertDialog(R.string.save_comment_draft, R.string.save_comment_draft_detail, true);
614-
}
615-
}
616-
}
617-
618615
@Override
619616
protected void onDestroy() {
620617
super.onDestroy();

app/src/main/java/ml/docilealligator/infinityforreddit/activities/CustomizeThemeActivity.java

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import android.view.View;
1111
import android.widget.Toast;
1212

13+
import androidx.activity.OnBackPressedCallback;
1314
import androidx.annotation.NonNull;
1415
import androidx.core.graphics.Insets;
1516
import androidx.core.view.OnApplyWindowInsetsListener;
@@ -253,6 +254,21 @@ public void onFailure(@NonNull Call<String> call, @NonNull Throwable throwable)
253254
binding.recyclerViewCustomizeThemeActivity.setAdapter(adapter);
254255
adapter.setCustomThemeSettingsItem(customThemeSettingsItems);
255256
}
257+
258+
getOnBackPressedDispatcher().addCallback(this, new OnBackPressedCallback(true) {
259+
@Override
260+
public void handleOnBackPressed() {
261+
new MaterialAlertDialogBuilder(CustomizeThemeActivity.this, R.style.MaterialAlertDialogTheme)
262+
.setTitle(R.string.discard)
263+
.setPositiveButton(R.string.discard_dialog_button, (dialogInterface, i)
264+
-> {
265+
setEnabled(false);
266+
triggerBackPress();
267+
})
268+
.setNegativeButton(R.string.no, null)
269+
.show();
270+
}
271+
});
256272
}
257273

258274
@Override
@@ -406,16 +422,6 @@ protected void onSaveInstanceState(@NonNull Bundle outState) {
406422
}
407423
}
408424

409-
@Override
410-
public void onBackPressed() {
411-
new MaterialAlertDialogBuilder(this, R.style.MaterialAlertDialogTheme)
412-
.setTitle(R.string.discard)
413-
.setPositiveButton(R.string.discard_dialog_button, (dialogInterface, i)
414-
-> super.onBackPressed())
415-
.setNegativeButton(R.string.no, null)
416-
.show();
417-
}
418-
419425
@Override
420426
public SharedPreferences getDefaultSharedPreferences() {
421427
return sharedPreferences;

app/src/main/java/ml/docilealligator/infinityforreddit/activities/MainActivity.java

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,26 @@ public void onDrawerClosed(View drawerView) {
401401
getOnBackPressedDispatcher().addCallback(this, new OnBackPressedCallback(true) {
402402
@Override
403403
public void handleOnBackPressed() {
404-
handleBackPress();
404+
if (binding.drawerLayout.isOpen()) {
405+
binding.drawerLayout.close();
406+
} else {
407+
if (mBackButtonAction == SharedPreferencesUtils.MAIN_PAGE_BACK_BUTTON_ACTION_CONFIRM_EXIT) {
408+
new MaterialAlertDialogBuilder(MainActivity.this, R.style.MaterialAlertDialogTheme)
409+
.setTitle(R.string.exit_app)
410+
.setPositiveButton(R.string.yes, (dialogInterface, i)
411+
-> {
412+
setEnabled(false);
413+
triggerBackPress();
414+
})
415+
.setNegativeButton(R.string.no, null)
416+
.show();
417+
} else if (mBackButtonAction == SharedPreferencesUtils.MAIN_PAGE_BACK_BUTTON_ACTION_OPEN_NAVIGATION_DRAWER) {
418+
binding.drawerLayout.open();
419+
} else {
420+
setEnabled(false);
421+
triggerBackPress();
422+
}
423+
}
405424
}
406425
});
407426

@@ -1271,25 +1290,6 @@ public boolean onOptionsItemSelected(@NonNull MenuItem item) {
12711290
return false;
12721291
}
12731292

1274-
private void handleBackPress() {
1275-
if (binding.drawerLayout.isOpen()) {
1276-
binding.drawerLayout.close();
1277-
} else {
1278-
if (mBackButtonAction == SharedPreferencesUtils.MAIN_PAGE_BACK_BUTTON_ACTION_CONFIRM_EXIT) {
1279-
new MaterialAlertDialogBuilder(this, R.style.MaterialAlertDialogTheme)
1280-
.setTitle(R.string.exit_app)
1281-
.setPositiveButton(R.string.yes, (dialogInterface, i)
1282-
-> finish())
1283-
.setNegativeButton(R.string.no, null)
1284-
.show();
1285-
} else if (mBackButtonAction == SharedPreferencesUtils.MAIN_PAGE_BACK_BUTTON_ACTION_OPEN_NAVIGATION_DRAWER) {
1286-
binding.drawerLayout.open();
1287-
} else {
1288-
finish();
1289-
}
1290-
}
1291-
}
1292-
12931293
@Override
12941294
public boolean onKeyDown(int keyCode, KeyEvent event) {
12951295
if (sectionsPagerAdapter != null) {

app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewPostDetailActivity.java

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,6 @@ public class ViewPostDetailActivity extends BaseActivity implements SortTypeSele
156156
private SectionsPagerAdapter mSectionsPagerAdapter;
157157
private long mPostFragmentId;
158158
private int mPostListPosition;
159-
private int mOrientation;
160159
private boolean mVolumeKeysNavigateComments;
161160
private boolean mIsNsfwSubreddit;
162161
private boolean mHideFab;
@@ -269,8 +268,6 @@ public WindowInsetsCompat onApplyWindowInsets(@NonNull View v, @NonNull WindowIn
269268
post = getIntent().getParcelableExtra(EXTRA_POST_DATA);
270269
}
271270

272-
mOrientation = getResources().getConfiguration().orientation;
273-
274271
binding.toolbarViewPostDetailActivity.setTitle("");
275272
setSupportActionBar(binding.toolbarViewPostDetailActivity);
276273
setToolbarGoToTop(binding.toolbarViewPostDetailActivity);
@@ -826,7 +823,7 @@ public boolean onCreateOptionsMenu(Menu menu) {
826823
@Override
827824
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
828825
if (item.getItemId() == android.R.id.home) {
829-
onBackPressed();
826+
triggerBackPress();
830827
return true;
831828
} else if (item.getItemId() == R.id.action_reset_fab_position_view_post_detail_activity) {
832829
binding.fabViewPostDetailActivity.resetCoordinates();
@@ -883,15 +880,6 @@ protected void onSaveInstanceState(@NonNull Bundle outState) {
883880
Bridge.saveInstanceState(this, outState);
884881
}
885882

886-
@Override
887-
public void onBackPressed() {
888-
if (mOrientation == getResources().getConfiguration().orientation) {
889-
super.onBackPressed();
890-
} else {
891-
finish();
892-
}
893-
}
894-
895883
@Override
896884
protected void onDestroy() {
897885
EventBus.getDefault().unregister(this);

0 commit comments

Comments
 (0)