I want to change the visibility of menu items of a fragment activity (abs) when ever I change the fragment in the activity. The fragments are SherlockListFragments.
The menu items I want to show/hide are spinners I create on menu creation:
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
MenuInflater inflater = getSupportMenuInflater();
IcsSpinner herbSortSpinner = new IcsSpinner(this, null, R.attr.actionDropDownStyle);
SpinnerAdapter herbSortSpinnerAdapter = ArrayAdapter.createFromResource(this, R.array.herb_sort_elements, android.R.layout.simple_spinner_dropdown_item);
herbSortSpinner.setAdapter(herbSortSpinnerAdapter);
herbSortSpinner.setOnItemSelectedListener(this);
herbSortSpinner.setId(HERB_SPINNER_ID);
menu.add(R.string.menu_sort).setActionView(herbSortSpinner).setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
IcsSpinner noteSortSpinner = new IcsSpinner(this, null, R.attr.actionDropDownStyle);
SpinnerAdapter noteSortSpinnerAdapter = ArrayAdapter.createFromResource(this, R.array.note_sort_elements, android.R.layout.simple_spinner_dropdown_item);
noteSortSpinner.setAdapter(noteSortSpinnerAdapter);
noteSortSpinner.setOnItemSelectedListener(this);
noteSortSpinner.setId(NOTE_SPINNER_ID);
menu.add(R.string.menu_sort).setActionView(noteSortSpinner).setVisible(false).setEnabled(false).setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
inflater.inflate(R.menu.activity_lexicon, menu);
menuHolder.setMenu(menu);
return true;
}
My logic for switching the fragments is:
public boolean onNavigationItemSelected(int position, long itemId) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
switch(position) {
case 0:
List<Herb> herbList = findHerbsByIntent(getHerbLocale());
HerbListFragment herbListFragment = new HerbListFragment();
herbListFragment.setListAdapter(new HerbListAdapter(this, getHerbLocale(), herbList));
fragmentTransaction.replace(R.id.fragment_container, herbListFragment, HERB_LIST_FRAGMENT_TAG);
//menuHolder.showHerbsSpinner();
break;
case 1:
SymptomListFragment symptomListFragment = new SymptomListFragment();
symptomListFragment.setListAdapter(new SymptomListAdapter(this, getDefaultSymptomLocale()));
fragmentTransaction.replace(R.id.fragment_container, symptomListFragment, SYMPTOM_LIST_FRAGMENT_TAG);
//menuHolder.showHerbsSpinner();
break;
case 2:
NoteRepository noteRepository = new NoteRepository(this);
List<Note> notes = noteRepository.getNoticables(ReferedType.HERB);
NoteListFragment noteListFragment = new NoteListFragment();
noteListFragment.setListAdapter(new NoteListAdapter(this, getHerbLocale(), notes));
fragmentTransaction.replace(R.id.fragment_container, noteListFragment, NOTE_LIST_FRAGMENT_TAG);
//menuHolder.showNotesSpinner();
break;
case 3:
FavoriteRepository favoriteRepository = new FavoriteRepository(this);
Set<Integer> favoriteHerbs = favoriteRepository.getFavorables(ReferedType.HERB);
List<Herb> favoriteHerbList = herbRepository.getHerbsByIds(favoriteHerbs, getHerbLocale());
FavoriteHerbListFragment favoriteHerbListFragment = new FavoriteHerbListFragment();
favoriteHerbListFragment.setListAdapter(new HerbListAdapter(this, getHerbLocale(), favoriteHerbList));
fragmentTransaction.replace(R.id.fragment_container, favoriteHerbListFragment, HERB_LIST_FRAGMENT_TAG);
//menuHolder.showHerbsSpinner();
break;
}
fragmentTransaction.commit();
return true;
}
My first idea was to hold the menu object in a holder class and manipulate it there whenever I switch the fragment (in every case statement above).
static class MenuHolder {
private Menu mMenu;
void setMenu(Menu menu) {
mMenu = menu;
}
void showNotesSpinner() {
if (mMenu != null) {
mMenu.findItem(HERB_SPINNER_ID).setVisible(false).setEnabled(false);
mMenu.findItem(NOTE_SPINNER_ID).setVisible(true).setEnabled(true);
}
}
void showHerbsSpinner() {
if (mMenu != null) {
mMenu.findItem(NOTE_SPINNER_ID).setVisible(false).setEnabled(false);
mMenu.findItem(HERB_SPINNER_ID).setVisible(true).setEnabled(true);
}
}
}
My problem is that there is no menu item with the given ID which are activity local constants. I get an NPE here. Does anybody have an idea how I can fix that? Is there a better way to change the menu on switching fragments?
Best regards
Carsten
Is there a better way to change the menu on switching fragments?
May be yes :).
In your fragment onCreate, add this :
setHasOptionsMenu (true);
The doc :
Report that this fragment would like to participate in populating the options menu by receiving a call to onCreateOptionsMenu(Menu, MenuInflater) and related methods.
Override onPrepareOptionMenu method in your fragment class.
Prepare the Screen's standard options menu to be displayed. This is called right before the menu is shown, every time it is shown. You can use this method to efficiently enable/disable items or otherwise dynamically modify the contents.
Then, in this method, try to find your menu items by ID, and make them visible/unvisible, enabled/disabled, like this :
#Override
public void onPrepareOptionsMenu(Menu menu) {
menu.findItem(HERB_SPINNER_ID).setVisible(false).setEnabled(false);
}
Read More
In my case, I have 2 fragments that has a different menu item.
On my MainActivity:
FragmentA fragmentA = new FragmentA();
fragmentA.setTargetFragment(fragmentA, 0);
FragmentB fragmentB = new FragmentB();
fragmentB.setTargetFragment(fragmentB, 1);
and FragmentA and FragmentB has:
#Override
public void onHiddenChanged(boolean hidden) {
super.onHiddenChanged(hidden);
if(hidden) {
getTargetFragment().setMenuVisibility(false);
}else{
getTargetFragment().setMenuVisibility(true);
}
}
this will let menu from fragment A be visible when B is leaving. If it’s going back from B to A old menus from A can be visible.
Reference: here
Related
I'm currently creating functionality to change Fragments using bottom navigation. But instead of destroying them and recreating them, I want to simply hide and show the fragments to preserve the member variables.
I've tried replace(), hide() and show() but haven't managed the get it right, I'm getting animation errors which I'm not able to track down.
I also can't find an example of switching fragments within an AppCompatActivity.
public class MainActivity extends AppCompatActivity {
PassengerFragment passengerFragment;
DriverFragment driverFragment;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BottomNavigationView bottomNav = findViewById(R.id.bottom_navigation);
bottomNav.setOnNavigationItemSelectedListener(navListener);
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new PassengerFragment()).commit();
}
// this handles the bottom navigation so when you click an item it changes fragment
private BottomNavigationView.OnNavigationItemSelectedListener navListener =
new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem menuItem) {
Fragment selectedFragment = null;
switch (menuItem.getItemId()) {
case R.id.nav_passenger:
selectedFragment = passengerFragment;
break;
case R.id.nav_driver:
if (driverFragment==null) {
selectedFragment = new DriverFragment();
}
else {
selectedFragment = driverFragment;
}
break;
}
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
selectedFragment).commit(); <---- line 54
return true;
}
};
}
ERROR
Java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Class java.lang.Object.getClass()' on a null object reference
at android.support.v4.app.BackStackRecord.doAddOp(BackStackRecord.java:396)
at android.support.v4.app.BackStackRecord.replace(BackStackRecord.java:444)
at android.support.v4.app.BackStackRecord.replace(BackStackRecord.java:434)
at je.digital.kevin_pickmeup.MainActivity$1.onNavigationItemSelected(MainActivity.java:54)
That is because you are trying do replace with Fragment which are null. You have created passengerFragment and driverFragment as fields or global variables and than you are trying to use them but in some cases especially passengerFragment is null. If you are trying to keep the data in Fragments even they are replaced you can use addToBackStack but you will need to manage not to putting too much to back stack probably by checking getSupportFragmentManager().getBackStackEntryCount() and removing if there are more than 1 using popBackStack() or save the data in Bundle on onDestroyView and restore that data in onCreateView:
just needed better if statements to handle all the cases.
private BottomNavigationView.OnNavigationItemSelectedListener navListener =
new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem menuItem) {
Fragment passengerFragment = getSupportFragmentManager().findFragmentByTag("A");
Fragment driverFragment = getSupportFragmentManager().findFragmentByTag("B");
switch (menuItem.getItemId()) {
case R.id.nav_passenger:
if(passengerFragment.isHidden()) {
getSupportFragmentManager().beginTransaction().show(passengerFragment).commit();
getSupportFragmentManager().beginTransaction().hide(driverFragment).commit();
}
break;
case R.id.nav_driver:
if(driverFragment==null) {
getSupportFragmentManager().beginTransaction().add(R.id.fragment_container, new DriverFragment(),"B").commit();
getSupportFragmentManager().beginTransaction().hide(passengerFragment).commit();
}
else {
if(driverFragment.isHidden()) {
getSupportFragmentManager().beginTransaction().show(driverFragment).commit();
getSupportFragmentManager().beginTransaction().hide(passengerFragment).commit();
}
}
break;
}
return true;
}
};
I have list items onlcick of list item I take to DetailActivity.
DetailActivity is swipable using viewpager and Fragments. The issue is when DetailActivity is opened and Fragment is loaded no menu options are displayed however if i swipe left or right i get the menu options
then when i return to the initial fragment the menu items are visible.
I am inflating the menu options in fragment.
DetailActivity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_article_new);
mViewPager = (CustomViewPager) findViewById(R.id.viewPager);
int currentItemIndex = mNewsItemList.indexOf(newsItem);
mArticleSwipeMap = new HashMap<>();
mPagerAdapter = new ArticlePagerAdapter(getSupportFragmentManager());
mPagerAdapter.swapList(mNewsItemList);
mViewPager.setOnSwipeOutListener(this);
mViewPager.setAdapter(mPagerAdapter);
mViewPager.setCurrentItem(currentItemIndex);
mViewPager.addOnPageChangeListener(this);
}
Adapter class
private class ArticlePagerAdapter extends FragmentStatePagerAdapter{
private Map<Integer, ArticleFragment> mPageReferenceMap = new HashMap<>();
private List<NewsItem> newsItemList;
public ArticlePagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
ArticleFragment articleFragment = ArticleFragment.newInstance(newsItemList.get(position));
mPageReferenceMap.put(position, articleFragment);
return articleFragment;
}
#Override
public int getCount() {
return newsItemList.size();
}
public void swapList(List<NewsItem> newsItemList){
this.newsItemList = newsItemList;
notifyDataSetChanged();
}
}
DetailFragment
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.toolbar_menu, menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int itemId = item.getItemId();
switch(itemId) {
case R.id.action_more:
View menuItemView = mRootView.findViewById(R.id.action_more);
onMenuClick(menuItemView);
break;
case R.id.action_share:
shareTextUrl();
break;
}
return true;
}
Update 1 : When I swipe left, menu item are not shown when i swipe right menu items are shown, now if i swipe left menu item are shown.
Has it got to do anything with setting up viewpager
This is how I am setting up viewpager in onCrete of Activity
mPagerAdapter = new ArticlePagerAdapter(getSupportFragmentManager());
mPagerAdapter.swapList(mNewsItemList);
mViewPager.setOnSwipeOutListener(this);
mViewPager.setAdapter(mPagerAdapter);
mViewPager.setCurrentItem(currentItemIndex);
mViewPager.addOnPageChangeListener(this);
Update 2 :
The issue is when I use custom toolbar it is not working. When i use with the DarkActionBar it works fine but when I use with NoActionBar with my custom toolbar it creates prob.
Here is the sample proj I created which has same issue
link to github https://github.com/KumarVelu/viewPagerDemo
The problem is that you are trying to set more than one main ActionBar because each fragment has its own Toolbar. So, when ViewPager loads the current page and preloads the next page, you have 2 fragments trying to set their own Toolbar as the main ActionBar and they are conflicting.
If you want to use a Toolbar as main Action Bar, move the Toolbar in the Activity layout instead of the fragments layout and initialize it once in the Activity.
If you want a separate Toolbar in each fragment, don't set any Toolbar as main action bar and use Toolbar.inflateMenu() and Toolbar.setOnMenuItemClickListener() instead. Each fragment will then inflate its own menu items directly in its own Toolbar, without involving the Activity.
void method(){
int currentItemIndex = mNewsItemList.indexOf(newsItem);
mArticleSwipeMap = new HashMap<>();
mPagerAdapter = new ArticlePagerAdapter(getSupportFragmentManager());
mPagerAdapter.swapList(mNewsItemList);
mViewPager.setOnSwipeOutListener(this);
mViewPager.setAdapter(mPagerAdapter);
mViewPager.setCurrentItem(currentItemIndex);
mViewPager.addOnPageChangeListener(this);
}
Before
Call in onresume your fragment.
See you
I am an Android programming newbie trying to move from Activities to Fragments.
I have a simple Bluetooth app with 3 Activities:
MainActivity (with "Settings" in menu)
SettingsActivity with few checkboxes and seekbars
ScanningActivity showing Bluetooth devices in a list
This is a screenshot of the MainActivity, with an ellipsis menu in top-right corner:
I am trying to change my app to have a single MainActivity, which displays one of 3 Fragments (MainFragment, SettingsFragment or ScanningFragment):
public class MainActivity extends Activity implements
MainListener, /* my 3 custom interfaces */
SettingsListener,
ScanningListener,
BleWrapperUiCallbacks {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); // empty FrameLayout
Fragment fragment = new MainFragment();
getFragmentManager().beginTransaction()
.replace(R.id.root, fragment, "main")
.commit();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_settings:
Fragment fragment = new SettingsFragment();
getFragmentManager().beginTransaction()
.addToBackStack(null)
.replace(R.id.root, fragment, "settings")
.commit();
break;
}
return super.onOptionsItemSelected(item);
}
My problem is that currently the "Settings" menu is always displayed - while I only need to display it when the MainFragment is shown (but not when SettingsFragment or ScanningFragment are shown).
How to solve this best?
And do I have to introduce an enum variable, which I can query to find which of the 3 Fragments is currently displayed or is there a nicer way?
Use the onCreateOptionsMenu() in the fragment you wish to show the menu, not in your activity. In the others fragments you don't want to display the menu, use setHasOptionMenu(false).
To get your current fragment, use the findFragmentByTag() from your FragmentManager
You can Override onCreateOptions menu in the fragment that should have the menu and only inflate it there.
In the fragment:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater){
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.your_fragment_menu);
}
I am using fixed tabs + swipe inside a FragmentActivity. I have added a menu in action bar. I want that when the menu is clicked it should open class based on the current tab that is selected.
static TabHost myTabs;
-
-
-
myTabs = getTabHost();
This shows an error asking me if it shouls create a method called getTabHost(). I am assuming its because I am using FragmentActivity instead of Activity. So how do I know which tab is selected?
EDIT
As told below I have done this
#Override
public void onAttachFragment(Fragment fragment){
super.onAttachFragment(fragment);
tabValue=fragment.getId();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.one, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (tabValue) {
case 0:
Toast.makeText(BarcodeActivity.this, "HELLO", Toast.LENGTH_LONG).show();
case 1:
Intent i = new Intent(BarcodeActivity.this, NewCodeAdder.class);
startActivity(i);
break;
}
return true;
}
But still nothing happens when I click on the menu add option.
Check this out, it has everything from id to finding names of tabs:
Get the current fragment object
In my application the main activity hosts two fragments and attached to acticity as ActionBarTabs. using the following code. NOTE: activity and the 2 fragments are defined in seperate xml layout files (See the picture at the bottom)
private void createActionTabs() {
ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
//Status Tab
String strStatus = getResources().getString(R.string.lblStatus);
ActionBar.Tab statusTab = actionBar.newTab();
statusTab.setText(strStatus);
TabListener<SFrag> sFTabListener = new TabListener<SFrag>(this, strStatus, SFrag.class);
statusTab.setTabListener(sFTabListener);
actionBar.addTab(statusTab);
//Controller Tab attached the same way
.....
}
The ActionBar Items (start and refresh) are added using
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_menu, menu);
return true;
}
Now coming to my problem:
I want to update the data shown on the Status Fragment at application load, fragment resume and click of refresh menu item from the action bar. Now when I try to access the Status fragment from Main Activity using the following code
SFrag frag = (SFrag) getFragmentManager().findFragmentById(R.id.st_frag);
Log.d(TAG, "In Activity SFrag is " + (frag == null ? "null" : "not null"));
if (frag != null) {
//calls the method to update data
fragment.updateStatusData(statusInformation);
}
The getFragmentManager().findFragmentById methods always returns null. I even tried the onResume method of the activity, the fragment objects is still returned as null. So how do I access the fragment and thus accessa method of that fragment from host acticty.
Secondly, I am trying to use the action_service (its shown as Start button just for this mockup) in action bar as a toggle for satrting or stopping a background service. I can easily update the title/icon for start menu item from onOptionsItemSelected method (I save the current status running/stoppede in shared-preferences). but when I try to accees the menuItem at onStart/onResume of the activity by using
MenuItem mi = (MenuItem) findViewById (R.id.action_service);
it always returns null. So How Can I access action_service menu Item in onResume/onStart to update it.
My Application looks like this
First of all if you want to declare MenuItem, you should do it in this way :
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getSupportMenuInflater().inflate(R.menu.main_activity, menu);
MenuItem mRefresh = menu.findItem(R.id.refresh_menu_item);
return true;
}
About updating your Fragment on applications load or Activity's start, just set selected the tab which holds your Fragment and put the code which will load the data in your Fragment on it's onStart() or onActivityCreated() method. You can override onOptionsItemSelected() in your Fragment, so you can update your views not from your Activity which holds your Fragment, but from it's own class.
Edit: Here is an example how you should handle menu from your Fragment :
public class MyFragment extends Fragment {
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setHasOptionsMenu(true); // Do not forget this!!!
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.first_fragment, menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
switch (item.getItemId()) {
case R.id.action_refresh:
// Do You stuff here
break;
}
return super.onOptionsItemSelected(item);
}
}
Using this you can add and use MenuItem's in your Fragment.
Hope this helps!
I can answer the MenuItem part just not the other part.
for the menu item as per the docs You can safely hold on to menu (and any items created from it), making modifications to it as desired, until the next time onCreateOptionsMenu() is called.
for example
#Override
public boolean onCreateOptionsMenu(Menu menu) {
actionMenu = menu;
return true;
}
so basically anytime you want to change one of the items you can do this
MenuItem item = actionMenu.getItem(0);