How to differentiate action for SearchView submit based on Fragment? - android

I have multiple fragments in my app. I want to add search function to may app. When i add search view in toolbar, the listener is in MainActivity. The problem is when i submit the query, the action for every fragment is same. Can i differentiate the action for every fragment ?
How to do that?

First of all, I guess you have added the SearchView in Activity through menu.
If so then you can implement the menu inside each fragment instead of Activity. Then you can get Search functionality for each fragment.
To enable Option Menu for fragment you have to override onCreate like below:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
Then you have to inflate your menu in
onCreateOptionsMenu like below:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
// TODO Add your menu entries here
super.onCreateOptionsMenu(menu, inflater);
}

Related

Add Menu to Activity's supportToolBar from a Fragment

I have an Activity with a Toolbar that I set as the supportActionBar. From this Activity I have various Fragments each with a customized ActionBar. I am able to call menu.clear() to remove the existing Menu but I am however unable to add another Menu in the same Fragment. This seems strange because menu.clear() behaves just as I would expect, but when calling inflater.inflate(R.menu.my_custom_menu,menu); appears to do nothing.
Example Fragment where I wish to modify the supportActionBar:
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
actionBar = ((AppCompatActivity)getActivity()).getSupportActionBar();
mGroupViewModel =
ViewModelProviders.of(requireActivity()).get(GroupsViewModel.class);
}
...
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
//Inflating seems to do nothing.
Log.i(TAG,"IN THE ONCREATEOPTIONSMENU FOR FRAGMENT.");
inflater.inflate(R.menu.group_edit_toolbar,menu);
super.onCreateOptionsMenu(menu, inflater);
}
#Override
public void onPrepareOptionsMenu(Menu menu) {
Log.i(TAG,"ONPREPARE OPTIONS MENU IN FRAGMENT.");
menu.clear();
super.onPrepareOptionsMenu(menu);
}
Clearly there is something I don't understand but I can't narrow down on what that problem is.
Would a better approach be to have each Fragment have their own Toolbar instead of all my fragments modifying the hosting activity's supportActionBar?
UPDATE
After further testing, I notice that if I try to assign a local MenuItem in my Fragment, I receive a null pointer exception unless I first inflate a menu in the Fragment itself. This is leading me to think that I am not hijacking control of the Activity's supportActionBar in the Fragment, but rather am trying to create a separate ActionBar for the Fragment. Would anyone be able to supplement my thinking here?
Fragment's menu callbacks:
MenuItem editItem;
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
Log.i(TAG,"IN ONCREATEOPTIONS");
menu.clear();
//MUST INFLATE MENU OTHERWISE WE GET NULL ERROR.
inflater.inflate(R.menu.home_actionbar,menu);
editItem = menu.findItem(R.id.action_edit_group);
Log.i(TAG,"edititem: "+editItem.getItemId());
super.onCreateOptionsMenu(menu, inflater);
}
// This is called every time the Menu opens.
#Override
public void onPrepareOptionsMenu(Menu menu) {
Log.i(TAG,"IN THE on prepare FOR FRAGMENT.");
menu.findItem(R.id.action_create_group).setVisible(false);
menu.findItem(R.id.action_create_group).setEnabled(false);
if(owner.equals(currUser)){
menu.findItem(R.id.action_edit_group).setEnabled(true);
menu.findItem(R.id.action_edit_group).setVisible(true);
} else {
menu.findItem(R.id.action_edit_group).setVisible(false);
menu.findItem(R.id.action_edit_group).setEnabled(false);
}
super.onPrepareOptionsMenu(menu);
}
onPrepareOptionsMenu is always called after onCreateOptionsMenu and there you're deleting your recently inflated menu!
Just delete onPrepareOptionsMenu and it should work fine.

How to remove a specific android SubMenu from toolbar?

I have a Fragment that, when added to a view, should have certain accompanying menu items in the toolbar. So when the Fragment is created, I add a submenu to the activity's toolbar menu.
The problem is that if I leave and return to the fragment, then I get multiple instances of that submenu. So what I'd like to do is remove a specific submenu from the toolbar menu. All I've been able to find is a way to remove all items from the menu, but I don't want that either as there's one other item I'd like to retain.
Does anybody have a strategy for removing a specific submenu?
The sub menu should not be created twice if you do correctly.
In your fragment:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true); //this line is important
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
// TODO Add your sub menu entries here
super.onCreateOptionsMenu(menu, inflater);
}

Show navigation drawer menu item only when a Fragment is shown

I am trying to show a button in the actionbar when a Fragment is shown and to hide the button when the other Fragment are shown.
I Override the onCreateOptionsMenu method:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.main, menu);
MenuItem item= menu.findItem(R.id.action_example);
item.setVisible(true);
super.onCreateOptionsMenu(menu,inflater);
}
And use setHasOptionMenu(true):
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
I have done a test and I noticed that initially the button doesn't appear in the other Fragment , but after I open the Fragment in which I put this code above, the button is shown also in the other Fragment.
What you are missing is removing the optionsMenu in the onDestroy of the fragment. The behaviour you describe is logical with your code: when the Fragment is created you also create the options menu. It will not automatically be destroyed when the Fragment is destroyed.

how to remove menu from fragment back pressed android

I am developing a application that should support on both phone and tablet.
In this application i am using fragments from android.
Now the flow of the application is like
MainActivity --> Fragment1 --> Fragment2
In this application , i want a menu item that should show only in Fragment2 along with activity's menu items.
So i have tried one solution like adding globle menu items in MainActivity and in Fragment2 replacing the whole MainActivity's menu with Fragment2 specific Menu.
setHasOptionsMenu(true);
inside onCreateView , and implement this method.
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_f, menu);
super.onCreateOptionsMenu(menu,inflater);
}
Now its work exactly fine for phone layout , but when its come to tablet problem arise.
Here is my ScreenShots.
Fragment 1
Fragment 1 and Fragment 2 combination when pressing 9 in keybord(Tablet mode ).
And Finally when i pressed 9 again to come back to pHone view it shows me extra menu item.
I just marked a extra menu item in Image. So why this me coming and how can i resolve it ?
You need to hide the menu group like this in your fragment 1
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
it'll make a call to onPrepareOptionsMenu where you can hide your menu group added by fragment2
#Override
public void onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
menu.setGroupVisible(0, false);
}

ABS Menu Item not appearing in Android 2.3

I am implementing ABS within my application, and I have a FragmentPagerAdapter that allows the user to swipe/select different tabs.
Each of these tabs contains a different fragment. Basically, my problem is that on an Android 2.3.6 device that I'm testing with, when the activity is first called, the first tab-fragment doesn't inflate the menu it is supposed to. However, once I swipe to another tab and go back, it appears.
Here is the code within the fragment:
public void onPrepareOptionsMenu(Menu menu) {
menu.clear();
MenuInflater inflater = getSherlockActivity().getSupportMenuInflater();
inflater.inflate(R.menu.menu_ongoing_fragment, menu);
}
Meanwhile, it works completely fine on an Android 4.2 device (the menu is inflated the first time the activity is launched) Does it have something to do with the order in which these two devices are calling the methods?
It's not working because of this:
menu.clear();
Remove that.
You should have the following code on your fragment:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
};
and instead of "onPrepareOptionsMenu()" use this:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_ongoing_fragment, menu);
super.onCreateOptionsMenu(menu, inflater);
}
I hope this solves your problem!

Categories

Resources