Search code examples
androidandroid-layoutandroid-fragmentsandroid-fragmentactivity

Android: Updating Fragments (that might not yet be created) from activity


Is there any way to do this?

I'm working on an application that uses multiple tabs to manage bluetooth connections, messages and other stuff.

I need my activity to inform each of these tabs (fragments) of certain events such as connection status, messages delivered.

I used an approach where i keep a reference to each of my fragments in the adapter. The problem is that fragments are not instantiated until o switch to the tab that uses it.

Is it possible to instantiate all four fragments so that i can update their view even though it has not been shown yet?

Thanks a lot,

and sorry for my english


Solution

  • If you use a ViewPager (you mentioned "tabs") just call setOffscreenPageLimit() in onCreate() of your App (after initializing ViewPager). Set the limit to the number of your fragments. With this all of your fragments will be instantiate.