andej adhikari
andej adhikari

Reputation: 25

Change Title of Action Bar from Fragmetn to Activity

I have One MainActivity with Button A to navigate to Fragment A and initiall Title Set as "This is Main Title"

When I click Button A, Following Code gets Executed

   getSupportActionBar().setTitle("Profile");
            getSupportFragmentManager().beginTransaction().replace(R.id.container,new TrialProfile()).addToBackStack(null).commit();

and Now the Title Bar Text chnage to "Profile" But The Problem is When I press Back Button, The Title remains Same. I want it to change to Original One i.e "This is Main Title"

Can you please help me with this?

Upvotes: 0

Views: 82

Answers (3)

Android Geek
Android Geek

Reputation: 9225

To Change the title of Action Bar from Fragment to Activity

Create a method in activity like below:

public void setTitleActionBar(String title) {
        getSupportActionBar().setTitle(title);
    }

Now you can call the method in fragment and activity for changing the title:

In Fragment:

  ((HomeActvitiy) getActivity()).setTitleActionBar("Fragment title");

In Activity call the method before fragment replace like below:

 setTitleActionBar("My Profile");
        getSupportFragmentManager().beginTransaction()
                .replace(R.id.home_container, new MyProfileFragment())
                .commit();

I hope its work for you.

Upvotes: 0

A.gubhaju
A.gubhaju

Reputation: 1

(getActivity()).setTitle("Profile");

Upvotes: 0

sagar sonar
sagar sonar

Reputation: 46

call this again on your current activity in oncreate method

getSupportActionBar().setTitle("This is Main Title");

Upvotes: 0

Related Questions