Inherited the MainActivity from some BasicActivity in order to avoid code repetition in new BasicActivity activities. In BasicActivity made a public method for changing the title, which is displayed in the toolbar:
public class BasicActivity extends AppCompatActivity { private Toolbar toolbar; // ... protected void initToolbar() { toolbar = (Toolbar) findViewById(R.id.toolbar); toolbar.setTitle(R.string.app_name); toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener(){ // ... } // ... public void changeToolbarTitle(String title){ toolbar.setTitle(title); } } From the MainActivity , or rather from onCreate() , this method can be easily called:
changeToolbarTitle("test"); But how to do the same from other classes that are not Activity (for example, from the heir Fragment )? I tried:
MainActivity.this.changeToolbarTitle("Test");
getActivity().changeToolbarTitle("Test");? - pavlofffCannot resolve methodif you call from the fragmentonCreateView. - Hokov Gleb