I've followed the following example to add a custom theme for my fragment which gets called immediately after I create my activity:
public class LunchActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lunch);
PrepareIntroFragment prepDriverFrag = new PrepareIntroFragment();
FragmentTransaction frTrans = getSupportFragmentManager().beginTransaction();
frTrans.replace(R.id.rootLayout, prepDriverFrag, "PrepareIntroFragment");
frTrans.disallowAddToBackStack();
frTrans.commit();
}
}
Fragment code to change the theme:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
getContext().getTheme().applyStyle(R.style.PrepareTheme, true);
View view = inflater.inflate(R.layout.fragment_prepare_intro, container, false);
return view;
}
My other solution was:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
final Context contextThemeWrapper = new ContextThemeWrapper(getActivity(), R.style.PrepareTheme);
LayoutInflater localInflater = inflater.cloneInContext(contextThemeWrapper);
return localInflater.inflate(R.layout.fragment_prepare_intro, container, false);// Inflate the layout for this fragment
}
My Theme:
<style name="PrepareTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
<item name="android:windowBackground">@color/BrightYellowCrayola</item>
<item name="android:colorPrimaryDark">@color/BrightYellowCrayola</item>
<item name="android:navigationBarColor">@color/BrightYellowCrayola</item>
<item name="colorButtonNormal">@color/LightGray</item>
</style>
However, after I launch my fragment, the activity theme remain. Is it due since I call my fragment in onCreate?