可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I am using the following method to switch between Fragments (in my NavigationDrawer) by showing / hiding them.
protected void showFragment(int container, Fragment fragment, String tag, String lastTag, boolean addToBackStack ) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
if ( lastTag != null && !lastTag.equals("")) {
Fragment lastFragment = fragmentManager.findFragmentByTag( lastTag );
if ( lastFragment != null ) {
transaction.hide( lastFragment );
}
}
if ( fragment.isAdded() ) {
transaction.show( fragment );
}
else {
transaction.add( container, fragment, tag );
}
if ( addToBackStack ) {
transaction.addToBackStack( tag );
}
transaction.commit();
// set the active tag
activeFragTag = tag;
}
What I am unclear about is which method of the Fragments lifecycle is called when I show or hide it? (since there is no method such as onShow() or onHide() im not quite sure what to use). I want to perform specific actions upon showing and hiding a certain Fragment.
回答1:
Similar to activity lifecycle, Android calls onStart() when fragment becomes visible. onStop()
is normally called when fragment becomes invisible, but it can also be called later in time.
Depending on your layout Android can call onStart()
even, when your Fragment is not yet visible, but it belongs to a visible parent container. For instance, this is valid for android.support.v4.view.ViewPager
which requires you to override Fragment.setUserVisibleHint()
method. In any case, if you need to register/unregister BroadcastReceivers or other listeners, you can safely use onStart()
and onStop()
methods because those will be called always.
Note: Some fragment containers can keep invisible fragments started. To handle this situation you can override Fragment.onHiddenChanged(boolean hidden)
. According to the documentation, a fragment must be both started and visible (not hidden), to be visible to the user.
Update: If you use android.support.v4.widget.DrawerLayout
then a fragment below the drawer stays started and visible even when drawer is open. In this case you need to use DrawerLayout.setDrawerListener()
and listen for onDrawerClosed()
and onDrawerOpened()
callbacks.
回答2:
I @Override this method and resolve my problem:
@Override
public void onHiddenChanged(boolean hidden) {
super.onHiddenChanged(hidden);
if (hidden) {
//do when hidden
} else {
//do when show
}
}
回答3:
off-course you can @Overriede following method to do so:
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if (isVisibleToUser) {
// Do your Work
} else {
// Do your Work
}
}
回答4:
Try this code:
@Override
public void setUserVisibleHint(boolean visible)
{
super.setUserVisibleHint(visible);
if (visible && isResumed())
{
onResume();
}
}
@Override
public void onResume()
{
super.onResume();
if (!getUserVisibleHint())
{
return;
}
//Add your code this section
}
回答5:
Just try this in your setUserVisibleHint()
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if(isVisibleToUser && getView() != null){
isActive = true;
init();
}else if(isVisibleToUser && getView() == null){
isActive = false;
}else{
isActive = true;
}
}
And create this code in onCreateView() :
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
if(!isActive){
init();
}
}
回答6:
setUserVisibleHint
call before onCreateView
. and you can't update any View inside setUserVisibleHint I use
public void setMenuVisibility(final boolean visible)
for visibility and onHiddenChanged() didn't call for the first time. it calls when the hidden state changes. because a fragment is visible by default
. In order to achieve this method for the first time you have to call mFragmentTransaction.hide(oldFragment)
then it will work
Note
if you want to use setUserVisible hint and update View Use this method
回答7:
Another way to calling fragment method when fragment is visible and you use viewpager in activity.
//first of all you create a interface
public interface ShowFragmentVisible{
public void showFragment();}
//After that this interface implement inside Fragment like that
public class MyFragment extends Fragment implements
ShowFragmentVisible {
@Override
public void showFragment() {
}
// Now goes your Activity then create object of interface and call inside when addOnViewpagerListener
ShowFragmentVisible showFragmentVisible;
@Override
public void onAttachFragment(Fragment fragment) {
super.onAttachFragment(fragment);
if (fragment instanceof ShowFragmentVisible) {
showFragmentVisible = (ShowFragmentVisible) fragment;
}
}
//your viewpager method
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
if (position==0){
showFragmentVisible.showFragment();
}
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
this is another alternative,but its work for me successfully
回答8:
Of course you could override setUserVisibleHint
or setMenuVisibility
but if you need to access Context
or Activity
, they will be null in there!
There is another method onStart
which always has the context available at hand, but it will only get called once upon creation of fragment and if you start moving between your fragments in a pager you will see that it won't get called in second view and afterwards.
So... what to do now?
The workaround is quite easy, use onStart
for the first visit and setMenuVisibility
for later ones.
Your code will probably look like below :
Fragment class:
public class MyFragmentClass{
private boolean isCurrentVisible = false;
...
@Override
public void onStart() {
super.onStart();
if (isCurrentVisible)
doSth();
}
@Override
public void setMenuVisibility(boolean menuVisible){
super.setMenuVisibility(menuVisible);
this.isCurrentVisible = menuVisible;
if(menuVisible && getContext() != null)
doSth();
}
This way Context
will always be available to doSth()
method.