How can i change this code to use it in the activity?

70 views Asked by At

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.
                                       }
            });
2

There are 2 answers

0
Karim On

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

4
dipali On

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