In a fragment I see in the onCreate setHasOptionsMenu(true);
I see the menu.
But I can't understand, if I need to change something in the menu where should I do that programmatically?
Now I see a menu which I am not sure which resource file is loaded but how can I change/update the menu before it is actually rendered e.g. to remove an item?
Update: I don't need todo this from the fragment. The hosting activity is fine but I don't know where should I do this and how
You can override onPrepareOptionsMenu(), which is called before the menu is displayed and everytime you call FragmentActivity.supportInvalidateOptionsMenu(). There you can call setVisible() to hide or show menu items as needed.
I need to change something in the menu where should I do that programmatically?
While menu is not managed by your fragment, I'd suggest to make fragment calling host Activity's method of your choice to request the change, but the change itself should be done by Activity
Related
I have been trying to determine why the onPause() and the onResume() methods were not fired when the user switches the the tab to be displayed when using a ViewPager and sectionsPageAdapter.
I assumed that when the user changes the tab being displayed, the onPause() or the onDestroy() methods would be called. Then, if the user returns to the tab, the onResume() method would be called, but it seems it is not the functionality...
is there any way to determine when the user switces a tab and to know to which tab it has been switched? Something like onFragmentBeingDisplayed() or similar.
Thanks!
EDIT: Answer from Suraj Vaishnav
By editing the constructor from the FragmentPageAdapter like this super(fm, FragmentPagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT); I could use the desired behaviour on the app
That's because ViewPager loads some fragments on the left side and on the right side of the current fragment. By default, ViewPager loads one on the left side and one on the right side of the fragment. You can change the number via: viewpager.setOfScreenPageLimit(int);
What you thinking is working exactly the same way in androidx's Fragment. Means onPause and onResume will call accordingly. So the first suggestion is to use androix. Check this answer: https://stackoverflow.com/a/57886441/7682257
For some reason, If you do not want to use androidx then in your Fragment, override setUserVisibleHint method, which gives you isVisibleToUser as a parameter.
If you don't want to use Androidx version of Viewpager to get the use of the BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT behavior to fire onResume only on the displayed Fragment then you can instead add an addOnPageChangeListener
You can use the ViewPager.SimpleOnPageChangeListener or your own implementation of the ViewPager.OnPageChangeListener interface.
This PageChangeListener has a onPageSelected method with gets called with the position selected.
While I have given links to the Androidx Documentation this also works in earlier support libraries, just Google have removed the docs for it as they want you to use Androidx
Note this ViewPager.OnPageChangeListener onPageSelected fires at a slightly different time to onResume with BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT. It fires when about 50% of the new tab is swiped on to the screen when onResume fires when 100% of the new tab is on screen.
I am working with fragments and a navigation drawer. I have two fragments and a fragment manager to recycle them when I click on the menu items in the navigation drawer. I am also receiving data from a server in one of the fragments.
However, every time I reinitialize the fragment, the call to the server occurs again. I would like to know if there is any way that I can make sure that the server call only occurs in the beginning and when I click the refresh button.
I would like to know if there is any way that I can make sure that the
server call only occurs in the beginning and when I click the refresh
button.
Yes , when you click on the refresh button use an ClickListener to trigger the call.
If you want the call to occur only when fragment is created you can refer to the fragment life cycle. The better way is to put this call in the onCreate() method.
However if you don't want to make a call each time the fragment is created i recommend you tu use the SharedPreference to save the information that call have already been made.
exemple:
prefs = PreferenceManager.getDefaultSharedPreferences(this);
// The seconde value of method getBoolean incates the default value if constant not found
if (prefs.getBoolean(Constants.CALL_DONE, false)) {
// Make call
prefs.edit().putBoolean(Constants.CALL_DONE, true).apply();
}
I have a Option Menu that opens when I'm clicking the menu button on a android smartphone. And I wonder if there is some way to call the onCreateOptionsMenu and onOptionsItemSelected method from a onClick method for a button. So when I click on a button in my app instead of clicking the menu button on the smartphone.
You can invoke Activity.openOptionsMenu to show the options menu.
You should not invoke onCreateOptionsMenu(…) directly; this method is for the Android UI framework, not for you. If you need this method to be called again, for example, to refresh the menu items, you can call invalidateOptionsMenu() and Android will re-initialize the menu.
The same goes for onOptionsItemSelected(MenuItem). If you need to make the app do something that normally happens when a particular menu item is selected, simply put that something in a separate method that you invoke from onOptionsItemSelected as well as from anywhere else you need it.
I have a button which is basically used for start/stop. So initially the text of the button is set to start. I attached a OnClickListener to it. So whenever it gets clicked i change its text. So if it was start it become stop and vice-versa.
The problem comes when i change my phone view from portrait to landscape or vice-versa the button text gets reset.
So for example I clicked the start button---it changed to stop. Now if I tilt my phone to change the view the button text gets set to start again.
Am I using the button in a wrong way?
You should save your button state. When screen orientation changes, onCreate is called and all your app variables are re-intitialized. Read more here http://developer.android.com/reference/android/app/Activity.html
No, you are using the button in the right way.
The thing what you are seeing is "configuration change". When you are tilting your device, Android recreating your activity and recreating all it's views (so, they getting default captions as them described in XML).
You need to do the
disable configuration changes for your Activity. To do so, add the following to your manifest's activity tag: android:configChanges="orientation|keyboardHidden". It is not suitable, if you have different layouts for landscape and portraint orientations, when you need to...
handle the configuration changes by overriding onSaveInsatnceState method of your Activity, save a state there and then use it in onCreate method.
See this article for further explanation
In an Android application I have a ListActivity. After creating it I have a broadcast receiver which has to disable some items in the list. So I have a pice of code ike this one:
View child = getListView().getChildAt(i);
child.setEnabled(false);
It works in the way it changes the color of the disabled views to gray. But I need to avoid this items can be clicked. So I've tried to call
child.setFocusable(false);
or
child.setClickable(false);
but in both cases onListItemClick is called after pressing on a disabled option.
How can I avoid that onListItemClick method is called when these textviews are clicked?
Thanks.
Disabling the views does nothing with regard to the onListItemClick event.
The child must be disabled by the Adapter you are using to populate the list.
See http://developer.android.com/reference/android/widget/ListAdapter.html#isEnabled(int)
This method needs to be overriden to return false for each item you do not want to be clickable. You should also add a method to your adapter like setEnabled(int position, boolean enabled)