我无法通过framgent
检索findFragmentByTag
。请帮忙!
请参阅下面的代码。我在检索片段时为NetEaseContent定义了一个标记,它丢失并返回null
:
public class MainActivity extends SlidingFragmentActivity {
private static final String MENU_TAG = "menuTag";
private Screen screen;
private SlidingMenu mSlideMenu;
private final static String CONTENTTAG = "contentTag";
private static final String TAG = "MainActivity";
private NetEaseContent mContent;
private Fragment mMenu;
private Bundle mBundle;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.content_frame);
setBehindContentView(R.layout.menu_frame);
Log.e(TAG, "onCreate");
screen = new Screen(this);
Log.e(TAG, "savedInstanceState null");
mSlideMenu = getSlidingMenu();
mSlideMenu.setEnabled(true);
mSlideMenu.setShadowWidthRes(R.dimen.shadow_width);
mSlideMenu.setShadowDrawable(R.drawable.shadow);
mSlideMenu.setFadeDegree(0.35f);
mSlideMenu
.setBehindOffset((int) ((float) screen.getWidth() * 2.5 / (float) 4));
mSlideMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
if (savedInstanceState == null) {
mContent = new NetEaseContent(mSlideMenu);
getSupportFragmentManager().beginTransaction()
.add(R.id.content_frame, mContent, CONTENTTAG).commit();
mMenu = new NetEaseMenuFragment();
getSupportFragmentManager().beginTransaction()
.add(R.id.menu_frame, mMenu, MENU_TAG).commit();
} else {
Log.e(TAG, "savedInstanceState not null!!!");
mContent = (NetEaseContent)getSupportFragmentManager()
.findFragmentByTag(CONTENTTAG);
if (mContent != null) {
mContent.setSlidingMenu(mSlideMenu);
} else {
Log.e(TAG, "mContent null!!!"); //here the mContent filed is null
}
mMenu = getSupportFragmentManager().findFragmentByTag(MENU_TAG);
}
}
}
答案 0 :(得分:0)
你是否在片段onCreate方法中使用了setRetainInstance(true)?如果没有,您的片段将在活动生命周期中被销毁