Create app with a Fragment containing a Button id=button In
@Override
protected void onStart() {
super.onStart();
getSupportFragmentManager()
.beginTransaction()
.add(R.id.activity_main, new Testfrag())
.commitNow();
View b = findViewById(R.id.button);
}
b is returned as the button and the fragments onCreateView
is called before the end of the method.
But in
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager fm= getSupportFragmentManager();
fm.beginTransaction()
.add(R.id.activity_main, new Testfrag())
.commitNow();
View b = findViewById(R.id.button);
}
b is null and the fragments onCreateView
is not called until after the method ends.
This may be a "feature" but there is nothing about it in the documentation.
Why does commit Now behave differently in onCreate
- doesn't call onCreateView
until later and onStart (or any other event handler) where onCreateView
is called synchronously?