diff --git a/Android/app/src/main/java/pub/doric/demo/DemoActivity.java b/Android/app/src/main/java/pub/doric/demo/DemoActivity.java index c9da789a..0e16a23e 100644 --- a/Android/app/src/main/java/pub/doric/demo/DemoActivity.java +++ b/Android/app/src/main/java/pub/doric/demo/DemoActivity.java @@ -70,7 +70,6 @@ public class DemoActivity extends AppCompatActivity { }); } - @Override public void onAttachedToWindow() { super.onAttachedToWindow(); diff --git a/Android/doric/src/main/java/pub/doric/DoricPanel.java b/Android/doric/src/main/java/pub/doric/DoricPanel.java index a20505aa..a9e578c5 100644 --- a/Android/doric/src/main/java/pub/doric/DoricPanel.java +++ b/Android/doric/src/main/java/pub/doric/DoricPanel.java @@ -16,11 +16,13 @@ package pub.doric; import android.content.Context; -import android.os.Build; import androidx.annotation.NonNull; import androidx.annotation.Nullable; -import androidx.annotation.RequiresApi; +import androidx.lifecycle.Lifecycle; +import androidx.lifecycle.LifecycleObserver; +import androidx.lifecycle.LifecycleOwner; +import androidx.lifecycle.OnLifecycleEvent; import android.util.AttributeSet; import android.widget.FrameLayout; @@ -32,26 +34,25 @@ import pub.doric.utils.DoricUtils; * @Author: pengfei.zhou * @CreateDate: 2019-07-18 */ -public class DoricPanel extends FrameLayout { +public class DoricPanel extends FrameLayout implements LifecycleObserver { private DoricContext mDoricContext; public DoricPanel(@NonNull Context context) { - super(context); + this(context, null); } public DoricPanel(@NonNull Context context, @Nullable AttributeSet attrs) { - super(context, attrs); + this(context, attrs, 0); } public DoricPanel(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); + if (getContext() instanceof LifecycleOwner) { + ((LifecycleOwner) getContext()).getLifecycle().addObserver(this); + } } - @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) - public DoricPanel(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) { - super(context, attrs, defStyleAttr, defStyleRes); - } public void config(String script, String alias) { DoricContext doricContext = DoricContext.create(getContext(), script, alias); @@ -64,6 +65,10 @@ public class DoricPanel extends FrameLayout { if (getMeasuredState() != 0) { mDoricContext.init(DoricUtils.px2dp(getMeasuredWidth()), DoricUtils.px2dp(getMeasuredHeight())); } + if (getContext() instanceof LifecycleOwner + && ((LifecycleOwner) getContext()).getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.RESUMED)) { + mDoricContext.onShow(); + } } @Override @@ -84,4 +89,25 @@ public class DoricPanel extends FrameLayout { } } } + + @OnLifecycleEvent(Lifecycle.Event.ON_RESUME) + public void onActivityResume() { + if (mDoricContext != null) { + mDoricContext.onShow(); + } + } + + @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE) + public void onActivityPause() { + if (mDoricContext != null) { + mDoricContext.onHidden(); + } + } + + @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY) + public void onActivityDestroy() { + if (mDoricContext != null) { + mDoricContext.teardown(); + } + } }