我创建的活动通过单击导航抽屉中的菜单项来打开多个片段,我面临的问题是当我选择某些项目并且相关片段打开时,工具栏上的标题正确,然后旋转应用程序,片段停留在该位置(我想要的是什么,我做了什么,我使用了saveInstanceState来保存它)工具栏中的标题更改为默认标题!
我不知道如何保留标题和片段
这里是活动
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
TextView header;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
header = findViewById(R.id.header);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
setupDrawer(toolbar);
if(savedInstanceState==null) {
HomeFragment fragment = new HomeFragment();
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.content, fragment);
fragmentTransaction.commit();
header.setText("Home");
}
ActionBar ab = getSupportActionBar();
if (ab != null) {
ab.setTitle(null);
ab.setHomeButtonEnabled(true);
ab.setDisplayHomeAsUpEnabled(true);
ab.setHomeAsUpIndicator(R.drawable.ic_launcher_foreground);
}
}
private void setupDrawer(Toolbar toolbar) {
DrawerLayout drawer = findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
int id = item.getItemId();
Fragment fragment = null;
switch (id) {
case R.id.nav_home:
fragment = new HomeFragment();
header.setText("Home");
break;
case R.id.nav_settings:
fragment = new SettingsFragment();
header.setText("Settings");
break;
case R.id.nav_about:
fragment = new AboutFragment();
header.setText("About app");
break;
}
if (fragment != null) {
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.content, fragment);
fragmentTransaction.commit();
}
DrawerLayout drawer = findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
这里是活动的xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start"
tools:context=".MainActivity">
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white">
<include layout="@layout/toolbar"/>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/content"
/>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
<com.google.android.material.navigation.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:itemTextColor="@color/colorPrimary"
app:itemIconTint="@color/colorPrimary"
app:headerLayout="@layout/drawer_header"
app:menu="@menu/navigation_drawer">
</com.google.android.material.navigation.NavigationView>
</androidx.drawerlayout.widget.DrawerLayout>
我的工具栏xml:
<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.appbar.AppBarLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:elevation="0dp">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@android:color/holo_red_dark">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:lineSpacingExtra="28sp"
android:text="Home"
android:textAllCaps="true"
android:textColor="@android:color/black"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</LinearLayout>
</androidx.appcompat.widget.Toolbar>
</com.google.android.material.appbar.AppBarLayout>
我搜索并找到了一些解决方案,但是对我没有用,或者我不知道该怎么做,所以请帮助,谢谢。]
我创建的活动具有多个片段,通过单击导航抽屉中的菜单项来打开,我遇到的问题是当我选择某些项目并且相关片段以...中的正确标题打开时...]
尝试在xml布局内的文本中声明saveEnabled =“ true”。