简体   繁体   中英

How to obtain android activity lifecycle events from view?

I'm creating a component that is intended to be used in some other application who's Activity I have no control over. In my View based component I'm wanting to know when the parent activity is going away (onPause) and coming back (onResume). I found that the Application class allows registration of a callback for these events in API 14 but I'm stuck coding to API 8.

I've tried various ViewTreeObserver attempts but they seem flaky and doesn't seem like a proper fit.

You should set up your custom view with methods for each lifecycle event, like onActivityPause, onActivityResume etc. Have the activity that uses your view 'forward' its life cycle events to these methods.

This is how Google does it with the new v2 MapView.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM