I have a SearchView in my onCreateOptionsMenu() that I hide using:
if(mState == 0){
msearchMenuItem.setVisible(false);
}
I want it to be true after I access a custom Fragment that is part of a ViewPager in:
setMenuVisibility(boolean menuVisible){
}
I understand that I cannot pass a reference of the MenuItem to my Fragment because the ActionBar is not a layout, and all of its process needs to be from the MainActivity.
My option was to change the value of mState from setMenuVisibility() in my Fragment, but It shows the SearchView after I interact with my DrawerLayout. If I don't touch the home button, it would never show the SearchView.
Now, what can I do to load it exactly when I am at the Fragment?
you create a callback to the activity that enables the menu option again when you are in the fragment.
See Communicating with an Activity
Now, what can I do to load it exactly when I am at the Fragment?
You can use the setMenuVisibility() callback from your Fragment's onCreateView() method.
The actual answer is overriding onCreateOptionsMenu() from my Fragment. But first, I had to call setHasOptionsMenu(true); in onCreate(), so the override could take effect.
Related
My only activity host onCreateOptionsMenu and onOptionsItemSelected but implementation of UI-Recyclerview is done in MainFragment.
From menu i would like to sort the list of Recyclerview. So i need to get the list then manipulate then to post back to MainFragment.
Please if someone know how to do this ?
You can receive menu callbacks directly in the fragment, so you don't have to involve the activity. In your fragment onCreate call:
setHasOptionsMenu(true);
Then implement the related menu handling callbacks in your fragment.
Check https://developer.android.com/guide/components/fragments#ActionBar
Can I use onClick Listener for a button located in one of tab fragment, in Main Activity?
I think you want to perform some action in your MAIN ACTIVITY when a button is clicked in your fragment. If I am right, then just create a interface callback from your fragment to the activity and use the over-ridden method to perform whatever action you require.
Have a look at this
http://developer.android.com/training/basics/fragments/communicating.html
or this
onAttach callback from fragment to activity
Hope this helps.
Yes, you can think of a Fragment as a stand alone user interface that contains it's own animations and logic. So an onClickListener for a Button in a Fragment is no different than in an Activity.
Though the method using Interface is sufficient, this is the simple way to do it.
You can pass context("getActivity()") of MainActivity while calling
the fragment.
Create a method in MainActivity to change the fragment.
call the MainActivity method to set the Fragment.
((MainActivity) context).setFragment(fragment);
you are done.
What I Have
I have a ViewPager with 5 fragments. I want to animate some TextViews inside the fragments whenever they become visible to the user.
I can't use onResume() as the fragments to the left and right are already created. I can't use setUserVisibilityHint() as it is called before onCreateView() so the views are not ready yet.
So what should be the way to animate the views whenever a particular fragment becomes visible to the user?
I'm not sure, but if you say that setUserVisibilityHint calls before onCreateView, than check view on null here (make reference on view - field), and if it not null - animate it. Also animate it always in onCreateView.
(1) I can't use onResume() as the fragments to the left and right are already created.
(2) I can't use setUserVisibilityHint() as it is called before onCreateView() so the views are not ready yet.
So what should be the way to animate the views whenever a particular fragment becomes visible to the user?
You're right on (1) and (2). However, setUserVisibilityHint() gets called Once Again with a True value after the Fragment comes to Front on Display. But on First Run the Fragment to be shown gets its setUserVisibilityHint() called before onCreateView().
SOL: You should use the above said behaviour of setUserVisibilityHint() along with onResume() to animate the views whenever a particular fragment becomes visible to the user.
Scenario 1: On First Run: Displayed Fragment's setUserVisibilityHint(boolean isVisibleToUser) gets called with
True param value. But as the Fragment's State is not Resumed we postpone and let the onResume() handle animation.
Scenario 2: For Other Fragments that are already in Resume State, setUserVisibilityHint(boolean isVisibleToUser) will get called with
True param it they come on to Display. Here you check for the
Fragment Animated or not and Do animation.
CODE
a) Declare two Global Boolean Fields: isAnimated and isOnDisplay
a.1) Set isAnimated boolean to True;
b) Override setUserVisibilityHint(boolean isVisibleToUser):
Here you set isOnDisplay boolean to isVisibleToUser and check is the Fragment Not Already Animated and is in Resumed State and is Visible to User.
{ if(!isAnimated && isResumed() && isVisibleToUser) // DO Animation }
c) Override onResume()
Check if the Fragment Not Already Animated and is Visible to User.
{ if(!isAnimated && isVisibleToUser) // DO Animation }
I know this answer might be a bit late, but I hope it can help others in a similar situation.
You could use FragmentViewPager library (I am the author), which deals with the issue you are facing for you. Its features are:
allows its Fragment pages to get notified when they are actually
visible/invisible to the user
supports multiple levels of FragmentViewPagers (nesting)
provides methods to control its paging
A basic usage would be:
Attach FragmentViewPager programmatically or via XML to an Activity
or Fragment, as you would with native ViewPager
Set FragmentViewPager's adapter. Your adapter should inherit
com.sbrukhanda.fragmentviewpager.adapters.FragmentPagerAdapter or
com.sbrukhanda.fragmentviewpager.adapters.FragmentStatePagerAdapter
Override onResumeFragments() of the hosting Activity and call
FragmentViewPager.notifyPagerVisible():
private FragmentViewPager mFragmentsPager;
#Override
public void onResumeFragments() {
super.onResumeFragments();
mFragmentsPager.notifyPagerVisible();
...
}
or onResume() of the hosting Fragment and call
FragmentViewPager.notifyPagerVisible():
private FragmentViewPager mFragmentsPager;
#Override
public void onResume() {
super.onResume();
mFragmentsPager.notifyPagerVisible();
...
}
Override onPause() of the hosting Activity or Fragment and call
FragmentViewPager.notifyPagerInvisible():
private FragmentViewPager mFragmentsPager;
#Override
public void onPause() {
super.onPause();
mFragmentsPager.notifyPagerInvisible();
...
}
Implement FragmentVisibilityListener on all Fragment pages that you
wish to receive callbacks for their visibility state
You are ready to go!
If you wish to see a more complete sample code, then check project's sample project.
If you want to do it in individual fragments, then you can use isVisible()
for each fragment in your fragment transition and create a listener. Whenever a fragment will become visible , listener will be invoked and each fragment will implement that listener and do your intended task in the overridden method.
So I have a fragment A which has a button to open another a fragment B. In fragment B I can pick some options, which is bundled into an Bundleobject. When I exit from fragment B, I want to refresh a TextView in fragment A.
Right now I'm using dismiss() method to remove the fragment, and then call back the fragment again so that onCreateView() is called. It works fine, but I don't want the animation where the fragment windows is run. So I like to not use dismiss() to remove the fragment instead I want to keep it on the Activity, but I need to know how I can refresh fragment A. I've tried overriding onActivityCreated() but it didn't result in the action I wanted.
So I wonder what's the approach if I want to refresh fragment A without having to dismiss it first so that onCreateView() can be called again.
I can attach code if needed. But maybe just an explanation is enough here?
you can use the life cycle function onResume() in fragment A to update the textview.
You can create your own Listener interface ( example how to do it or this) that listens when you remove your fragment, and you can get the event on Fragment A where you can setText to your TextView.
I Have a Fragment inside an Activity. This Activity contains OptionsMenu.
I use a FragmentTransaction to launch a new instance of the same Fragment class, when I selected the OptionsMenu, it appears in double because it load twice.
How can I do to manage OptionsMenu between the Fragments
Thanks.
PS : Sorry for the English
There is no way such way. You can handle on BackPressed and show your own custom menu as a new fragmnet. Or manage your options menu inside of FragmentTransaction inactivity in dependent of your fragment class and content
I find a solution,
I create the OptionsMenu only in the Activity.
I get the OptionsItemsSelected on the Activity too.
When onOptionsItemSelected() occur, I get the current Fragment like this :
FragmentManager fg = getSupportFragmentManager();
Fragment frag = fg.findFragmentById(R.id.fragmentMap);
All my Fragment have the same id but I always get the Fragment on the top.
Then, I can call :
frag.onOptionsItemSelected(item);
to the Fragment receive and my UI is correctly update.
I hope It helps.
Try playing with setHasOptionsMenu(boolean) in your Fragment extending classes,
if that is set to true the FragmentManager should automatically call your current fragment's onOptionsItemSelected,
it set to false you can manually call those callbacks yourself in your Activity's onOptionsItemSelected.
Worked for me... :)