@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
Main2Binding binding1 = Main2Binding.inflate(getLayoutInflater());
User user = new User("Test", "User");
MyHandlers myHandlers = new MyHandlers(this);
MyStringUtils myStringUtils= new MyStringUtils();
binding1.setUser(user);
binding1.setHandlers(myHandlers);
}
Page is not updated,
MainActivityBinding binding1 = DataBindingUtil.setContentView (this, R.layout.main_activity); there is no problem
How to solve this problem?
The right way using DataBinding via inflate method
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.main2); //<---comment it
Main2Binding binding1 = Main2Binding.inflate(getLayoutInflater());
setContentView(binding1.getRoot());
...
}
If you want to bind layout with your Activity
you need to use
MainActivityBinding binding1 = DataBindingUtil.setContentView (this, R.layout.main_activity);
But when you are working with Fragment
you can use inflate
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
MainActivityBinding binding = DataBindingUtil.inflate(inflater, R.layout.main_activity, container, false);
}
Note : Binding name will be based on layout name, if layout file name is activity_main.xml then your binding will be ActivityMainBinding.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With