From 7f0265fe242182da60ca42d3aaea60f24b6cfb63 Mon Sep 17 00:00:00 2001 From: sk Date: Sat, 21 Jan 2023 19:22:23 +0100 Subject: [PATCH] work around black screen opening notifs closes sk22#342 --- .../joinmastodon/android/MainActivity.java | 41 +++++++++++++++---- 1 file changed, 34 insertions(+), 7 deletions(-) diff --git a/mastodon/src/main/java/org/joinmastodon/android/MainActivity.java b/mastodon/src/main/java/org/joinmastodon/android/MainActivity.java index eab161889..99ea1eb4a 100644 --- a/mastodon/src/main/java/org/joinmastodon/android/MainActivity.java +++ b/mastodon/src/main/java/org/joinmastodon/android/MainActivity.java @@ -39,12 +39,13 @@ public class MainActivity extends FragmentStackActivity{ AccountSession session; Bundle args=new Bundle(); Intent intent=getIntent(); - if(intent.getBooleanExtra("fromNotification", false)){ + boolean fromNotification = intent.getBooleanExtra("fromNotification", false); + boolean hasNotification = intent.hasExtra("notification"); + if(fromNotification){ String accountID=intent.getStringExtra("accountID"); try{ session=AccountSessionManager.getInstance().getAccount(accountID); - if(!intent.hasExtra("notification")) - args.putString("tab", "notifications"); + if(!hasNotification) args.putString("tab", "notifications"); }catch(IllegalStateException x){ session=AccountSessionManager.getInstance().getLastActiveAccount(); } @@ -54,13 +55,13 @@ public class MainActivity extends FragmentStackActivity{ args.putString("account", session.getID()); Fragment fragment=session.activated ? new HomeFragment() : new AccountActivationFragment(); fragment.setArguments(args); - showFragmentClearingBackStack(fragment); - if(intent.getBooleanExtra("fromNotification", false) && intent.hasExtra("notification")){ + if(fromNotification && hasNotification){ Notification notification=Parcels.unwrap(intent.getParcelableExtra("notification")); showFragmentForNotification(notification, session.getID()); - }else if(intent.getBooleanExtra("compose", false)){ + } else if (intent.getBooleanExtra("compose", false)){ showCompose(); - }else{ + } else { + showFragmentClearingBackStack(fragment); maybeRequestNotificationsPermission(); } } @@ -139,4 +140,30 @@ public class MainActivity extends FragmentStackActivity{ requestPermissions(new String[]{Manifest.permission.POST_NOTIFICATIONS}, 100); } } + + /** + * when opening app through a notification: if (thread) fragment "can go back", clear back stack + * and show home fragment. upstream's implementation doesn't require this as it opens home first + * and then immediately switches to the notification's ThreadFragment. this causes a black + * screen in megalodon, for some reason, so i'm working around this that way. + */ + @Override + public void onBackPressed() { + Fragment currentFragment = getFragmentManager().findFragmentById( + (fragmentContainers.get(fragmentContainers.size() - 1)).getId() + ); + Bundle currentArgs = currentFragment.getArguments(); + if (this.fragmentContainers.size() == 1 + && currentArgs.getBoolean("_can_go_back", false) + && currentArgs.containsKey("account")) { + Bundle args = new Bundle(); + args.putString("account", currentArgs.getString("account")); + args.putString("tab", "notifications"); + Fragment fragment=new HomeFragment(); + fragment.setArguments(args); + showFragmentClearingBackStack(fragment); + } else { + super.onBackPressed(); + } + } }