Reputation: 18846
There is ComponentCallbacks interface in android using by activity, ActivityGroup, service etc. They implements it and someone notify them when activity configuration changed. So, I want to create my own class which implements ComponentCallbacks listens for onConfigurationChanged and doing some actions... So my class is implementing, but... I guess I need to register my class in someone's observer.
So, is it possible? Is there any ways to "register" my own class to be notified when configuration changed?
I guess this someone may be ActivityThread and it's method collectComponentCallbacksLocked. But I did't see here any ways to register my own class.
ActivityThread and ComponentCallbacks using
p.s. Of course I can override activity's method onConfigurationChange change and then call the onConfigurationChanged method of my class, but I don't want. I want to know if there is any way in android to do it.
Upvotes: 2
Views: 4726
Reputation: 4259
In the "Google I/O 2012 - Doing More With Less: Being a Good Android Citizen " talk (which you can find on youtube), it's explained that you can do this by calling Context.registerComponentCallbacks
.
So, you just make your class implement the ComponentCallbacks
interface (or ComponentCallbacks2
as you wish), the methods that you can override are added automatically, and from your Activity, Fragment.. just pass the instance from your class to register to it. Something like this:
registerComponentCallbacks( mYourClass );
That should do the work :)
Upvotes: 6
Reputation: 18846
According to @Sandra's answer we can use Context.registerComponentCallbacks to register our own component callbacks in the system and use it like this:
context.registerComponentCallbacks(new ComponentCallbacks() {
@Override
public void onLowMemory() {
// make some operations when system memory is low
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
// make some operations when configuration has been changed
}
});
All our registered callbacks will be called when system configuration changed / on low memory.
Method registerComponentCallbacks available from API level 14. Checkout how it works here
Upvotes: 2
Reputation: 95626
What you want to do is to register a BroadcastReceiver to receive this braodcast Intent:
Intent.ACTION_CONFIGURATION_CHANGED
That event is broadcast by the system any time the configuration changes.
Upvotes: 0