Issue
I'm writing an app that involves several activities: each of them is composed by more fragments. In almost each fragment I want to set-up a different toolbar... where should I do that? I used to do almost everything inside the onCreate() or the onCreateView(), but now I'm asking myself: maybe it's more correct to put it in the onStart()?
I know how activity and fragment lifecycles work... but I didn't find an answer to my specific case.
Here's the code I used in one of the fragments:
// Toolbar setup
setHasOptionsMenu(true);
AppCompatActivity activity = (AppCompatActivity) getActivity();
ActionBar actionBar = activity.getSupportActionBar();
if (actionBar != null)
actionBar.setDisplayHomeAsUpEnabled(true);
Toolbar tb = (Toolbar) activity.findViewById(R.id.toolbar_main);
tb.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
FragmentManager mng = getFragmentManager();
mng.popBackStack();
}
});
if (activity.getSupportActionBar() != null)
activity.getSupportActionBar().setTitle(R.string.toolbar_title);
tb.setVisibility(View.VISIBLE);
Here's a part of the activity_main.xml file where R.id.toolbar_main is defined:
<!--- [...] --->
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar_main"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<!--- [...] --->
Solution
Do it in .onCreate() method - see Google's sample project for Material design: https://github.com/chrisbanes/cheesesquare
Answered By - Alex Shutov
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.