Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MainActivityBinding.inflate(getLayoutInflater()) page is not updated

@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?

like image 230
user6283975 Avatar asked Sep 07 '25 10:09

user6283975


2 Answers

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());
    ...
}
like image 172
yoAlex5 Avatar answered Sep 10 '25 03:09

yoAlex5


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.

like image 34
Ravi Avatar answered Sep 10 '25 03:09

Ravi