Search code examples
androidfragmentnavigation-drawerorientationonsaveinstancestate

how to keep title not changing it when orientation happen to default title android


I created activity has multiple fragments opens by clicking in menu items in navigation drawer, the problem I face is when I select some item and the relevant fragment opens with the right title in toolbar then rotate the app the fragment stays at it is(which what I want and what I did , I used savedInstanceState to keep it) the title in toolbar changes to the default one!

I didn't know how to keep the title with the fragments

here is the activity

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;
}


}

and here the xml for the activity

<?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>

my toolbar 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>

I searched and found some solutions but didn't worked for me or maybe I didn't know how to do it right, so please help, thanks in advance


Solution

  • I found a solution, actually I tried it before but I was doing something wrong, now I new what I did .. the solution I find is to use the following code :

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
    
        outState.putString(STATE_SELECTED_KEY, title);
    }
    
    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
    
        title = savedInstanceState.getString(STATE_SELECTED_KEY, "Home");
        header.setText(title);
    }
    

    the value of variable title is changing on menu item selected, and then we just save it onSaveInstanceState(Bundle outState) and call onRestoreInstanceState(Bundle savedInstanceState) to get it back

    thanks to this solution I found here