These are the options you have. Use one of these as you need in the project.
The best way
The best way of doing this is what you already said in your question, add a BaseActivity
and extend all your activities from it. According to the official documentation of CoordinatorLayout
,
CoordinatorLayout
is intended for two primary use cases:
- As a top-level application decor or chrome layout
- As a container for a specific interaction with one or more child views
So CoordinatorLayout
is created primarily for this reason (Though there are also other reasons). There will be the least performance issues as mentioned in the documentation.
By Using FrameLayout
As already answered by Rainmaker, you can use a Activity with a reference to the CoordinatorLayout
layout in your layouts folder where the child will be a framelayout.
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
android:id="@+id/activity_root"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.design.widget.CoordinatorLayout>
Then you will be using only one activity with setContentView(R.layout.root_coordinate_layout)
. Then you will convert all other activities into fragments and add them with :
MyFragment myf = new MyFragment();
FragmentTransaction transaction = getFragmentManager()
.beginTransaction()
.add(R.id.your_layout, myf)
.commit();
The programmatic way
This is another way of doing the same thing. But this is a bit more complex and need a lot of works to do.
In all your activity, instead of setContentView(R.id.your_layout)
, use this:
LayoutInflater inflater = LayoutInflater.from(this);
ConstraintLayout yourLayout = (ConstraintLayout) inflater.inflate(R.layout.your_layout, null, false);
CoordinatorLayout layout = new CoordinatorLayout(this);
// Set its property as you wish ...
layout.addView(mainScreen);
setContentView(layout);