Changing Action Bar items and title on adding Fragments - android

Let's say I have a FragmentA visible.
I add a FragmentB (keyword: add, not replace), add it to the Fragment back stack and commit.
The issues I'm having doing this is:
1) The action buttons of the action menu of FragmentB are added, but those of FragmentA are not removed.
2) The title of the ActionBar does not change (despite calling getActivity().setTitle("FragmentB") in onResume() of FragmentB.
I can resolve both of these by calling replace instead of add when showing FragmentB however, for quite a few reasons I specifically need to add the Fragment instead (one of them being, I need to retain the state of FragmentA while showing B).
So how would I go about updating the ActionBar correctly as described?

Try this piece of code:
getActivity().getActionBar().setTitle("FragmentB");

Use this code in your Activity.. (for setting title to your fragment).
public void setActionBarTitle(String title) {
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_TITLE);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setTitle(title);
}
from your fragment onResume(), call this using
// Set title bar
((MyActivity) getActivity())
.setActionBarTitle("Fragment A");
and in each fragment you need to override onCreateOptionsMenu() to load your menus of that fragment.

Related

How to change ActionBar Item depending on Fragment?

I am trying to create a selected and unselected Item on a the ActionBar in my app. For example, when the user is in a fragment if they click the Item in the ActionBar, the ImageView will switch from a grey to a red square and if they switch to a different fragment then come back it will still be selected. So each fragment can either be selected or unselected based upon the color of the square and if the user clicked it or not. I am unsure how to implement this, anything helps?
Based on the location of the item in the ActionBar, this can be presented as a fragment option. Each fragment that gets inflated has the chance to inflate its own menu items into the action bar. This can be done by calling setHasOptionsMenu(true) in fragment's onCreate or maybe in the constructor. This will cause the onCreateOptionsMenu / onPrepareOptionsMenu / onOptionsItemSelected methods to be called and this gives you the chance to add your menu item which you can control based on your fragment's state.
First off, for saving state in a fragment, you can use onSaveInstanceState() to remember your selected state. You would need a property on your fragment:
private boolean mSelected;
In your onSaveInstanceState() override you save this value:
outState.putBoolean("selected", mSelected);
Then in onCreate() you can recover the value:
if (savedInstanceState != null) {
mSelected = savedInstanceState.getBoolean("selected");
}
And in onCreateView() you use the selected property to set up your ImageView:
imageView.setBackground(mSelected ? R.color.gray : R.color.red); // for example
So now when your user switches away from the fragment then comes back, the ImageView will have the same selection state as before.
Next thing is the action bar. Fragments can make their own changes to the menu. Start by calling setHasMenuOptions(true) in your fragment's onCreateView(). Then you override onCreateOptionsMenu() and onOptionsItemSelected() just like you would for an activity.

Android Fragments - How to change the ActionBar on a different fragment?

I have a 3 page Fragment in my app, but I need each fragment to have a different ActionBar. For one fragment I have set the current code in the OnCreateView method that adds in an EditText to the ActionBar:
//mainActivityContext is the context for the Main Activity (This is a fragment file)
ActionBar actionBar = mainActivityContext.getActionBar();
// add the custom view to the action bar
actionBar.setCustomView(R.layout.actionbar_view);
search = (EditText) actionBar.getCustomView().findViewById(R.id.searchfield);
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM
| ActionBar.DISPLAY_SHOW_HOME);
Yet this EditText stays persistent throughout all of the ActionBar menus. I only want it on one. I have tried everything, menu.clear();, setHasOptionsMenu(true);, inflater.inflate(R.menu.different_file, menu);, but nothing has worked.
Any help?
There is a very good approach to go about this situation:
on each fragment's onActivityCreated() method call setHasOptionsMenu(true);
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setHasOptionsMenu(true);
}
now you can override onCreateOptionsMenu() and onOptionsItemSelected() inside each fragment.
And also dont forget to call getActivity().supportInvalidateOptionsMenu(); inside onResume() of fragment.
I think this sample by google will be very helpful.
Since the actions are populated by the activity's options menu you can use Activity#invalidateOptionsMenu(). This will dump the current menu and call your activity's onCreateOptionsMenu/onPrepareOptionsMenu methods again to rebuild it.
If you're using action bar tabs to change your fragment configuration there's a better way. Have each fragment manage its own portion of the menu. These fragments should call setHasOptionsMenu(true). When fragments that have options menu items are added or removed the system will automatically invalidate the options menu and call to each fragment's onCreateOptionsMenu/onPrepareOptionsMenu methods in addition to the activity's. This way each fragment can manage its own items and you don't need to worry about performing menu switching by hand.

Android: How to completely remove ActionBar

How can I completely remove the ActionBar from Certain Fragments. I want to remove it not just to hide it. I have Actionbar Tab Navigation. From that i added a new Fragmnt which didn't need actionBar. So i need to remove it. I can't hide it because when i pressed back button and moved to Tabs section, i need to show Action bar again.
This is how i am replacing Fragments.
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.add(R.id.frame_container, fragment)
.addToBackStack(null).commit();
The action bar is not part of your fragment. It's part of your activity.
You can create a new Activity with a theme Theme.Holo.Light.NoActionBar and open this Activity with :
getActivity().startActivity(new Intent(getActivity(), SecondActivity.class));
And inside your second activity you can display the fragment you want.
I recommend you this post to understand more the differences between Fragment and Activity: https://stackoverflow.com/a/10515807/3112836
If you're not using actionbar.hide() just because you have to show Action bar in other fragment, than I think you should give it a try by using :
actionbar.hide() inside onAttach method of your fragment and actionBar.show() inside your onDestroy() method.

Remove actionbar menu item when closing fragment

I'm using the code from the Example section of this page http://developer.android.com/guide/components/fragments.html
In landscape mode, the activity displays two fragments side-by-side, and on rotation, it launches a new activity containing the "detail" fragment.
The problem is that if I add a menu item to the actionbar from the detail fragment then rotate the screen to portrait to launch the detail fragment in a new activity then exit the activity, the menu item is still displayed even though the fragment supplying the menu item has been removed.
I've tried removing the detail fragment with a FragmentTransaction in onResume and then calling invalidateOptionsMenu(), but it doesn't remove the menu item.
I'm using ActionBarSherlock, and I've also tried supportInvalidateOptionsMenu()
Has anyone else run into this problem?
I thought I was removing the fragment, but I wasn't. After removing the fragment in onResume, my problem was solved.

Navigation drawer title according to the Drawer item

I have used getSupportActionBar().setTitle("drawer_title"); and the title changes according to drawer item. But when I go back the title does not get updated. for eg: If I'm at dashboard-fragment toolbar-title is dashboard and after that when I go to message-fragment toolbar-title changes to message, but, after when I press back button it goes back to dashboard-fragment but the title does not updated to dashboard it remains message.
How could I update the title again??
In your fragment use requireActivity() to get the activity to the activitiy's SupportActionBar.
So, to set the title in the fragment then use:
((AppCompatActivity) requireActivity()).getSupportActionBar().setTitle("");
Add following line in onViewCreated method of your fragment:
getSupportActionBar().setTitle("Your title here");
Use getSupportActionBar().setTitle("drawer_title"); inside onResume() method of your fragment.
This works for me I just used getActivity().setTitle("fragment_title"); inside onCreateView() method of fragment.

Categories

Resources