-1

i have this code to set the title while adding the fragment:

if (!HomeActivity.checkLoading)
        SettingsMain.showDilog(getActivity());
    Call<ResponseBody> myCall = restService.getHomeDetails(UrlController.AddHeaders(getActivity()));
    myCall.enqueue(new Callback<ResponseBody>() {
        @Override
        public void onResponse(Call<ResponseBody> call, Response<ResponseBody> responseObj) {
            try {
                if (responseObj.isSuccessful()) {
                    Log.d("info HomeGet Responce", "" + responseObj.toString());

                    JSONObject response = new JSONObject(responseObj.body().string());
                    if (response.getBoolean("success")) {
                        responseData = response.getJSONObject("data");
                        HomeActivity.checkLoading = false;
                        getActivity().setTitle(response.getJSONObject("data").getString("page_title"));

and i want to add in the activity addOnBackStackChangedListener so how can i use that code inside this in the activity to set the title while going back :

getSupportFragmentManager().addOnBackStackChangedListener(
            new FragmentManager.OnBackStackChangedListener() {
                public void onBackStackChanged() {

                    // Update your UI here.
                                       }
            });
ankuranurag2
  • 2,300
  • 15
  • 30

2 Answers2

0

I am not sure this is what you want, but you can try

    activity?.supportActionBar?.title = "some title"

I use it to change the title when selecting different tab in the tabbar

Karim
  • 962
  • 7
  • 10
0

you can manage onbackpressed method in activity like this:

@Override
public void onBackPressed(){
    FragmentManager fm = getFragmentManager();
    if (fm.getBackStackEntryCount() > 0) {
        Log.i("MainActivity", "popping backstack");
        fm.popBackStack();
        Fragment fragment = getFragmentManager().findFragmentByTag("YOUR_TARGET_FRAGMENT_TAG");
        if (fragment instanceof FragmentA) {
            // add your code to change title of toolbar
        }
    } else {
        Log.i("MainActivity", "nothing on backstack, calling super");
        super.onBackPressed();  
    }
}
dipali
  • 10,966
  • 5
  • 25
  • 51
  • how can i add that code in there , because i have two fragments but its change with data from data base , so how can i solve it ? – Talal Awija Jan 28 '19 at 10:25
  • Please add above code in activity onbackpressed and find which fragment is running, based on this you can change bg and title – dipali Jan 28 '19 at 10:59
  • i have fragment B is running , but it depend to the data so its change its name, for example : button1 in fragment A => run fragment B with title "title1" button 2 in fragment A => run fragment B with title "title2" and so on.. – Talal Awija Jan 28 '19 at 11:03
  • didn´t wark man , when i go back its put the title that i put in this code , and keep stuck like that after i reload new fragment – Talal Awija Jan 28 '19 at 12:20