From 10c1bf76b674872966693c80416c5f02ebb5a466 Mon Sep 17 00:00:00 2001 From: Twig N Date: Sat, 18 Jan 2020 20:45:20 +1100 Subject: [PATCH] - fix crashes on activity recreation because android is just awful --- .../core/gui/remoterss/RemoteRssFragment.java | 15 ++++++- .../core/gui/rss/RssfeedsActivity.java | 45 ++++++++++++++----- .../core/gui/rss/RssfeedsFragment.java | 16 ++++++- 3 files changed, 63 insertions(+), 13 deletions(-) diff --git a/app/src/main/java/org/transdroid/core/gui/remoterss/RemoteRssFragment.java b/app/src/main/java/org/transdroid/core/gui/remoterss/RemoteRssFragment.java index e73de91c..022a72e6 100644 --- a/app/src/main/java/org/transdroid/core/gui/remoterss/RemoteRssFragment.java +++ b/app/src/main/java/org/transdroid/core/gui/remoterss/RemoteRssFragment.java @@ -75,10 +75,23 @@ public class RemoteRssFragment extends Fragment { torrentsList.setFastScrollEnabled(true); } + protected RssfeedsActivity getRssActivity() { + return (RssfeedsActivity) getActivity(); + } + @Override public void onResume() { super.onResume(); - ((RssfeedsActivity) getActivity()).refreshRemoteFeeds(); + + getRssActivity().onFragmentReady(this); + getRssActivity().refreshRemoteFeeds(); + } + + @Override + public void onDestroy() { + super.onDestroy(); + + getRssActivity().onFragmentDestroy(this); } /** diff --git a/app/src/main/java/org/transdroid/core/gui/rss/RssfeedsActivity.java b/app/src/main/java/org/transdroid/core/gui/rss/RssfeedsActivity.java index e5671ded..7cd4ac34 100644 --- a/app/src/main/java/org/transdroid/core/gui/rss/RssfeedsActivity.java +++ b/app/src/main/java/org/transdroid/core/gui/rss/RssfeedsActivity.java @@ -84,7 +84,7 @@ public class RssfeedsActivity extends AppCompatActivity { protected ApplicationSettings applicationSettings; // Contained feeds and items fragments - protected RssfeedsFragment fragmentFeeds; + protected RssfeedsFragment fragmentLocalFeeds; protected RemoteRssFragment fragmentRemoteFeeds; @FragmentById(R.id.rssitems_fragment) @@ -120,10 +120,10 @@ public class RssfeedsActivity extends AppCompatActivity { Fragment fragment = null; if (position == RSS_FEEDS_LOCAL) { - fragment = fragmentFeeds; + fragment = RssfeedsFragment_.builder().build(); } else if (position == RSS_FEEDS_REMOTE) { - fragment = fragmentRemoteFeeds; + fragment = RemoteRssFragment_.builder().build(); } return fragment; @@ -163,15 +163,30 @@ public class RssfeedsActivity extends AppCompatActivity { getSupportActionBar().setTitle(NavigationHelper.buildCondensedFontString(getString(R.string.rss_feeds))); getSupportActionBar().setDisplayHomeAsUpEnabled(true); - fragmentFeeds = RssfeedsFragment_.builder().build(); - fragmentRemoteFeeds = RemoteRssFragment_.builder().build(); - PagerAdapter pagerAdapter = new PagerAdapter(getSupportFragmentManager()); viewPager.setAdapter(pagerAdapter); tabLayout.setupWithViewPager(viewPager); viewPager.setCurrentItem(0); } + public void onFragmentReady(Fragment fragment) { + if (fragment instanceof RssfeedsFragment) { + fragmentLocalFeeds = (RssfeedsFragment) fragment; + } + else if (fragment instanceof RemoteRssFragment) { + fragmentRemoteFeeds = (RemoteRssFragment) fragment; + } + } + + public void onFragmentDestroy(Fragment fragment) { + if (fragment instanceof RssfeedsFragment) { + fragmentLocalFeeds = null; + } + else if (fragment instanceof RemoteRssFragment) { + fragmentRemoteFeeds = null; + } + } + @TargetApi(Build.VERSION_CODES.HONEYCOMB) @OptionsItem(android.R.id.home) protected void navigateUp() { @@ -190,7 +205,10 @@ public class RssfeedsActivity extends AppCompatActivity { loaders.add(loader); loadRssfeed(loader); } - fragmentFeeds.update(loaders); + + if (fragmentLocalFeeds != null) { + fragmentLocalFeeds.update(loaders); + } } /** @@ -221,7 +239,10 @@ public class RssfeedsActivity extends AppCompatActivity { @UiThread protected void handleRssfeedResult(RssfeedLoader loader, Channel channel, boolean hasError) { loader.update(channel, hasError); - fragmentFeeds.notifyDataSetChanged(); + + if (fragmentLocalFeeds != null) { + fragmentLocalFeeds.notifyDataSetChanged(); + } } /** @@ -324,9 +345,11 @@ public class RssfeedsActivity extends AppCompatActivity { return; } - fragmentRemoteFeeds.updateRemoteItems( + if (fragmentRemoteFeeds != null) { + fragmentRemoteFeeds.updateRemoteItems( selectedFilter == 0 ? recentItems : feeds.get(selectedFilter -1).getItems(), false /* allow android to restore scroll position */ ); + } showRemoteChannelFilters(); } @@ -398,6 +421,8 @@ public class RssfeedsActivity extends AppCompatActivity { }); feedLabels.addAll(feeds); - fragmentRemoteFeeds.updateChannelFilters(feedLabels); + if (fragmentRemoteFeeds != null) { + fragmentRemoteFeeds.updateChannelFilters(feedLabels); + } } } diff --git a/app/src/main/java/org/transdroid/core/gui/rss/RssfeedsFragment.java b/app/src/main/java/org/transdroid/core/gui/rss/RssfeedsFragment.java index d7025ec7..2ad23339 100644 --- a/app/src/main/java/org/transdroid/core/gui/rss/RssfeedsFragment.java +++ b/app/src/main/java/org/transdroid/core/gui/rss/RssfeedsFragment.java @@ -77,20 +77,32 @@ public class RssfeedsFragment extends Fragment { MainSettingsActivity_.intent(getActivity()).start(); } + protected RssfeedsActivity getRssActivity() { + return (RssfeedsActivity) getActivity(); + } + @Override public void onResume() { super.onResume(); + getRssActivity().onFragmentReady(this); this.refreshScreen(); } + @Override + public void onDestroy() { + super.onDestroy(); + + getRssActivity().onFragmentDestroy(this); + } + @OptionsItem(R.id.action_refresh) protected void refreshScreen() { - ((RssfeedsActivity) getActivity()).refreshFeeds(); + getRssActivity().refreshFeeds(); } @ItemClick(R.id.rssfeeds_list) protected void onFeedClicked(RssfeedLoader loader) { - ((RssfeedsActivity) getActivity()).openRssfeed(loader, true); + getRssActivity().openRssfeed(loader, true); } /**