My activities extend from a generic base activity, where I declare and initialize public variables like context
of type Context, activity
of type Activity and mActionBar of type ActionBar
.
So this avoids redundant initialization code in all my app's activities.
But with the advent of Toolbar
, I am a little confused on how to do this. Toolbar is not like ActionBar and replaces it, but also extends it.
The ActionBar is a view object that is always available for retrieval, by ActionBar activity, and it sits above the views that are created. This is not declared in layout XML anywhere.
But Toolbar is declared only in layout XML, so I have to include it in each and every layout I create, or else I will not be able to access the Toolbar object.
I typically use setContentView(R.layout.mylayout)
in the onCreate
method of each individual activity. And then I have to initialize my Toolbar object after that using findViewById
. Therefore I can't put this code in my BaseActivity's onCreate function because the setContentView
wouldn't have been initialized yet.
Even if I created Toolbar programmatically with it's constructor, and attempted to add the view to the top of the hierarchy, I would still have to do this on a layout by layout, and activity by activity basis, because some layouts are RelativeLayout's as the root object, and others are different. So these will still have separate code considerations.
The reason I am curious about a good way for my activities to inherit Toolbar, is because it is a complete nightmare for Google to suddenly require Android 4.0-4.4 devices to use the v7 compatibility pack, replace the actionbar completely with the Toolbar object, use v4 compatibility pack fragments instead of native ones, all to use the latest design paradigms.
onCreate()
method, but don't add it to the on-screen layout until after the subclass callssetContentView()
.