gpt4 book ai didi

java - 如何使用 onCreate 方法在 MainActivity 中打开 fragment

转载 作者:行者123 更新时间:2023-12-02 02:40:21 26 4
gpt4 key购买 nike

我创建了一个 Drawer Activity ,现在我有 MainActivity 和 RegisterFragment。 XML 和其他事情现在已经完成,我可以使用抽屉导航打开 RegisterFragment,但是如何在应用程序启动时显示 RegisterFragment?

主要 Activity

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
navigationView.setNavigationItemSelectedListener(this);
}


//unnecessary code block removed

@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
Fragment fragment = null;

if (id == R.id.nav_home) {

} else if (id == R.id.nav_gallery) {

fragment = new RegisterFragment();

} else if (id == R.id.nav_slideshow) {

} else if (id == R.id.nav_tools) {

} else if (id == R.id.nav_share) {

} else if (id == R.id.nav_send) {

}

if (fragment != null){
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction =
fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.screen_area, fragment);
fragmentTransaction.commit();
}

DrawerLayout drawer = findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}

正如我所说,它基本上运行良好,我想在应用程序启动时打开 RegisterFragment。

最佳答案

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
navigationView.setNavigationItemSelectedListener(this);

// change fragment at onCreate method
changeFragment(new RegisterFragment());
}


private void changeFragement(Fragment fragment){
if (fragment != null){
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction =
fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.screen_area, fragment);
fragmentTransaction.commit();
}
}


//unnecessary code block removed

@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
Fragment fragment = null;

if (id == R.id.nav_home) {

} else if (id == R.id.nav_gallery) {

fragment = new RegisterFragment();

} else if (id == R.id.nav_slideshow) {

} else if (id == R.id.nav_tools) {

} else if (id == R.id.nav_share) {

} else if (id == R.id.nav_send) {

}

changeFragment(fragment);

DrawerLayout drawer = findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}

关于java - 如何使用 onCreate 方法在 MainActivity 中打开 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57182698/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com