Search code examples
androidkotlinandroid-activitylayoutdata-binding

Manage single Databinding object when conditional multiple layouts apply to single activity


Now I am working on a project in which I have to use multiple layouts in single activity. Let me describe with example

For example there is an activity called "DashboardActivity" and I have to create 4 layout for this activity.

If there is flowId=1 from the server I have to apply activity_dashboard_layout_flow_one. Same as like for flowId=2/3/4. I have different layout for all flows like activity_dashboard_layout_flow_two , activity_dashboard_layout_flow_three , activity_dashboard_layout_flow_four etc

Now I am using databinding in my project so simple all layout files generate Binding files. So there is different binding objects in my activity. But I want to use and manage only one binding object to access views from XML files.

So please suggest me how to handle Databinding in this situation. And what is the efficient way to do this in kotlin with databinding

Thank you


Solution

  • After done some r&d I do the one solution that set binding object as Any type. Theme wise condition to set root layout and make extension functions of all views that return particular view from available 4 binding.

    And today I found similar question with similar solution to my solution but this given solution is in better manner than my solution. check out following link

    View Binding with 2 possible layouts, assign binding variable to 2 generated binding classes