I have a BaseActivity class that extends AppCompatActivity to display a common navigator toolbar, and also implements some other custom listener interfaces for a drawer layout as well. This BaseActivity class is being utilized by many other activity classes in the same project as well, and it will not be practical to refactor it.
I have now created a new activity class, but this one implements view binding. Everything works well, except that it does not seem to display the BaseActivity layout, that is, the common navigator toolbar and the drawer.
Is there a way to implement the layouts of this base activity into this new view-binding class? Through some interface class, perhaps?
Clarification
When the class that is using the BaseActivity class is using conventional layout inflaters (eg: setContentView(R.layout.main)), the common navigator and drawer layouts from the BaseActivity are displayed correctly.
But when the class that is using the BaseActivity class is using view binding layout inflaters (eg: setContentView(binding.root)), the common navigator and drawer layouts from the BaseActivity are not displayed at all.
Code Extracts
public class BaseActivity extends BaseActivityGroup implements DrawerLayout.DrawerListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActivityManager.getInstance().addActivity(this);
super.setContentView(R.layout.base_activity);
}
}
This activity displays the BaseActivity common navigator and drawer layouts, as well as its own layout (R.layout.old_style_activity):
public class OldStyleActivity extends BaseActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.old_style_activity);
}
}
This activity only displays its own layout (binding.getRoot()), but not the BaseActivity’s layout:
import com.myapp.new.databinding.ActivityNewStyleBinding;
public class NewStyleActivity extends BaseActivity {
private ActivityNewStyleBinding binding;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityNewStyleBinding.inflate(getLayoutInflater());
View viewBinding = binding.getRoot();
setContentView(viewBinding);
}
}
2
Answers
You can’t show two activities on screen, activity should host fragments
ViewBinding generate the class from activity_new_style.xml and because of that it do not know about any OOP prinicples… and sory this is in Kotlin but your IDE can change it to Java… the logic is the same
I recoment that you use fragemnt instead of NewStyleActivity and your will have refernce on you parent activity which is composition not inheritance