diff options
author | Alex Ning <chineseperson5@gmail.com> | 2019-08-19 15:58:35 +0000 |
---|---|---|
committer | Alex Ning <chineseperson5@gmail.com> | 2019-08-19 15:58:35 +0000 |
commit | 01f5dd7886e25fd05b966944771bf3b86f54c8bd (patch) | |
tree | 646b7f425810acf8243de2b0062bb88f64ad748e /app/src/main/java | |
parent | 556047aa42b8fba25be56c38e5e919690444858b (diff) | |
download | infinity-for-reddit-01f5dd7886e25fd05b966944771bf3b86f54c8bd.tar infinity-for-reddit-01f5dd7886e25fd05b966944771bf3b86f54c8bd.tar.gz infinity-for-reddit-01f5dd7886e25fd05b966944771bf3b86f54c8bd.tar.bz2 infinity-for-reddit-01f5dd7886e25fd05b966944771bf3b86f54c8bd.tar.lz infinity-for-reddit-01f5dd7886e25fd05b966944771bf3b86f54c8bd.tar.xz infinity-for-reddit-01f5dd7886e25fd05b966944771bf3b86f54c8bd.tar.zst infinity-for-reddit-01f5dd7886e25fd05b966944771bf3b86f54c8bd.zip |
Finish all the activities after switching account by view another account's messages.
Diffstat (limited to '')
20 files changed, 250 insertions, 10 deletions
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/AccountPostsActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/AccountPostsActivity.java index d366891d..3acfd94b 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/AccountPostsActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/AccountPostsActivity.java @@ -19,6 +19,9 @@ import androidx.fragment.app.Fragment; import com.google.android.material.appbar.AppBarLayout; import com.google.android.material.appbar.CollapsingToolbarLayout; +import org.greenrobot.eventbus.EventBus; +import org.greenrobot.eventbus.Subscribe; + import javax.inject.Inject; import butterknife.BindView; @@ -62,6 +65,8 @@ public class AccountPostsActivity extends AppCompatActivity implements UserThing ((Infinity) getApplication()).getAppComponent().inject(this); + EventBus.getDefault().register(this); + Resources resources = getResources(); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1 @@ -234,9 +239,20 @@ public class AccountPostsActivity extends AppCompatActivity implements UserThing } @Override + protected void onDestroy() { + super.onDestroy(); + EventBus.getDefault().unregister(this); + } + + @Override public void userThingSortTypeSelected(String sortType) { if(mFragment != null) { ((PostFragment) mFragment).changeSortType(sortType); } } + + @Subscribe + public void onAccountSwitchEvent(SwitchAccountEvent event) { + finish(); + } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/EditCommentActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/EditCommentActivity.java index 11e30fd8..db413238 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/EditCommentActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/EditCommentActivity.java @@ -19,6 +19,9 @@ import androidx.core.content.ContextCompat; import com.google.android.material.snackbar.Snackbar; +import org.greenrobot.eventbus.EventBus; +import org.greenrobot.eventbus.Subscribe; + import java.util.HashMap; import java.util.Map; @@ -63,6 +66,8 @@ public class EditCommentActivity extends AppCompatActivity { ((Infinity) getApplication()).getAppComponent().inject(this); + EventBus.getDefault().register(this); + if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { Window window = getWindow(); if((getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK) != Configuration.UI_MODE_NIGHT_YES) { @@ -134,4 +139,15 @@ public class EditCommentActivity extends AppCompatActivity { } return false; } + + @Override + protected void onDestroy() { + super.onDestroy(); + EventBus.getDefault().unregister(this); + } + + @Subscribe + public void onAccountSwitchEvent(SwitchAccountEvent event) { + finish(); + } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/EditPostActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/EditPostActivity.java index 7213522a..a3ab5efe 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/EditPostActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/EditPostActivity.java @@ -20,6 +20,9 @@ import androidx.core.content.ContextCompat; import com.google.android.material.snackbar.Snackbar; +import org.greenrobot.eventbus.EventBus; +import org.greenrobot.eventbus.Subscribe; + import java.util.HashMap; import java.util.Map; @@ -62,6 +65,8 @@ public class EditPostActivity extends AppCompatActivity { ((Infinity) getApplication()).getAppComponent().inject(this); + EventBus.getDefault().register(this); + if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { Window window = getWindow(); if((getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK) != Configuration.UI_MODE_NIGHT_YES) { @@ -124,4 +129,15 @@ public class EditPostActivity extends AppCompatActivity { } return false; } + + @Override + protected void onDestroy() { + super.onDestroy(); + EventBus.getDefault().unregister(this); + } + + @Subscribe + public void onAccountSwitchEvent(SwitchAccountEvent event) { + finish(); + } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/FilteredThingActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/FilteredThingActivity.java index caeb7ef0..7a5469b3 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/FilteredThingActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/FilteredThingActivity.java @@ -18,6 +18,9 @@ import androidx.fragment.app.Fragment; import com.google.android.material.appbar.AppBarLayout; +import org.greenrobot.eventbus.EventBus; +import org.greenrobot.eventbus.Subscribe; + import javax.inject.Inject; import butterknife.BindView; @@ -65,6 +68,8 @@ public class FilteredThingActivity extends AppCompatActivity implements SortType ((Infinity) getApplication()).getAppComponent().inject(this); + EventBus.getDefault().register(this); + Resources resources = getResources(); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1 @@ -269,6 +274,12 @@ public class FilteredThingActivity extends AppCompatActivity implements SortType } @Override + protected void onDestroy() { + super.onDestroy(); + EventBus.getDefault().unregister(this); + } + + @Override public void searchSortTypeSelected(String sortType) { ((PostFragment)mFragment).changeSortType(sortType); } @@ -282,4 +293,9 @@ public class FilteredThingActivity extends AppCompatActivity implements SortType public void userThingSortTypeSelected(String sortType) { ((PostFragment)mFragment).changeSortType(sortType); } + + @Subscribe + public void onAccountSwitchEvent(SwitchAccountEvent event) { + finish(); + } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java index ee0f4792..65530111 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/MainActivity.java @@ -252,6 +252,7 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe if(mNewAccountName != null) { if(account == null || !account.getUsername().equals(mNewAccountName)) { new SwitchAccountAsyncTask(mRedditDataRoomDatabase, mNewAccountName, newAccount -> { + EventBus.getDefault().post(new SwitchAccountEvent(getClass().getName())); mNewAccountName = null; if(newAccount == null) { mNullAccessToken = true; @@ -272,7 +273,7 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe .build(); WorkManager.getInstance(this).enqueueUniquePeriodicWork(PullNotificationWorker.WORKER_TAG, - ExistingPeriodicWorkPolicy.REPLACE, pullNotificationRequest); + ExistingPeriodicWorkPolicy.KEEP, pullNotificationRequest); } bindView(); @@ -294,7 +295,7 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe .build(); WorkManager.getInstance(this).enqueueUniquePeriodicWork(PullNotificationWorker.WORKER_TAG, - ExistingPeriodicWorkPolicy.REPLACE, pullNotificationRequest); + ExistingPeriodicWorkPolicy.KEEP, pullNotificationRequest); bindView(); } @@ -318,7 +319,7 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe .build(); WorkManager.getInstance(this).enqueueUniquePeriodicWork(PullNotificationWorker.WORKER_TAG, - ExistingPeriodicWorkPolicy.REPLACE, pullNotificationRequest); + ExistingPeriodicWorkPolicy.KEEP, pullNotificationRequest); } bindView(); @@ -327,7 +328,6 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe } private void bindView() { - sectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager()); viewPager.setAdapter(sectionsPagerAdapter); viewPager.setOffscreenPageLimit(2); @@ -733,7 +733,9 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe @Subscribe public void onAccountSwitchEvent(SwitchAccountEvent event) { - finish(); + if(!getClass().getName().equals(event.excludeActivityClassName)) { + finish(); + } } private class SectionsPagerAdapter extends FragmentPagerAdapter { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostImageActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostImageActivity.java index 67bcd4ed..a4080fcf 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostImageActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostImageActivity.java @@ -475,6 +475,11 @@ public class PostImageActivity extends AppCompatActivity implements FlairBottomS } @Subscribe + public void onAccountSwitchEvent(SwitchAccountEvent event) { + finish(); + } + + @Subscribe public void onSubmitImagePostEvent(SubmitImagePostEvent submitImagePostEvent) { isPosting = false; if(submitImagePostEvent.postSuccess) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostLinkActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostLinkActivity.java index 7643ec07..34bf0236 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostLinkActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostLinkActivity.java @@ -380,6 +380,11 @@ public class PostLinkActivity extends AppCompatActivity implements FlairBottomSh } @Subscribe + public void onAccountSwitchEvent(SwitchAccountEvent event) { + finish(); + } + + @Subscribe public void onSubmitLinkPostEvent(SubmitTextOrLinkPostEvent submitTextOrLinkPostEvent) { isPosting = false; if(submitTextOrLinkPostEvent.postSuccess) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostTextActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostTextActivity.java index 105da07f..815e12a8 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostTextActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostTextActivity.java @@ -376,6 +376,11 @@ public class PostTextActivity extends AppCompatActivity implements FlairBottomSh } @Subscribe + public void onAccountSwitchEvent(SwitchAccountEvent event) { + finish(); + } + + @Subscribe public void onSubmitTextPostEvent(SubmitTextOrLinkPostEvent submitTextOrLinkPostEvent) { isPosting = false; if(submitTextOrLinkPostEvent.postSuccess) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostVideoActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostVideoActivity.java index fcf3a5d4..4b0e03ef 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostVideoActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostVideoActivity.java @@ -475,6 +475,11 @@ public class PostVideoActivity extends AppCompatActivity implements FlairBottomS } @Subscribe + public void onAccountSwitchEvent(SwitchAccountEvent event) { + finish(); + } + + @Subscribe public void onSubmitVideoPostEvent(SubmitVideoPostEvent submitVideoPostEvent) { isPosting = false; if(submitVideoPostEvent.postSuccess) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/RulesActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/RulesActivity.java index 53790e32..d33b63e8 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/RulesActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/RulesActivity.java @@ -21,6 +21,8 @@ import androidx.recyclerview.widget.RecyclerView; import com.google.android.material.appbar.AppBarLayout; +import org.greenrobot.eventbus.EventBus; +import org.greenrobot.eventbus.Subscribe; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; @@ -64,6 +66,8 @@ public class RulesActivity extends AppCompatActivity { ((Infinity) getApplication()).getAppComponent().inject(this); + EventBus.getDefault().register(this); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) { Resources resources = getResources(); @@ -173,6 +177,17 @@ public class RulesActivity extends AppCompatActivity { return false; } + @Override + protected void onDestroy() { + super.onDestroy(); + EventBus.getDefault().unregister(this); + } + + @Subscribe + public void onAccountSwitchEvent(SwitchAccountEvent event) { + finish(); + } + private static class ParseRulesAsyncTask extends AsyncTask<Void, ArrayList<Rule>, ArrayList<Rule>> { private String response; private ParseRulesAsyncTaskListener parseRulesAsyncTaskListener; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SearchActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SearchActivity.java index cfade0b9..eb1c111a 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/SearchActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SearchActivity.java @@ -21,6 +21,9 @@ import androidx.core.content.ContextCompat; import com.ferfalk.simplesearchview.SimpleSearchView; +import org.greenrobot.eventbus.EventBus; +import org.greenrobot.eventbus.Subscribe; + import butterknife.BindView; import butterknife.ButterKnife; @@ -53,6 +56,8 @@ public class SearchActivity extends AppCompatActivity { ButterKnife.bind(this); + EventBus.getDefault().register(this); + if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { Window window = getWindow(); if((getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK) != Configuration.UI_MODE_NIGHT_YES) { @@ -213,4 +218,15 @@ public class SearchActivity extends AppCompatActivity { outState.putString(SUBREDDIT_NAME_STATE, subredditName); outState.putBoolean(SUBREDDIT_IS_USER_STATE, subredditIsUser); } + + @Override + protected void onDestroy() { + super.onDestroy(); + EventBus.getDefault().unregister(this); + } + + @Subscribe + public void onAccountSwitchEvent(SwitchAccountEvent event) { + finish(); + } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SearchResultActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SearchResultActivity.java index 72b6215f..56814364 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/SearchResultActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SearchResultActivity.java @@ -23,6 +23,9 @@ import androidx.viewpager.widget.ViewPager; import com.google.android.material.appbar.AppBarLayout; import com.google.android.material.tabs.TabLayout; +import org.greenrobot.eventbus.EventBus; +import org.greenrobot.eventbus.Subscribe; + import javax.inject.Inject; import butterknife.BindView; @@ -65,6 +68,8 @@ public class SearchResultActivity extends AppCompatActivity implements SearchPos ((Infinity) getApplication()).getAppComponent().inject(this); + EventBus.getDefault().register(this); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) { Resources resources = getResources(); @@ -207,6 +212,12 @@ public class SearchResultActivity extends AppCompatActivity implements SearchPos } @Override + protected void onDestroy() { + super.onDestroy(); + EventBus.getDefault().unregister(this); + } + + @Override public void searchSortTypeSelected(String sortType) { sectionsPagerAdapter.changeSortType(sortType, 0); } @@ -216,6 +227,11 @@ public class SearchResultActivity extends AppCompatActivity implements SearchPos sectionsPagerAdapter.changeSortType(sortType, fragmentPosition); } + @Subscribe + public void onAccountSwitchEvent(SwitchAccountEvent event) { + finish(); + } + private class SectionsPagerAdapter extends FragmentPagerAdapter { private PostFragment postFragment; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SearchSubredditsResultActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SearchSubredditsResultActivity.java index 2776ed38..a5059d59 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/SearchSubredditsResultActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SearchSubredditsResultActivity.java @@ -10,6 +10,9 @@ import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.widget.Toolbar; import androidx.fragment.app.Fragment; +import org.greenrobot.eventbus.EventBus; +import org.greenrobot.eventbus.Subscribe; + import javax.inject.Inject; import butterknife.BindView; @@ -46,6 +49,8 @@ public class SearchSubredditsResultActivity extends AppCompatActivity { ((Infinity) getApplication()).getAppComponent().inject(this); + EventBus.getDefault().register(this); + setSupportActionBar(toolbar); getSupportActionBar().setDisplayHomeAsUpEnabled(true); @@ -113,4 +118,15 @@ public class SearchSubredditsResultActivity extends AppCompatActivity { outState.putString(ACCESS_TOKEN_STATE, mAccessToken); outState.putString(ACCOUNT_NAME_STATE, mAccountName); } + + @Override + protected void onDestroy() { + super.onDestroy(); + EventBus.getDefault().unregister(this); + } + + @Subscribe + public void onAccountSwitchEvent(SwitchAccountEvent event) { + finish(); + } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditSelectionActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditSelectionActivity.java index e9f5e4db..2eae6426 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditSelectionActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SubredditSelectionActivity.java @@ -21,6 +21,9 @@ import androidx.fragment.app.Fragment; import com.google.android.material.appbar.AppBarLayout; +import org.greenrobot.eventbus.EventBus; +import org.greenrobot.eventbus.Subscribe; + import java.util.ArrayList; import javax.inject.Inject; @@ -75,6 +78,8 @@ public class SubredditSelectionActivity extends AppCompatActivity { ((Infinity) getApplication()).getAppComponent().inject(this); + EventBus.getDefault().register(this); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) { Resources resources = getResources(); @@ -254,4 +259,15 @@ public class SubredditSelectionActivity extends AppCompatActivity { outState.putString(ACCOUNT_NAME_STATE, mAccountName); outState.putString(ACCOUNT_PROFILE_IMAGE_URL, mAccountProfileImageUrl); } + + @Override + protected void onDestroy() { + super.onDestroy(); + EventBus.getDefault().unregister(this); + } + + @Subscribe + public void onAccountSwitchEvent(SwitchAccountEvent event) { + finish(); + } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedThingListingActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedThingListingActivity.java index ab0c03ef..1d70e9e9 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedThingListingActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SubscribedThingListingActivity.java @@ -21,6 +21,9 @@ import androidx.viewpager.widget.ViewPager; import com.google.android.material.appbar.AppBarLayout; import com.google.android.material.tabs.TabLayout; +import org.greenrobot.eventbus.EventBus; +import org.greenrobot.eventbus.Subscribe; + import java.util.ArrayList; import javax.inject.Inject; @@ -68,6 +71,8 @@ public class SubscribedThingListingActivity extends AppCompatActivity { ((Infinity) getApplication()).getAppComponent().inject(this); + EventBus.getDefault().register(this); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) { Resources resources = getResources(); @@ -167,6 +172,12 @@ public class SubscribedThingListingActivity extends AppCompatActivity { outState.putString(ACCOUNT_NAME_STATE, mAccountName); } + @Override + protected void onDestroy() { + super.onDestroy(); + EventBus.getDefault().unregister(this); + } + private void loadSubscriptions() { if (!mInsertSuccess) { FetchSubscribedThing.fetchSubscribedThing(mOauthRetrofit, mAccessToken, mAccountName, null, @@ -195,6 +206,11 @@ public class SubscribedThingListingActivity extends AppCompatActivity { } } + @Subscribe + public void onAccountSwitchEvent(SwitchAccountEvent event) { + finish(); + } + private class SectionsPagerAdapter extends FragmentPagerAdapter { public SectionsPagerAdapter(FragmentManager fm) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/SwitchAccountEvent.java b/app/src/main/java/ml/docilealligator/infinityforreddit/SwitchAccountEvent.java index 0395f913..82b73d73 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/SwitchAccountEvent.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/SwitchAccountEvent.java @@ -1,5 +1,9 @@ package ml.docilealligator.infinityforreddit; public class SwitchAccountEvent { - public void accountSwitched(){} + String excludeActivityClassName; + public SwitchAccountEvent() {} + public SwitchAccountEvent(String excludeActivityClassName) { + this.excludeActivityClassName = excludeActivityClassName; + } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewMessageActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewMessageActivity.java index 5bf78cd2..a35a9c66 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewMessageActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewMessageActivity.java @@ -29,6 +29,7 @@ import com.google.android.material.appbar.CollapsingToolbarLayout; import com.lsjwzh.widget.materialloadingprogressbar.CircleProgressBar; import org.greenrobot.eventbus.EventBus; +import org.greenrobot.eventbus.Subscribe; import javax.inject.Inject; import javax.inject.Named; @@ -80,6 +81,8 @@ public class ViewMessageActivity extends AppCompatActivity { ((Infinity) getApplication()).getAppComponent().inject(this); + EventBus.getDefault().register(this); + mGlide = Glide.with(this); Resources resources = getResources(); @@ -152,7 +155,7 @@ public class ViewMessageActivity extends AppCompatActivity { if(mNewAccountName != null) { if(account == null || !account.getUsername().equals(mNewAccountName)) { new SwitchAccountAsyncTask(mRedditDataRoomDatabase, mNewAccountName, newAccount -> { - EventBus.getDefault().post(new SwitchAccountEvent()); + EventBus.getDefault().post(new SwitchAccountEvent(getClass().getName())); mNewAccountName = null; if(newAccount == null) { mNullAccessToken = true; @@ -251,4 +254,17 @@ public class ViewMessageActivity extends AppCompatActivity { outState.putString(ACCESS_TOKEN_STATE, mAccessToken); outState.putString(NEW_ACCOUNT_NAME_STATE, mNewAccountName); } + + @Override + protected void onDestroy() { + super.onDestroy(); + EventBus.getDefault().unregister(this); + } + + @Subscribe + public void onAccountSwitchEvent(SwitchAccountEvent event) { + if(!getClass().getName().equals(event.excludeActivityClassName)) { + finish(); + } + } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java index aba1aa99..f8d2a41b 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java @@ -222,7 +222,7 @@ public class ViewPostDetailActivity extends AppCompatActivity implements FlairBo if(mNewAccountName != null) { if(account == null || !account.getUsername().equals(mNewAccountName)) { new SwitchAccountAsyncTask(mRedditDataRoomDatabase, mNewAccountName, newAccount -> { - EventBus.getDefault().post(new SwitchAccountEvent()); + EventBus.getDefault().post(new SwitchAccountEvent(getClass().getName())); mNewAccountName = null; if(newAccount == null) { mNullAccessToken = true; @@ -704,6 +704,13 @@ public class ViewPostDetailActivity extends AppCompatActivity implements FlairBo } } + @Subscribe + public void onAccountSwitchEvent(SwitchAccountEvent event) { + if(!getClass().getName().equals(event.excludeActivityClassName)) { + finish(); + } + } + @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.view_post_detail_activity, menu); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewSubredditDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewSubredditDetailActivity.java index c7ea29a5..f981c9c1 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewSubredditDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewSubredditDetailActivity.java @@ -32,6 +32,7 @@ import com.google.android.material.floatingactionbutton.FloatingActionButton; import com.google.android.material.snackbar.Snackbar; import org.greenrobot.eventbus.EventBus; +import org.greenrobot.eventbus.Subscribe; import javax.inject.Inject; import javax.inject.Named; @@ -114,6 +115,8 @@ public class ViewSubredditDetailActivity extends AppCompatActivity implements So ((Infinity) getApplication()).getAppComponent().inject(this); + EventBus.getDefault().register(this); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) { Resources resources = getResources(); @@ -273,7 +276,7 @@ public class ViewSubredditDetailActivity extends AppCompatActivity implements So if(mNewAccountName != null) { if(account == null || !account.getUsername().equals(mNewAccountName)) { new SwitchAccountAsyncTask(mRedditDataRoomDatabase, mNewAccountName, newAccount -> { - EventBus.getDefault().post(new SwitchAccountEvent()); + EventBus.getDefault().post(new SwitchAccountEvent(getClass().getName())); mNewAccountName = null; if(newAccount == null) { mNullAccessToken = true; @@ -483,6 +486,12 @@ public class ViewSubredditDetailActivity extends AppCompatActivity implements So getSupportFragmentManager().putFragment(outState, FRAGMENT_OUT_STATE_KEY, mFragment); } + @Override + protected void onDestroy() { + super.onDestroy(); + EventBus.getDefault().unregister(this); + } + private void makeSnackbar(int resId, boolean retry) { if(showToast) { Toast.makeText(this, resId, Toast.LENGTH_SHORT).show(); @@ -527,6 +536,13 @@ public class ViewSubredditDetailActivity extends AppCompatActivity implements So } } + @Subscribe + public void onAccountSwitchEvent(SwitchAccountEvent event) { + if(!getClass().getName().equals(event.excludeActivityClassName)) { + finish(); + } + } + private static class InsertSubredditDataAsyncTask extends AsyncTask<Void, Void, Void> { interface InsertSubredditDataAsyncTaskListener { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewUserDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewUserDetailActivity.java index 6a0e3649..f3b1d1ba 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewUserDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewUserDetailActivity.java @@ -36,6 +36,7 @@ import com.google.android.material.snackbar.Snackbar; import com.google.android.material.tabs.TabLayout; import org.greenrobot.eventbus.EventBus; +import org.greenrobot.eventbus.Subscribe; import javax.inject.Inject; import javax.inject.Named; @@ -122,6 +123,8 @@ public class ViewUserDetailActivity extends AppCompatActivity implements UserThi ((Infinity) getApplication()).getAppComponent().inject(this); + EventBus.getDefault().register(this); + username = getIntent().getStringExtra(EXTRA_USER_NAME_KEY); if (savedInstanceState == null) { @@ -353,7 +356,7 @@ public class ViewUserDetailActivity extends AppCompatActivity implements UserThi if(mNewAccountName != null) { if(account == null || !account.getUsername().equals(mNewAccountName)) { new SwitchAccountAsyncTask(mRedditDataRoomDatabase, mNewAccountName, newAccount -> { - EventBus.getDefault().post(new SwitchAccountEvent()); + EventBus.getDefault().post(new SwitchAccountEvent(getClass().getName())); mNewAccountName = null; if(newAccount == null) { mNullAccessToken = true; @@ -536,6 +539,12 @@ public class ViewUserDetailActivity extends AppCompatActivity implements UserThi outState.putString(NEW_ACCOUNT_NAME_STATE, mNewAccountName); } + @Override + protected void onDestroy() { + super.onDestroy(); + EventBus.getDefault().unregister(this); + } + private void showMessage(int resId, boolean retry) { if(showToast) { Toast.makeText(this, resId, Toast.LENGTH_SHORT).show(); @@ -554,6 +563,13 @@ public class ViewUserDetailActivity extends AppCompatActivity implements UserThi sectionsPagerAdapter.changeSortType(sortType); } + @Subscribe + public void onAccountSwitchEvent(SwitchAccountEvent event) { + if(!getClass().getName().equals(event.excludeActivityClassName)) { + finish(); + } + } + private static class InsertUserDataAsyncTask extends AsyncTask<Void, Void, Void> { interface InsertUserDataAsyncTaskListener { |