I am trying to implement an action bar within an activity.
Is it possible to "refresh" or re-inflate the action bar with a new menu(without restarting the activity)
The only way I have found so far is to leave the action bar empty and assign it a new customView every time I want it changed.
Try calling invalidateOptionsMenu() on your activity. Quoting the documentation:
Declare that the options menu has changed, so should be recreated. The onCreateOptionsMenu(Menu) method will be called the next time it needs to be displayed.
Related
I have two activtiy. When a boolean variable in activity 1 is true then the UI in activity 2 should be update (activity 2 is with fragment). When I back to activity 2 with (setDisplayHomeAsUpEnabled) it works correctly but with back button it does not work. What is the difference between this ways and how I can solve this. I try update the activity 2 with this code but it does not work:
#Override
public void onBackPressed() {
super.onBackPressed();
//update();
}
The Home Button is not meant to behave like the Back Button. See this reference. The Home or "Up" Button should take the user up within the view hierarchy (i.e. if you have a number of multiple choice fragments, don't go back through each of them, but back to the activity that started the first fragment)
Each activity should specify what it's parent activity is in order to properly use this functionality. You can do this through the manifest, or by overriding onOptionsItemSelected() as outlined here
In terms of why your activity 2 may be updating when using the "Up" button but not the back button, this could be because both handle the back stack in a different way. Back will take you to the last thing on the stack and resume it, assuming you don't haven't tagged the activity with a launchmode that alters this behavior. Here's more info on the back stack. If you haven't designated your Activity 2 as "singletop" then when you use the "Up" Button, a new instance of that parent activity is created. See here. This may means that the information is updating after using the home button because it's creating a new instance of the activity, while for the back button - you are not creating a new instance but resuming a previous instance... make sure you implement an onResume() function to properly handle this and update the information.
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();
}
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
I have added one star icon on Action Bar. Now I want to add click event on that star. If the star is clicked then it should be bright or Turned ON which is OFF by default. And if that star is ON and clicked, then it should go to OFF. I am not getting any idea how to perform onClick event on action bar.
You have a good example here.
Action Bar Example
To add onClickListener to a view in your action bar you have to create a reference to that view first and then in the onCreateOptionsMenu() method you can register the onClickListener.
See the answer to this question here. Its the same principle, you're simply using a 'star icon' instead of a switch.
I'm relatively new with Android development, and I received a layout for an app where I have to put some kind of bar with images above the action bar.
I searched a lot in Google but I couldn't find anything about change the action bar position. I also found some info saying:
Google highly recommend to NOT change the action bar position(...)
Well, how I got some custom views for the action bar icons was to put the view below the action bar. However, what I am supposed to do is put the view above the action bar.
Is there a way to put a view above the action bar or change the action bar position?
Is there a way to put a view above the action bar or change the action bar position?
Not that I am aware of.