要将布局填充到片段中,但不启动onCreate方法,可以使用以下解决方法:
public class ExampleFragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_layout, container, false);
// 在这里进行其他视图和布局元素的初始化和操作
return rootView;
}
}
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ExampleFragment fragment = new ExampleFragment();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.add(R.id.fragment_container, fragment);
transaction.commit();
}
}
通过以上步骤,你可以将布局填充到片段中,并且不会启动片段的onCreate方法。