Skip to content

Commit

Permalink
fix: ensure we unregister the activity comparing by name
Browse files Browse the repository at this point in the history
fix: use also onResume/onPause to register/unregister the activity
  • Loading branch information
aitorvs committed Feb 28, 2016
1 parent 9e92aa9 commit 9967f3c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,10 @@ public static void registerActivity(@NonNull Activity activity) {
}

public static void unregisterActivity(@NonNull Activity activity) {
if (activity == getInstance().mActivity) {
//noinspection ConstantConditions
if (getInstance().mActivity == null || activity == null) {
Log.w(TAG, "Trying to unregister null activity");
}else if (activity.getClass().getName().equals(getInstance().mActivity.getClass().getName())) {
getInstance().mActivity = null;
} else {
Log.w(TAG, "unregisterActivity: Old activity is trying to unregister");
Expand All @@ -75,7 +78,7 @@ public static void unregisterActivity(@NonNull Activity activity) {
private static Activity safeActivity() {
Activity activity = getInstance().mActivity;
if (activity == null) {
throw new IllegalStateException("Activity not registered");
throw new IllegalStateException("Ooops!! Activity not registered (?)");
}

return activity;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,28 @@ protected void onCreate(Bundle savedInstanceState) {
@Override
protected void onStart() {
super.onStart();
// on visible
AllowMe.registerActivity(this);
}

@Override
protected void onResume() {
super.onResume();
// on begin interaction
AllowMe.registerActivity(this);
}

@Override
protected void onPause() {
super.onPause();
// on stop interaction
AllowMe.unregisterActivity(this);
}

@Override
protected void onStop() {
super.onStop();
// on invisible
AllowMe.unregisterActivity(this);
}

Expand Down

0 comments on commit 9967f3c

Please sign in to comment.