我需要知道我的片段何时可见,我使用的是setMenuVisibility,但我现在知道它不是一个好选择。我试图在FragmentStatePagerAdapter片段上实现setUserVisibleHint,但它永远不会被调用。
import android.app.Fragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
public class Contacts extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View view = inflater.inflate(R.layout.fragment_screen_contacts, container, false);
return view;
}
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
Log.d("MyFragment", "This never shows up.");
Toast.makeText(getActivity(), "Neither does this", Toast.LENGTH_LONG).show();
}
}
我正在运行API级别19,并在我的AndroidManifest上设置了最低API级别15。还有什么可以做setUserVisibleHint,我做错了什么?
答案 0 :(得分:19)
setUserVisibleHint是可用的,这样你就可以告诉系统片段实际上是不可见的,而不是相反,当你做一些专门隐藏它的奇特片段事务时。您不能使用它来确定可见性,它默认为true。
您应该使用isVisible调用来了解片段是否可见以及片段的onAttach或根视图类,以便在将其附加到活动或相应的根视图时获得回调。
答案 1 :(得分:15)
// There is a bug in older iOS debugservers where they don't shut down the process
// they are debugging properly. If the process is sitting at a breakpoint or an exception,
// this can cause problems with restarting. So we check to see if any of our threads are stopped
// at a breakpoint, and if so we remove all the breakpoints, resume the process, and THEN
// destroy it again.
//
// Note, we don't have a good way to test the version of debugserver, but I happen to know that
// the set of all the iOS debugservers which don't support GetThreadSuffixSupported() and that of
// the debugservers with this bug are equal. There really should be a better way to test this!
//
// We also use m_destroy_tried_resuming to make sure we only do this once, if we resume and then halt and
// get called here to destroy again and we're still at a breakpoint or exception, then we should
// just do the straight-forward kill.
//
// And of course, if we weren't able to stop the process by the time we get here, it isn't
// necessary (or helpful) to do any of this.
仅适用于 FragmentPagerAdapter 。 请参阅Is Fragment.setUserVisibleHint() called by the android System?