How to fix getActionBar method may produce java.lang.NullPointerException - android

I am using a toolbar as my actionbar in an activity. I am trying to add the method getActionBar().setDisplayHomeAsUpEnabled(true); to the Activity.java file for Up navigation for older devices.
The method produces the following error message in Android Studio:
Method invocation may produce java.lang.NullPointerException
The Up navigation on the toolbar works fine on newer devices...now I'm trying to figure out how to make sure it will work for older devices.
Please advise.
From build.gradle:
dependencies {
compile "com.android.support:appcompat-v7:22.1.0"
}
From AndroidManifest.xml:
android:theme="#style/Theme.AppCompat.NoActionBar.FullScreen"
From styles.xml
<style name="Theme.AppCompat.NoActionBar.FullScreen" parent="AppTheme">
<item name="android:windowNoTitle">true</item>
<item name="windowActionBar">false</item>
<item name="android:windowFullscreen">true</item>
from Activity.java
public class CardViewActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.cardviewinput);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
if (toolbar != null) {
// Up navigation to the parent activity for 4.0 and earlier
getActionBar().setDisplayHomeAsUpEnabled(true);
toolbar.setNavigationIcon(R.drawable.ic_action_previous_item);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onBackPressed();
}
});
}
}

Actually Android Studio isn't showing you an "error message", it's just a warning.
Some answers propose the use of an assertion, Dalvik runtime has assertion turned off by default, so you have to actually turn it on for it to actually do something. In this case (assertion is turned off), what you're essentially doing is just tricking Android Studio to not show you the warning. Also, I prefer not to use "assert" in production code.
In my opinion, what you should do is very simple.
if(getActionBar() != null){
getActionBar().setDisplayHomeAsUpEnabled(true);
}
Update:
In case you're using the support library version of the Action Bar, you should replace getActionBar() with getSupportActionBar().
if(getSupportActionBar() != null){
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}

First off, you need to set the toolbar as the support ActionBar.
Then if you're certain it's going to be there all the time, just assert it as != null. This will tell the compiler it won't be null, so the null check passes.
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.cardviewinput);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
assert getSupportActionBar() != null;
getSupportActionBar().setDisplayHomeAsUpEnabled(true); // it's getSupportActionBar() if you're using AppCompatActivity, not getActionBar()
}

Thank You Andrew for your answer.
If you have a Nav Drawer or something else that uses getSupportActionBar() you need to add assert getSupportActionBar() != null;
Peace,
Example:
#Override
public void setTitle(CharSequence title) {
mTitle = title;
assert getSupportActionBar() != null;
getSupportActionBar().setTitle(mTitle);
}

Try this :
private ActionBar getActionBar() {
return ((AppCompatActivity) getActivity()).getSupportActionBar();
}

What I have done is override the getSupportActionBar() method in my base Activity and add a #NonNull annotation. This way, I only get one lint warning in the base activity about how I use #NonNull annotation for something that has a #Nullable annotation.
#NonNull
#Override
public ActionBar getSupportActionBar() {
// Small hack here so that Lint does not warn me in every single activity about null
// action bar
return super.getSupportActionBar();
}

I created a generic class such as:
public final class Cast
{
private Cast() {}
/**
* Helps to eliminate annoying NullPointerException lint warning.
*/
#android.support.annotation.NonNull
public static <T> T neverNull(T value)
{
return value;
}
}
then I can use it for any call with NullPointerException warning for which I am sure that it will never happen, e.g.
final ActionBar actionBar = Cast.neverNull(getSupportActionBar());
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeButtonEnabled(true);
P.S. don't forget to add "com.android.support:support-annotations" to your gradle file.

add assert getSupportActionBar() != null; before getSupportActionBar().setDisplayHomeAsUpEnabled(true);

if(actionBar != null) {
actionBar.setHomeButtonEnabled(true);
actionBar.setBackgroundDrawable(ContextCompat.getDrawable(mContext,
R.drawable.action_bar_gradient));
}

use this theme: android:theme="#style/Theme.AppCompat.Light.NoActionBar"
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitle("Title");
setSupportActionBar(toolbar);
ActionBar actionBar = getSupportActionBar();
actionBar.setHomeButtonEnabled(true);
actionBar.setHomeAsUpIndicator(R.drawable.ic_action_previous_item);
actionBar.setDisplayHomeAsUpEnabled(true);

Alternatively you could assert actionbar to not null.Add the assertion before calling your actionbar as follows
assert getSupportActionBar() != null;
Final snippet would therefore look as follows:
setSupportActionBar((Toolbar) findViewById(R.id.toolbar));
assert getSupportActionBar() != null;
getSupportActionBar().setDisplayHomeAsUpEnabled(true);

Try this :
setSupportActionBar (toolbar);
if(getSupportActionBar () != null) {
assert getSupportActionBar () != null;
getSupportActionBar ().setDisplayHomeUpEnabled(true);
}
Note that setSupportActionBar(toolbar) should be before getSupportActionBar().

if(getSupportActionBar() != null){
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
OR
Replace the MainActivity extends AppCompatActivity to public class MainActivity extends AppCompatActivity

just check getSupportActionBar not equal to null
setSupportActionBar(toolbar);
if(getSupportActionBar() != null) {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setTitle("Daily Shopping List");
}

If you are importing
android.app.ActionBar
you have to use getActionBar()
and if you are importing
android.support.v7.app.ActionBar
use getSupportActionBar()

Related

Suppress NPE Warning for getSupportActionBar() when called inside Fragments [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 4 years ago.
I am getting a NullPointerException warning below at setDisplayHomeAsUpEnabled method.
if((getActivity()) != null) {
if(((AppCompatActivity) getActivity()).getSupportActionBar() != null) {
((AppCompatActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(false);
}
}
How do I fix this?
Because you don't put checks for NullPointerException
((AppCompatActivity) getActivity()).getSupportActionBar() gives actionbar object
but you are calling directly by
((AppCompatActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(false)
that is why system gives warning for NullPointerException.
if((getActivity()) != null) {
ActionBar actionBar = ((AppCompatActivity) getActivity()).getSupportActionBar();
if(actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(false);
}
}
Put above code. Your warning will remove.
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
Using toolbar is a preferred choice. If you use Toolbar and set the Actionbar, you don't need to chekc for NullPointerException.
Probable cause is if you are trying to do this out of the Activity Context you will get NullpointerException. It must set within the Activity Context.
Check your Manifest File, if you have set the theme as NoActionBar and trying to access Actionbar, change it to Apptheme.

getSupportActionBar setSubtitle only shows after activity recreation

I have a generic
MyActivity extends AppCompatActivity
I don't override the toolbar with a custom xml defined toolbar, just use the generated one Android provides.
I can set the title via your normal
getSupportActionBar().setTitle("foo");
but setting the subtitle via
getSupportActionBar().setSubtitle("bar");
doesn't set it. It remains blank. I'm doing this onCreate()
(I feel I've done this many times before with no fail)
Although I've noticed if I visit another activity, then return, the subtitle would then show... not on orientation change, not on recreate() but only when I'm returning from an activity.
I'm experiencing this on 5.0 and 7.0
For the time being I'll likely define my own Toolbar and move forward since that seems where most people have solutions for this same problem.
Relevant code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_replenishment_list);
ButterKnife.bind(this);
MyApplication.getInstance().getComponent().inject(this);
setupUI();
}
private void setupUI() {
setupActionBar();
}
private void setupActionBar() {
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
//TODO: not working unless activity is recreated...
// explore custom xml defined toolbar
//actionBar.setTitle("different title than what is defined in manifest"); <-- this does work, but not this
actionBar.setSubtitle(UserUtil.getFormattedFirstNameLastName(userService.getUserFromJWT(), this));
}
}
I have put the below code in my onCreate() method.
ActionBar actionBar = getActionBar();
if (actionBar==null) {
System.out.println("TEST NULL");
} else {
System.out.println("TEST NOT NULL");
}
The result is null. When I add the toolbar first it works fine.
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ActionBar actionBar = getActionBar();
actionBar.setSubtitle("TESTING");
Your getSupportActionBar or getActionBar will return null if you didn't set toolbar to it. You need to set the toolbar to your action bar before using getSupportActionBar or getActionBar.

Android how to use Toolbar in ListActivity?

I made a ListActivity:
public class NoteListActivity extends ListActivity{
}
and the following methods are no longer available:
setSupportActionBar(toolbar);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
But I can use:
setActionBar(toolbar);
getActionBar().setHomeButtonEnabled(true);
getActionBar().setDisplayHomeAsUpEnabled(true);
Note that I used Support library in other activities in the same project. It means I have the correct gradle dependency added.
How do I use ListActivity and android.support.v7.widget.Toolbar ?
You can't do that in ListActivity.
If you want to access getSupportActionBar(), you need to extend your class with AppCompatActivity.
My Suggestion : Don't use ListActivty as you want to use ToolBar. Create an Activity and then only have ListView within that Activity. It'll work just fine.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AppCompatCallback callback = new AppCompatCallback() {
#Override
public void onSupportActionModeStarted(ActionMode actionMode) {
}
#Override
public void onSupportActionModeFinished(ActionMode actionMode) {
}
#Nullable
#Override
public ActionMode onWindowStartingSupportActionMode(ActionMode.Callback callback) {
return null;
}
};
AppCompatDelegate delegate = AppCompatDelegate.create(this, callback);
delegate.onCreate(savedInstanceState);
delegate.setContentView(R.layout.saved_report_activity);
Toolbar toolbar= (Toolbar) findViewById(R.id.toolbar);
delegate.setSupportActionBar(toolbar);
delegate.getSupportActionBar().setDisplayShowHomeEnabled(true);
toolbar.setNavigationIcon(R.drawable.abc_ic_ab_back_mtrl_am_alpha);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
NavUtils.navigateUpFromSameTask(SavedReportActivity.this);
}
});
AppCompatActivity is the direct child class of FragmentActivity of support v4 .Read this article
http://android-developers.blogspot.it/2015/04/android-support-library-221.html
so for your requirement, public class NoteListActivity extends AppCompatActivity {...}
Just change your dependency,Like :
compile "com.android.support:appcompat-v7:22.1.0"
I was able to solve this by adding a support library toolbar style and then adding the style to my list activity in the manifest.
In styles.xml:
<style name="FullscreenActionBarStyle" parent="Widget.AppCompat.ActionBar">
In Manifest
<activity android:theme="#style/FullscreenActionBarStyle"
android:name="#string/my_activity_name"
android:label="#string/my_activity_label"/>
This should add a toolbar to your activity that you can add items to and process events with, etc...

ToolBar AppCompat & Action Bar backStack

I currently have an issue with the AppCompat Toolbar.
If I set the Theme to NoActionBar, no action is shown.
If I set the Theme normally, I can't set the action bar like :
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
if (toolbar != null) {
setSupportActionBar(toolbar);
}
Because you have no right to set the action bar if one is already set.
I simply would like to get the back arrow on my Fragments navigation like :
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
I can't figure out how to do that :/
try below code:
getSupportActionBar().setHomeButtonEnabled(true);
toolbar.setNavigationIcon(R.drawable.ic_back);
Provide a Parent Activity in the Manifest.
See http://developer.android.com/training/implementing-navigation/ancestral.html
You have to set this attribute in your style.
<style name="AppTheme" parent="Theme.AppCompat.Light">
<item name="windowActionBar">false</item>
</style>
Then in your Activity:
public class BaseActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
Toolbar mActionBarToolbar = (Toolbar) findViewById(R.id.toolbar);
if (mActionBarToolbar != null) {
setSupportActionBar(mActionBarToolbar);
getSupportActionBar().setHomeButtonEnabled(true);
}
}
}

Set a title in Toolbar from fragment in Android

I have been using the latest Toolbar from AppCompatv7 lib.I have placed a textview in the ToolBar ViewGroup And I want to set a title into this Textview from the fragment in my activity.In case of a custom action bar ((ActionBarActivity)getActivity).setcustomView(..) would have done the job.But due to use of this ToolBar I am not able to use that.Also I have implemented a method in my BaseActivity that is inherited by all Activities.This BaseActivity contains my method to initialize a sliding drawer to the left.I have to initialize the initDrawerLayout() method in activity else the drawer would not be initialized.And if I initialize it in fragment its giving me all empty results,neither the toggle button for drawer and nor is the custom title getting set.
This is my initDrawer code..
public void initDrawerLayout(String toolbar_text) {
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerRelative = (RelativeLayout) findViewById(R.id.drawer_relative);
if (mDrawerLayout != null) {
findViewById(R.id.drawer_btn_a).setOnClickListener(this);
findViewById(R.id.drawer_btn_b).setOnClickListener(this);
findViewById(R.id.drawer_btn_c).setOnClickListener(this);
findViewById(R.id.drawer_btn_d).setOnClickListener(this);
findViewById(R.id.drawer_btn_e).setOnClickListener(this);
findViewById(R.id.drawer_btn_f).setOnClickListener(this);
findViewById(R.id.drawer_btn_g).setOnClickListener(this);
findViewById(R.id.drawer_btn_h).setOnClickListener(this);
findViewById(R.id.drawer_btn_i).setOnClickListener(this);
findViewById(R.id.drawer_btn_j).setOnClickListener(this);
findViewById(R.id.drawer_btn_k).setOnClickListener(this);
findViewById(R.id.drawer_btn_l).setOnClickListener(this);
findViewById(R.id.my_layout).setOnClickListener(this);
Toolbar toolbar = (Toolbar) findViewById(R.id.my_awesome_toolbar);
toolbar.setBackground(getResources().getDrawable(R.drawable.icn_actionbar_background));
TextView mTitle = (TextView) toolbar.findViewById(R.id.toolbar_title);
mTitle.setText(toolbar_text);
mTitle.setTypeface(Typeface.DEFAULT_BOLD);
if (toolbar != null) {
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
toolbar.setNavigationIcon(R.drawable.ic_drawer);
mDrawerToggle = new ActionBarDrawerToggle(
this, mDrawerLayout, toolbar,
R.string.drawer_open, R.string.drawer_close
);
mDrawerLayout.setDrawerListener(mDrawerToggle);
toolbar.setNavigationOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (mDrawerLayout.isDrawerOpen(Gravity.LEFT)) {
mDrawerLayout.closeDrawer(Gravity.LEFT);
} else {
mDrawerLayout.openDrawer(Gravity.LEFT);
}
}
});
mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow,
GravityCompat.START);
mDrawerLayout.setScrimColor(getResources().getColor(
android.R.color.transparent));
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayShowTitleEnabled(false);
}
}
And this my code in the fragment..
((FirstActivity) getActivity()).initDrawerLayout(mFirst.name);
where mFirst is a object of class Person
and the toolbar code..
<android.support.v7.widget.Toolbar
android:id="#+id/my_awesome_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize">
<TextView
android:id="#+id/toolbar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="Toolbar Title"
android:textColor="#color/action_text-color"
android:textSize="18sp"
android:textStyle="bold" />
</android.support.v7.widget.Toolbar>
Please help guys..
I do this like this:
from the fragment call
getActivity().setTitle("your title");
But where you call it is important, because if each Fragment has it's own title, then one may override the other accidentally, which may be prevented like:
#Override
public void onResume() {
super.onResume();
Activity activity = getActivity();
if (activity != null) {
activity.setTitle(getString(R.string.my_title));
}
}
For example, two Fragments of the same Pager can not be in resume-state at the same time.
Also you can "cast", to call any function of your parent Activity like this:
YourActivity mYourActiviy = (YourActivity) getActivity();
mYourActivity.yourActivityFunction(yourParameters);
In Kotlin.
In fragment:
(activity as YourActivity).supportActionBar?.title = getString(R.string.your_title)
In activity:
setSupportActionBar(toolbar)
supportActionBar?.setDisplayHomeAsUpEnabled(true)
supportActionBar?.setDisplayShowHomeEnabled(true)
If you have setSupportActionBar in Your Activity then you can easily change the toolbar title from your fragment
((YourActivity) getActivity()).getSupportActionBar().setTitle("Your Title");
To allow a Fragment to communicate up to its Activity (to set your Toolbar Title), you can define an interface in the Fragment class and implement it within the Activity as described here: Communicating with Other Fragments.
The answer is writen below in the oncreateview method of fragments.
getActivity().setTitle("your name");
You can create Interface inside the Fragment. check below:-
public class MyFragment extends Fragment {
OnMyFragmentListener mListener;
// Where is this method called??
public void setOnMyFragmentListener(OnMyFragmentListener listener) {
this.mListener = listener;
}
#Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnMyFragmentListener) {
mListener = (OnMyFragmentListener) context;
mListener.onChangeToolbarTitle("My Fragment"); // Call this in `onResume()`
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}
#Override
public void onDetach() {
super.onDetach();
mListener = null;
}
#Override
public void onResume(){
super.onResume();
mListener.onChangeToolbarTitle("My Fragment");
}
// This interface can be implemented by the Activity, parent Fragment,
// or a separate test implementation.
public interface OnMyFragmentListener {
public void onChangeToolbarTitle(String title);
}
}
In Activity:-
public class MyActivity extends AppCompatActivity implements MyFragment.OnMyFragmentListener {
#Override
public void onChangeToolbarTitle(String title){
toolbar.setTitle(title);
}
}
This works for me. :)
This has worked for me, in Kotlin. Put this in your fragment class:
if (activity != null) {
(activity as MainActivity).supportActionBar?.title = getString(R.string.action_history)
}
If somebody struggles with this problem, this may be useful.
Basically you have 4 options, how to handle that:
use an interface in order to communicate with your activity, or any other convenient method, like an event bus.
you call getActivity().setTitle("Title"), but in this case you need to attach your Toolbar to the ActionBar by calling the setSupportActionBar() in your activity.
You can have a public instance of your Toolbar and access that instance from the fragment.
Finally, if you need the instance of your Toolbar(you may want to do something else with), you can simply get it this way:
Toolbar bar=Toolbar.class.cast(getActivity().findViewById(R.id.toolbar));
Well, the last option would solve the problem only if the Toolbar hasn't been passed to the setSupportActionBar method.
If it has been, then you need to call this method in your activity:
supportActionBar.setDisplayShowTitleEnabled(false),
which will solve the problem.
However, I would suggest to use ButterKnife which will make it a little bit cleaner, here an example:
Toolbar actionBar=findById(getActivity(),R.id.actionBar);
actionBar.setTitle("Title");
In Kotlin, I use
fun onAttach(...){
..
activity?.title = "My Title"
}
This work for me :
Toolbar toolbar = (Toolbar) getActivity().findViewById(R.id.id_toolbar);
toolbar.setTitle("New Title");
getSupportFragmentManager().addOnBackStackChangedListener(new FragmentManager.OnBackStackChangedListener() {
#Override
public void onBackStackChanged() {
FragmentManager.BackStackEntry lastBackStackEntry=null;
int lastBackStackEntryCount = getSupportFragmentManager().getBackStackEntryCount() - 1;
if(lastBackStackEntryCount >= 0 )
{
lastBackStackEntry = getSupportFragmentManager().getBackStackEntryAt(lastBackStackEntryCount);
}
Toast.makeText(MainActivity.this, ""+lastBackStackEntryCount, Toast.LENGTH_SHORT).show();
if(lastBackStackEntryCount == -1)
{
toolbar.setTitle("");
toolbar.setLogo(R.drawable.header_logo);
}
else
{
toolbar.setTitle(lastBackStackEntry.getName());
}
}
});
For me the problem was that for some reason the label was overwritten. I had to change it back to the string resource, in
navigation.xml
inside the fragment tag;
android:label="#string/android_trivia"
You can change the title of your toolbar on the event OnAttach, something like this
var toolbar = activity.FindViewById<Android.Support.V7.Widget.Toolbar>(Resource.Id.toolbar);
toolbar.Title = "New Title";
In your Activity declare the toolbar as public.
public class YourActivity : Activity
{
public Toolbar toolbar;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = .... // initilize your toolbar
}
}
Then, from your fragment
((YourActivity) getActivity()).toolbar.Title = "Your Title";
If you are using a custom toolbar, this will help you:
Toolbar toolbar = (Toolbar) getActivity().findViewById(R.id.toolbar);
toolbar.setTitle("Feedback");
You need to set the title in activity commiting the fragment and return the fragment
getSupportFragmentManager().beginTransaction().replace(R.id.main_fragment, mainFragment).commit();
toolbar.setTitle("ShivShambhu");
return contentFragment;
This works for me.
xml
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar_detail"
android:layout_width="match_parent"
android:layout_height="55dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
app:contentInsetStart="0dp"
app:contentInsetStartWithNavigation="0dp"
android:background="#color/tool_bar_color">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:gravity="center">
<TextView
android:id="#+id/toolbar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="#fff"
android:textSize="16sp" />
</LinearLayout>
</android.support.v7.widget.Toolbar>
Find TextView Id from toolbar
if you are using Activity
TextView mTitle = findViewById(R.id.toolbar_title);
mTitle.setText("set your title");
if you are using Fragment
TextView mTitle = view.findViewById(R.id.toolbar_title);
mTitle.setText("set your title");
Here's one simple solution.
In your activity that extends AppCompatActivity, you can do this:
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
The inside its fragment, you can do this:
((AppCompatActivity) getActivity()).getSupportActionBar().setTitle(title);

Categories

Resources