alexa
Facebook
Twitter
LinkedIn
Instagram
Whatsapp
Call Now
Quick Inquiry

AppCompatActivity BaseActivity Layout not displaying in View-Binding activity in android ?

AppCompatActivity BaseActivity Layout not displaying in View-Binding activity in android ?

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


42 0
7

Write a Comments


* Be the first to Make Comment

GoodFirms Badge
GoodFirms Badge

Fix Your Meeting With Our SEO Consultants in India To Grow Your Business Online