I am using navigation drawer with fragments.
Generally, I want the fragments to change the activity's toolbar title.
When starting the app, I supply a default fragment by using fragmentManager replace method.
But the fragment can't change the title of the activity's toolbar.
On the other hand, when changing fragments after App is started, it can change the activity's toolbar title
Can someone elaborate on this?
Thank you
Best regards Morten
Related
I have an Android App which consists of several Fragments.
Each time the fragment is shown I set the ActionBar title to this fragment.
I did this with
getActivity().setTitle("abc");
Later in the App I needed to work a bit more with the ActionBar of a Fragment. So I had to change the title like this:
ActionBar actionBar = ((AppCompatActivity) getActivity()).getSupportActionBar();
if (actionBar != null)
actionBar.setTitle("xyz");
This works perfectly fine and changes the title to "xyz".
Still when changing to another Fragment that uses setTitle("abc") from the Activity itself, the title still stays at "xyz". Once I've done this for the first time, I can only change the title with the getSupportActionBar() object.
My question is: is this normal? Does this call transform / invalidate the normal Activity-Title somehow?
If you check the Activity Source Code, setTitle() method sets the text to actionBar itself.
Consider there are two fragments, A and B.
Fragment A will be shown by default in the Activity.
So later when you update the title from fragment B, you use getSupportActionBar() or getActionBar() method and update the title.
Since this is the recent change that is occured, even if you go back to the previous fragment,that is fragment A, the title will be same, as set in the fragment B.
If you want to change the title again, do it in onResume() method of the fragment A.
In my app I want to remove toolbar title for one fragment only. When I use the following code
((AppCompatActivity)getActivity()).getSupportActionBar().setDisplayShowTitleEnabled(false);
it also removes title from all other fragments too. Is there any other way to remove title for only one fragment and keeping it in others. I also tried removing title attribute from navigation drawer implementation but then it starts showing app name in title. I don't want anything in this fragment . How this can be done?
I have an AppCompatActivity with DrawerLayout and a NavigationView. I can start three different fragments from the NavigationView.
I want to show the checked status in the navigation drawer according to the fragment I am in.
It works fine, when navigating through the navigation drawer. But I don't know, how to achieve that also with the back button.
Probably I need to use something like
navigationView.getMenu().getItem(0).setChecked(true);
but how do I get ahold of the NavigationView from within the fragment?
Or, other way around, I would need to handle that in the main activity. Then I must need to know, which fragment is active at the moment.
I have made a nice app with a whole bunch of activities , then i needed a navigation drawer and found out that you need to have only one activity for the whole app and the individual different screens should be fragments that are inserted at runtime.
my question is :
How to convert the entire app to use fragments instead of activities ? (eg: how to preserve activity hierarchy , show a main activity when the user opens the app , different actionbar for each screen , etc...)
There is no magic converter, you need to convert manually each activity to be extended from Fragment
and add few must methods like onCreateView for the fragment instead of SetContentView of activity.
Regarding the actionbars, it sits on the Main Activity so you need to create callback events from each fragment to the main activity in order to control the action bar.
Navigation drawer has nothing related to fragments.
If you wish you can put it into activities also.
What I created was a BaseActivity with layout having navigation drawer and all other activities extend BaseActivity so that each of your activity will have drawer. Only you need to change content page for particular activity.
happy coding.
I got a navigation drawer on the activity of my app, but once I get the fragment parts of my app running (through a fragmentActivity) I have trouble programming a navigation process over there. I want my users to be able to navigate from one fragment to another fragment using the navigation drawer, making life easier.
Does anyone have a solution to this?
I think what you want is a ViewPagerIndicator, and to use a ViewPager with a FragmentPagerAdapter to page your Fragments.