Home > front end >  AppCompatActivity BaseActivity Layout not displaying in View-Binding activity
AppCompatActivity BaseActivity Layout not displaying in View-Binding activity

Time:05-03

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);
    }
}

CodePudding user response:

You can't show two activities on screen, activity should host fragments

CodePudding user response:

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

abstract class BaseActivity<VB : ViewBinding> : AppCompatActivity() {
    
        private var _binding: VB? = null
        
        val childActivityBinding: VB
                get() = _binding as VB
    
        abstract fun inflateLayout(
            parent: FrameLayout,
            inflater: LayoutInflater
        ): VB
    
        lateinit var baseActivityBinding: ActivityBaseBinding
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
    
            baseActivityBinding = ActivityBaseBinding.inflate(layoutInflater)
            _binding = inflateLayout(baseActivityBinding.root.layout_container, layoutInflater)
    
            val coordinatorLayout = baseActivityBinding.root
            .
            .// some base activity processes like setting Snackbar etc.
            val snack = coordinatorLayout.snack
            .
    
            super.setContentView(coordinatorLayout)
        }
    }

public class MainActivity extends BaseActivity<ActivityMainBinding> {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }
    @NonNull
    @Override
    public ActivityMainBinding inflateLayout(@NonNull FrameLayout parent, @NonNull LayoutInflater inflater) {
        return ActivityMainBinding.inflate(inflater, parent, true);
    }
}

I recoment that you use fragemnt instead of NewStyleActivity and your will have refernce on you parent activity which is composition not inheritance

  • Related