0

I need to change and update the ActionBar title according to the fragment that is called.

I used this example method:

=Method 1===============================
Activity > Codigo
public void setActionBarTitle(String title) {
    getSupportActionBar().setTitle(title);
}

Fragmento > Codigo
public void onResume(){
        super.onResume();

    // Set title bar
    ((MainFragmentActivity) getActivity())
            .setActionBarTitle("Your title");

}

=Method 2================================

Activity:
public void setActionBarTitle(String title){
    YOUR_CUSTOM_ACTION_BAR_TITLE.setText(title);
}

Fragment:
((MainFragmentActivity) getActivity()).setActionBarTitle(YOUR_TITLE);

I have both ways and none updates the title in real time in real time.

But it is not working correctly, because it does not update the title according to the screen / fragment change. It takes a while to upgrade, and I wish it were in real time.

What is missing?

EKN
  • 1,886
  • 1
  • 16
  • 29
TiagoIB
  • 439
  • 7
  • 25

2 Answers2

3

First create Interface

public interface ToolbarInterface {

public void getToolbarResources(String title, int visibility);
}

Implement it in Your activity

public class NavigationDrawerActivity extends AppCompatActivity
    implements ToolbarInterface

And override interface method in activity

 @Override
public void getToolbarResources(String title, int visibility) {
        getSupportActionBar().setTitle(title);
    }
}

In Fragment call interface in Oncreete method like

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
 ToolbarInterface toolbarCallback = (ToolbarInterface) getActivity();
}

And set title from onCreateView Method like

toolbarCallback.getToolbarResources("Toolbar title",1);
Anil
  • 1,087
  • 1
  • 11
  • 24
  • I'm using this process for updating cart item at cart icon on action bar ,the cart icon is updated when i delete one item from cart and need changes on cart counter but it is working properly because i'm using this process in adapter class but i'm also using same adapter class in other activity ,when i delete item from that activity than app crashes. – Farhana Naaz Ansari Jul 11 '17 at 12:06
  • @Anil Sorry, but I did not understand. The interface where it will be created (ToolbarInterface)? In Activity Main? My activity is this way : public class MainActivity extends AppCompatActivity {} – TiagoIB Jul 11 '17 at 14:29
  • @TiagoIB create seprate file for intarface(ToolbarInterface) – Anil Jul 12 '17 at 08:19
0

You have to impliment OnBackStackChangedListener it will override onBackStackChanged method.

MainFragmentActivity

@Override
public void onBackStackChanged() {
    try {
       
        Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.home_container);
        fragment.onResume();

    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

Fragment

public void onResume(){
    super.onResume();

// Set title bar
((MainFragmentActivity) getActivity())
        .setActionBarTitle("Your title");
}

onBackStackChanged will call every time when your fragments changed from one to another

NOTE- don't forget use getSupportFragmentManager().addOnBackStackChangedListener(this); And fragmentTransaction.addToBackStack(null);

Hope it will help you.

EDIT

  • First impliments this interface like this - class MyActivity extends AppCompatActivity implements OnBackStackChangedListener - Example and Documents
  • Then write this code in onCreate() method getSupportFragmentManager().addOnBackStackChangedListener(this); This method will take care of changing of fragments.
  • When you are replacing fragment then use this method with FragmentTransection fragmentTransaction.addToBackStack(null); This method will add your fragment in stack and when you press back button it will show previous fragment from stack - Example and Documents
Community
  • 1
  • 1
Vishal Chhodwani
  • 2,567
  • 5
  • 27
  • 40
  • Okay, I end up in parts. Would you have any examples of where and how to implement onBackStackChanged in the snippet? My main doubt is in the implementation of this section: NOTE- don't forget use getSupportFragmentManager().addOnBackStackChangedListener(this); And fragmentTransaction.addToBackStack(null); – TiagoIB Jul 11 '17 at 14:43
  • @TiagoIB Please see my Edited Answer. – Vishal Chhodwani Jul 12 '17 at 05:58