2

I'm trying out the single activity approach together with the Navigation Component but I was wondering how to apply multiple themes to one activity.

Normally I would set a theme to an activity via the AndroidManifest.xml and all the fragment within that activity will use that same styling. Whenever I want to use another theme I would simply create another activity and apply the other styling. But since there only is one activity I would have to change the styling after the activity has been created. This is fine for styling used by Fragments. However styles for the StatusBar and NavigationBar are a lot harder because they affect the Activity.

Things I've tried:

  1. Change the theme of the Activity after is has been created. Unfortunately these changes aren't applied directly and would require a restart of the activity (which is the one currently running).
  2. Set styles like StatusBar and NavigationBar when creating the Fragment. This works, but it requires me to set the themes and styles using code instead of using the usual themes.xml and styles.xml.

Can anybody tell me what the intended solution is? Because of these limitations we started adding activities again to the navigation component whenever the theme needed to be changed.

Wirling
  • 4,810
  • 3
  • 48
  • 78
  • When i faced this issue, i passed the theme resource id to the fragment, on the fragment onCreateView, i create a contextThemeWrapper like Context context = new ContextThemeWrapper(getActivity(), R.style.your_Custom_Theme); use this context to create layout inflater. So i think you have to pass the reference of your theme to the destination fragment using navigation as an argument. – Anis BEN NSIR Jul 16 '20 at 11:34
  • @AnisBENNSIR Yes I also [tried that](https://stackoverflow.com/a/15496425/2212770) together with the 2nd solution, but it only applies the theme to the fragment. Things like the StatusBar and NavigationBar won't be affected. – Wirling Jul 16 '20 at 13:52
  • if you need to avoid restarting the activity(only recreate will update theme) all toolbar, Navbar must be part of fragment. The host activity must be empty. The toolbar will be part of the screen transition. – Anis BEN NSIR Jul 16 '20 at 17:48
  • @AnisBENNSIR That's the problem. They aren't part of my layout at all. They are part of the system UI. And changing the colors of these components is somehow handled by the theme that is applied to the activity. – Wirling Jul 17 '20 at 14:06
  • i think that you have to refactor your code like this thread. https://stackoverflow.com/questions/57014922/how-to-setup-different-toolbar-using-navigation-controller-component – Anis BEN NSIR Jul 17 '20 at 14:52
  • I think you're confusing the StatusBar and NavigationBar with the ToolBar. Take a look at this: https://stackoverflow.com/q/27839105 – Wirling Jul 17 '20 at 15:02

0 Answers0