简体   繁体   中英

how to callback into various activities from a android library

I've written a library for Android, upon initialising the library I register for call backs for location updates, subscription status changes, library status changes and various other updates... Originally, I wanted all these updates to call back to a controller in my android UI app, and then for the controller to launch activity A, pass on the messages from the controller to activity A and so on. Or launch activity B for signing up for a subscription and forward messages to this etc However, it appears that there isn't a way to achieve this - Because each activity is in isolation? Unless I'm mistaken?

So what are my options here? It sounds like I either have to use one activity for the whole app and swap the UI's which after looking into it doesn't appear to be the way to go?

I did try to subclass Application, which worked and gave me access to my library from an activity – but I want it the other way around. Is this possible? would wrapping the library in a service achieve what I want to do?

You could use broadcasts and send them from your library. Each of your activities would have to register a BroadcastReceiver in their onResume() method and unregister it in their onPause() method. This would be comparatively easy when you use a common base class for your activities. You could then send commands from your library to whatever activity is currently active.

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