onBackPressed not called when search view is open - android

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~

Related

SearchView automatically closed

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.

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

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

Problems implemeting Options Menu on Action Bar

Good morning!
I'm having some troubles when I try to show correctly the option menu on the Action Bar.
This is what i have:
Main activity extends from ActionBarActivity. I've declared 3 fragments: A, B and C.
Inside B i've declared two fragments, B1 and B2. B2 extends from SUpporMapFragment. The B fragment navigation chosen is TAB_NAVIGATION. Both A and C have Standar navigation.
B2 fragment:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.menu, menu);
}
The problem is that the menu appears in A and C fragments when it should happen. On both fragments A and C i've the next:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(false);
}
The behaviour is the next. When i run my app, the A and C fragments has the option menu visible. When i go to B, if I'm in B1 and go back to A or C, the menu dissapears. If i go to B2 and go back to A or C, the menu is shown. I've debbuged it and when i click in the A or C fragment after being in B2 fragment, either fragments A or C calls the onCreateOptionsMenu from B2 fragment. Any help will be great!
Solved using control variables. I call onCreateOptionsMenu in my Main Activity:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
if (posicionFragmento==1 && Listado_tiendas_mapas_fragment.seleccionFragmentoMenu){
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
}
return true;
}

Categories

Resources