Application Level onResume Android

前端 未结 4 802
粉色の甜心
粉色の甜心 2021-01-12 03:54

Problem

The idea is very simple. Whenever an user comes back to my app from the Recents I want to show a simple dialog prompting with the password.<

4条回答
  •  终归单人心
    2021-01-12 04:16

    I would suggest using LifecycleObserver. If your Application class implements this interface it marks a class as a LifecycleObserver, it does not have any methods, instead, it relies on OnLifecycleEvent annotated methods. The usage is simple:

    public class AndroidApplication extends Application implements LifecycleObserver {
    
        @OnLifecycleEvent(Lifecycle.Event.ON_START)
        public void onAppStart() {
             //enter code here
        }
    
        @OnLifecycleEvent(Lifecycle.Event.ON_STOP)
        public void onAppStop() {
             //enter code here
        }
    
        ...etc
    }
    

    With Lifecycle.Event you can access all lifecycle states through Enum. It is part of androidx.

提交回复
热议问题