1111
1212import androidx .annotation .NonNull ;
1313import androidx .annotation .Nullable ;
14+ import androidx .lifecycle .Lifecycle ;
15+ import androidx .lifecycle .LifecycleEventObserver ;
16+ import androidx .lifecycle .LifecycleOwner ;
1417
18+ import com .instabug .flutter .generated .InstabugPigeon ;
1519import com .instabug .flutter .modules .ApmApi ;
1620import com .instabug .flutter .modules .BugReportingApi ;
1721import com .instabug .flutter .modules .CrashReportingApi ;
2731import io .flutter .embedding .engine .plugins .FlutterPlugin ;
2832import io .flutter .embedding .engine .plugins .activity .ActivityAware ;
2933import io .flutter .embedding .engine .plugins .activity .ActivityPluginBinding ;
34+ import io .flutter .embedding .engine .plugins .lifecycle .HiddenLifecycleReference ;
3035import io .flutter .embedding .engine .renderer .FlutterRenderer ;
3136import io .flutter .plugin .common .BinaryMessenger ;
3237
33- public class InstabugFlutterPlugin implements FlutterPlugin , ActivityAware {
38+ public class InstabugFlutterPlugin implements FlutterPlugin , ActivityAware , LifecycleEventObserver {
3439 private static final String TAG = InstabugFlutterPlugin .class .getName ();
3540
3641 @ SuppressLint ("StaticFieldLeak" )
3742 private static Activity activity ;
3843
44+ private InstabugPigeon .InstabugFlutterApi instabugFlutterApi ;
45+ private Lifecycle lifecycle ;
3946
4047 @ Override
4148 public void onAttachedToEngine (@ NonNull FlutterPluginBinding binding ) {
4249 register (binding .getApplicationContext (), binding .getBinaryMessenger (), (FlutterRenderer ) binding .getTextureRegistry ());
50+ instabugFlutterApi = new InstabugPigeon .InstabugFlutterApi (binding .getBinaryMessenger ());
4351 }
4452
4553 @ Override
@@ -50,23 +58,60 @@ public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
5058 @ Override
5159 public void onAttachedToActivity (@ NonNull ActivityPluginBinding binding ) {
5260 activity = binding .getActivity ();
61+
62+ // Register lifecycle observer if available
63+ if (binding .getLifecycle () instanceof HiddenLifecycleReference ) {
64+ lifecycle = ((HiddenLifecycleReference ) binding .getLifecycle ()).getLifecycle ();
65+ lifecycle .addObserver (this );
66+ }
5367 }
5468
5569 @ Override
5670 public void onDetachedFromActivityForConfigChanges () {
71+ if (lifecycle != null ) {
72+ lifecycle .removeObserver (this );
73+ }
5774 activity = null ;
5875 }
5976
6077 @ Override
6178 public void onReattachedToActivityForConfigChanges (@ NonNull ActivityPluginBinding binding ) {
6279 activity = binding .getActivity ();
80+
81+ // Re-register lifecycle observer if available
82+ if (binding .getLifecycle () instanceof HiddenLifecycleReference ) {
83+ lifecycle = ((HiddenLifecycleReference ) binding .getLifecycle ()).getLifecycle ();
84+ lifecycle .addObserver (this );
85+ }
6386 }
6487
6588 @ Override
6689 public void onDetachedFromActivity () {
90+ if (lifecycle != null ) {
91+ lifecycle .removeObserver (this );
92+ lifecycle = null ;
93+ }
6794 activity = null ;
6895 }
6996
97+ @ Override
98+ public void onStateChanged (@ NonNull LifecycleOwner source , @ NonNull Lifecycle .Event event ) {
99+ if (event == Lifecycle .Event .ON_PAUSE ) {
100+ handleOnPause ();
101+ }
102+ }
103+
104+ private void handleOnPause () {
105+ if (instabugFlutterApi != null ) {
106+ instabugFlutterApi .dispose (new InstabugPigeon .InstabugFlutterApi .Reply <Void >() {
107+ @ Override
108+ public void reply (Void reply ) {
109+ Log .d (TAG , "Screen render cleanup dispose called successfully" );
110+ }
111+ });
112+ }
113+ }
114+
70115 private static void register (Context context , BinaryMessenger messenger , FlutterRenderer renderer ) {
71116 final Callable <Bitmap > screenshotProvider = new Callable <Bitmap >() {
72117 @ Override
@@ -77,7 +122,7 @@ public Bitmap call() {
77122
78123 Callable <Float > refreshRateProvider = new Callable <Float >() {
79124 @ Override
80- public Float call (){
125+ public Float call () {
81126 return getRefreshRate ();
82127 }
83128 };
0 commit comments