SearchView automatically closed - android

In my case, I have activity with 2 containers for fragments.
One container show Fragment with list, second show detail information on the selected list item. Second container GONE by default.
In activity I have SearchView, which instance I get from fragment like that:
#Override
public void onPrepareOptionsMenu(Menu menu) {
MenuItem searchMenuItem = menu.findItem(R.id.action_search);
rxSearchView = (RxSearchView) searchMenuItem.getActionView();
rxSearchView.setSearchListener(taskListPresenter);
rxSearchView.setOnCloseListener(() -> {
taskListPresenter.clearSearch();
return false;
});
}
and, I use method setHasOptionsMenu(true); in OnCreateView();
Problem case:
1.User click on the searchView, and get filtered element:
2. User click on the element, calling next method:
`((MainActivity)getActivity()).showSecondaryFragment(TaskDetailFragment.newInstance();`
In MainActivity it's look like that:
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.ltRightContainer, baseFragment)
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
.commit();
After that, searchView automatically closed, like screen below.
How fix that moment? I don't want it automatically closing.
RxSearchView just wrapper with RxJava implementation, without ovverride methods.

Second fragment just call method setupMenu(R.menu.menu_my_task);
It makes searchView from first fragment close.

Related

Change fragment options menu after user sets a value in DialogFragment

I have a Fragment A which shows a MenuItem Mi in the toolbar. On clicking Mi, I am showing a DialogFragment Df to the user to set a value V.
I am passing this value to fragment A by implementing a callback listener interface.
Once the value is set, I want to hide Mi from toolbar menu of fragment A.
I wanted to handle this inside onPause() and onResume() of fragment A, but showing a DialogFragment doesn't change the lifecycle of fragment. I was wondering how to approach this problem.
How can I achieve this thing?
I did this using a callback listener in my fragment A which listens to DialogFragment Df. Once the value V is set, this callback method in A is invoked. Inside this method, I am using V and setting a flag to indicate V has been set and then calling invalidateOptionsMenu().
Refer this for implementing your own callback.
How to send data from DialogFragment to a Fragment?
public void myCallback(int V){
//Use V according to my logic
vIsSet = Boolean.TRUE;
getActivity().invalidateOptionsMenu();
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater){
super.onCreateOptionsMenu(menu,inflater);
if(vIsSet) {
menu.removeItem(MENU_ITEM_ID);//item id of Mi
}
}

onBackPressed not called when search view is open

I click on the search view and enter data to search, the result is shown and I click the search result to go to Detail Fragment and now if I press back button it doesn't work. I have to press back button twice to go back to ListFragment. However it works fine when I press up button.
What could be the reason for this ?
Edit 1 : (Adding code snippet)
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
menu.clear();
inflater.inflate(R.menu.defect_search, menu);
mIsMenuInflated = true;
mSearchItem = menu.findItem(R.id.action_search);
mSearchView = (SearchView) MenuItemCompat.getActionView(mSearchItem);
mSearchView.setQueryHint(getString(R.string.search_defect_hint));
mSearchView.setOnQueryTextListener(this);
mSearchView.setOnQueryTextFocusChangeListener(this);
super.onCreateOptionsMenu(menu, inflater);
}
public void onClickDefect(Integer defectId) {
hideKeyboard(mActivity);
getActivity().getSupportFragmentManager()
.beginTransaction()
.add(R.id.frame_content, DefectTabFragment.newInstance(defectId))
.addToBackStack(null)
.commit();
}
And In onBackPressed() of Activity I pop the fragment.
If your SearchView is in open state then you should press back button twice to go back to ListFragment.
From Documentation:
By default, the searchable activity receives the ACTION_SEARCH intent
with a call to onCreate() and a new instance of the activity is
brought to the top of the activity stack. There are now two instances
of your searchable activity in the activity stack (so pressing the
Back button goes back to the previous instance of the searchable
activity, rather than exiting the searchable activity).
Hope this will help~

Working with OptionsMenu in nested fragment not updating

I displayed a fragment A that implements a ViewPager with several fragments (nested fragments).
In my nested fragments, I inflate a menu with the following method.
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.my_menu, menu);
super.onCreateOptionsMenu(menu, inflater);
}
This question was already asked here. And i tried all the answers its not working.
My issue is
Everything working fine.but when i open another fragment(it have not any option menu) and get back to previous view pager fragment while clicking menu item onOptionsItemSelected not firing. When i swipe fragment in viewpager and come back to the previous one, when i click menu item its firing.
Its because viewpager maintain 3 fragment alive at a time. so when you come back, it set menu visibility status true to last fragment. thats why your menu item click not firing.
Use the following in the fragment where you keeping a viewpager in your case fragment A.
private boolean isInitial=true;
#Override
public void onResume() {
super.onResume();
if (!isInitial) {
int pos = viewpager.getCurrentItem();
if (pageAdapter.getItem(pos).getUserVisibleHint() && pageAdapter.getItem(pos).isVisible()) {
pageAdapter.getItem(pos).setMenuVisibility(true);
}
} else {
isInitial = false;
}
}

Android: Send info from activity to fragment

In my MainActivity I have an active search button on my Action Bar/Tool bar/menu. I was wondering if there is a way to filter a list in a Fragment form that active search? Thanks in advance!
It would be even better if each fragment (they are in a viewpager + action bar with tabs) Can have its own action bar!
You should create a custom adapter for your activity's view pager
Then show fragments from adapter, now you have 2 choices for your purpose :
define custom toolbar for each fragment in it's own layout (.xml) file and then use it or
pass data from activity to fragment adapter and from fragment adapter to your purpose fragment(s) and do filtering.
Good Luck my friend
If you do not want to implement using a system service for searching as described in docs, then one way of doing it would be as follows:
In the onCreate method get a handle to your fragment
// if you fragment is embedded with <fragment tag>
mMySearchFragment = (MySearchfragment)getFragmentManager()
.findFragmentById(R.id.your_fragment)
//in case your fragment is added programmatically then, of course,
getFragmentManager()
.beginTransaction()
.add(fragmentid, gragmentinstance, TAG)
.commit();
In the onCreateOptionsMenu do something like
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_searching, menu);
mSearchView = (SearchView) menu.findItem(R.id.menu_search_message).getActionView();
mSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener(){
#Override
public boolean onQueryTextSubmit(String query) {handleSearch(query)}
#Override
public boolean onQueryTextChange(String newText) { ... }
});
return true;
}
And finally
private void handleSearch(String query){
if(mMySearchFragment != null){
mMySearchFragment.updateSearchResults(query);
}
}
Your fragment would need to have a public method updateSearchResults(String query) where you would run the search and update your list view's adapter with the new collection
Also, you can import the BasicContactables sample in the Studio (File -> Import Sample) and investigate the code

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);
}

Categories

Resources