This repository has been archived by the owner on Apr 27, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(android): fix current activity not found
Added custom ReactModule base class to keep track for current activity. Refs: #30
- Loading branch information
Showing
6 changed files
with
92 additions
and
21 deletions.
There are no files selected for viewing
76 changes: 76 additions & 0 deletions
76
android/src/main/java/com/rnadmob/admob/ActivityAwareJavaModule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package com.rnadmob.admob; | ||
|
||
import android.app.Activity; | ||
import android.app.Application; | ||
import android.os.Bundle; | ||
|
||
import androidx.annotation.NonNull; | ||
import androidx.annotation.Nullable; | ||
|
||
import com.facebook.react.bridge.LifecycleEventListener; | ||
import com.facebook.react.bridge.ReactApplicationContext; | ||
import com.facebook.react.bridge.ReactContextBaseJavaModule; | ||
|
||
public abstract class ActivityAwareJavaModule extends ReactContextBaseJavaModule implements Application.ActivityLifecycleCallbacks, LifecycleEventListener { | ||
|
||
@Nullable | ||
private Application application; | ||
|
||
@Nullable | ||
protected Activity currentActivity; | ||
|
||
public ActivityAwareJavaModule(ReactApplicationContext reactContext) { | ||
super(reactContext); | ||
reactContext.addLifecycleEventListener(this); | ||
} | ||
|
||
@Override | ||
public void onActivityCreated(Activity activity, Bundle savedInstanceState) {} | ||
|
||
@Override | ||
public void onActivityStarted(@NonNull Activity activity) { | ||
currentActivity = activity; | ||
} | ||
|
||
@Override | ||
public void onActivityResumed(@NonNull Activity activity) { | ||
currentActivity = activity; | ||
} | ||
|
||
@Override | ||
public void onActivityStopped(Activity activity) {} | ||
|
||
@Override | ||
public void onActivityPaused(Activity activity) {} | ||
|
||
@Override | ||
public void onActivitySaveInstanceState(Activity activity, Bundle bundle) {} | ||
|
||
@Override | ||
public void onActivityDestroyed(@NonNull Activity activity) { | ||
if (activity.equals(currentActivity)) { | ||
currentActivity = null; | ||
} | ||
} | ||
|
||
@Override | ||
public void onHostResume() { | ||
if (application == null) { | ||
currentActivity = getCurrentActivity(); | ||
if (currentActivity != null) { | ||
application = currentActivity.getApplication(); | ||
application.registerActivityLifecycleCallbacks(this); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public void onHostPause() {} | ||
|
||
@Override | ||
public void onHostDestroy() { | ||
if (application != null) { | ||
application.unregisterActivityLifecycleCallbacks(this); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters