Reputation: 1022
I want to change the font size of entire application. I have used 3 styles(small, medium,large), keeping app theme as parent. Whenever user selects the the font change button, based on selected style, I'm changing the font size of entire application. Since, for a fragment, theme has to be applied inside onCreateView(), before inflating the layout,theme will not be applied to the fragment on click of the font size change button. I need to refresh the fragment, so that it's view is recreated. I'm manually detaching the fragment and then attaching again to the activity. This destroys the fragment view and recreates it and the fragment UI is updated. My query is, is it recomended to call onDetach() & onAttach() manually? This is how I'm refreshing the fragment:
private void reloadFragment() {
android.support.v4.app.FragmentManager mngr = getSupportFragmentManager();
Fragment fragment = mngr.findFragmentById(R.id.container);
if (fragment instanceof HomeFragment) {
try {
SectionPagerAdapter adapter = ((HomeFragment) fragment).getAdapter();
android.support.v4.app.FragmentTransaction transaction = this.getSupportFragmentManager().beginTransaction();
transaction.detach(fragment).commitAllowingStateLoss();
transaction.attach(fragment);
adapter.notifyDataSetChanged();//I'm using viewpager, so notifying it
} catch (IllegalStateException e) {
e.printStackTrace();
}
}
}
Thanks in advance..
Upvotes: 0
Views: 1112
Reputation: 14021
No, you can do this. All life circle methods can't be called manually. If you want to change Fragment
s, you need to do like this:
private void reloadFragment() {
android.support.v4.app.FragmentManager mngr = getSupportFragmentManager();
Fragment fragment = mngr.findFragmentByTag(tag);
if (fragment == null){
fragment = new YourFragment();
}
if (fragment instanceof HomeFragment) {
try {
SectionPagerAdapter adapter = ((HomeFragment) fragment).getAdapter();
android.support.v4.app.FragmentTransaction transaction = this.getSupportFragmentManager().beginTransaction();
transaction.replace(resId, fragment, tag);
adapter.notifyDataSetChanged();//I'm using viewpager, so notifying it
} catch (IllegalStateException e) {
e.printStackTrace();
}
}
}
Upvotes: 1