0

I have one Activity which manages 4 fragments. I'm using a Navigation Drawer and a Collapsing Toolbar within the Activity. When I switch Fragments, I simply would like for the ActionBar title to become what I programmatically set it to but for some reason it's not happening.

I know that it works initially because within my onCreate() method in the Activity I set the title to "Home" and it shows up fine. When I start navigating, however, the code doesn't execute. Does anyone know why?

Here is my MainActivity XML:

<android.support.v4.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">

<include
    layout="@layout/app_bar"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>


<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    app:menu="@menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>

Here is the app_bar layout:

<android.support.design.widget.CoordinatorLayout
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:layout_width="match_parent"
android:layout_height="wrap_content">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="200dp"
    android:fitsSystemWindows="true"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/collapsing_toolbar"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        app:layout_scrollFlags="scroll|exitUntilCollapsed">

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@drawable/task_header"
            android:scaleType="centerCrop"
            android:fitsSystemWindows="true"
            app:layout_collapseMode="parallax"/>

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_height="?attr/actionBarSize"
            android:layout_width="match_parent" />

    </android.support.design.widget.CollapsingToolbarLayout>

</android.support.design.widget.AppBarLayout>

<FrameLayout
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.markfeldman.tasktrack.MainActivity">
</FrameLayout>
</android.support.design.widget.CoordinatorLayout>

Here is my Main Activity:

public class MainActivity extends AppCompatActivity implements Home
    .onFragmentClicked,NavigationView.OnNavigationItemSelectedListener {
private Fragment selectedFragment;
private DrawerLayout drawerLayout;
private ActionBarDrawerToggle drawerToggle;
private String title;
private final String INSTANCE_STATE_SAVE_TITLE = "save_title";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    selectedFragment = new Home();
    if (savedInstanceState==null){
        title  = getString(R.string.fragment_home_title);
        getSupportFragmentManager()
                .beginTransaction()
                .add(R.id.container,selectedFragment)
                .addToBackStack(title)//WORKS HERE
                .commit();
        if (getSupportActionBar()!=null){
            getSupportActionBar().setTitle(title);
        }
    }else{
        title = savedInstanceState.getString(INSTANCE_STATE_SAVE_TITLE);
        if (getSupportActionBar()!=null){
            getSupportActionBar().setTitle(title);
        }
    }
    title = getTitle().toString();
    drawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
    drawerToggle = new ActionBarDrawerToggle(this,drawerLayout,toolbar,R.string.drawer_open,R.string.drawer_close);
    drawerLayout.addDrawerListener(drawerToggle);
    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);
    if (getSupportActionBar()!=null){
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);
    }
    drawerToggle.syncState();
}

@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
    int id = item.getItemId();

    switch (id){
        case (R.id.home_title_nav_drawer) :{
            selectedFragment = new Home();
            title = getString(R.string.fragment_home_title);
            break;
        }
        case (R.id.cal_title_nav_drawer) :{
            selectedFragment = new Calendar();
            title = getString(R.string.fragment_cal_title);
            break;
        }
        case (R.id.contacts_title_nav_drawer) :{
            selectedFragment = new Contacts();
            title = getString(R.string.fragment_contacts_title);
            break;
        }
        case (R.id.tasks_title_nav_drawer) :{
            selectedFragment = new Tasks();
            title = getString(R.string.fragment_task_title);
            break;
        }
    }
    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    ft.replace(R.id.container,selectedFragment)
            .addToBackStack(title)
    if (getSupportActionBar()!=null){
        getSupportActionBar().setTitle(title);//NO LONGER WORKS
    }

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

@Override
protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    // Sync the toggle state after onRestoreInstanceState has occurred.
    drawerToggle.syncState();
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    drawerToggle.onConfigurationChanged(newConfig);
}


@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putString(INSTANCE_STATE_SAVE_TITLE, title);
}

@Override
public void onBackPressed() {
    super.onBackPressed();
    int count = this.getSupportFragmentManager().getBackStackEntryCount();
    if(count==0){
        this.finish();
    }else if (count>0){
        title = getSupportFragmentManager().getBackStackEntryAt(count-1).getName();
        if (getSupportActionBar()!=null){
            getSupportActionBar().setTitle(title);//DOESN'T WORK
        }
    }
}

@Override
public void buttonClicked(View v) {
    int id = v.getId();
    switch (id){
        case (R.id.cal_button):{
            selectedFragment = new Calendar();
            title = getString(R.string.fragment_cal_title);
            break;
        }
        case (R.id.tasks_button):{
            selectedFragment = new Tasks();
            title = getString(R.string.fragment_task_title);
            break;
        }
        case (R.id.contacts_button):{
            selectedFragment = new Contacts();
            title = getString(R.string.fragment_contacts_title);
            break;
        }
    }
    FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
    fragmentTransaction.replace(R.id.container,selectedFragment)
            .addToBackStack(title)
            .commit();

    if (getSupportActionBar()!=null){
        getSupportActionBar().setTitle(title);//DOESN'T WORK
    }
}}

PS: Just as a heads up, the Home.onFragmentClicked is an interface from one of my Fragment's which manages Button onClick's and replaces Fragment's accordingly.

Mark F
  • 1,523
  • 3
  • 23
  • 41
  • IIRC, `CollapsingToolbarLayout` commandeers the `Toolbar`s title, and draws it itself, so you should probably be calling `setTitle()` on the `CollapsingToolbarLayout` instead. – Mike M. Mar 07 '17 at 20:19
  • Thank you. Please answer and I'll accept. If you have a sec, I asked another related question that I would really appreciate your feedback on: http://stackoverflow.com/questions/42658904/navigation-drawer-icon-disappearing-with-collapsing-tollbar If not, don't worry about it. Thanks again. – Mark F Mar 07 '17 at 21:48
  • Actually, I'll just mark this as a duplicate, as it's already been answered a few times. Thanks, though. I'll have a look at your other question later on, if I get a minute. Cheers! – Mike M. Mar 07 '17 at 23:28

0 Answers0