首页 > 解决方案 > 当方向发生在默认标题android上时如何保持标题不变

问题描述

我创建的活动通过单击导航抽屉中的菜单项打开了多个片段,我面临的问题是当我选择某个项目并且相关片段在工具栏中以正确的标题打开然后旋转片段停留在它的应用程序时(哪个我想要并且我做了什么,我使用了 savedInstanceState 来保留它)工具栏中的标题更改为默认标题!

我不知道如何在片段中保留标题

这是活动

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>

我搜索并找到了一些解决方案,但没有为我工作,或者我不知道如何正确地做,所以请帮助,提前谢谢

标签: androidfragmentnavigation-drawerorientationonsaveinstancestate

解决方案


1- 尝试在您的 Text inside xml layout 中声明 saveEnabled = "true" 。

2-试试这个:

header = findViewById(R.id.header);

    if (myNewBoolean) {
        tvNowUsedFiltres.setVisibility(View.VISIBLE);
    }

3-也试试这个:

把它放在你的片段 onCreate()

setRetainInstance(boolean)

推荐阅读