我疯狂地尝试让 optionsMenu 为不同的视图提供不同的选项。如果在我的片段上调用 onResume() 我可以让它工作,但事实并非如此。
我有一个 SherlockFragmentActivity,它在 onCreate 期间添加一个 SherlockFragment,如下所示:
if (getSupportFragmentManager().findFragmentById(android.R.id.content) == null) {
Fragment f = LoginFragment.newInstance();
getSupportFragmentManager()
.beginTransaction()
.add(android.R.id.content, f, "loginfragment")
.attach(f)
.commit();
}
最终使用相同的方法在其之上添加另一个 SherlockFragment:
Fragment f = OfferListFragment.newInstance();
getActivity().getSupportFragmentManager()
.beginTransaction()
.add(android.R.id.content, f, "offerList")
.addToBackStack(f.getClass().getSimpleName())
.commit();
最后,使用以下方法在顶部启动另一个 SherlockFragment:
AddOfferFragment newFragment = AddOfferFragment.newInstance(offer);
getActivity()
.getSupportFragmentManager()
.beginTransaction()
.add(android.R.id.content, newFragment, "addofferdialog")
.addToBackStack(newFragment.getClass().getSimpleName())
.commit();
我添加了 onResume() 事件,如下所示:
@Override
public void onResume() {
Log.e(TAG, "onResume");
super.onResume();
}
现在我明白了onResume logs when they are created, but when I press
Backfrom any of them, I don't get the
onResume` 日志根本没有。我的选择之一是注销,这会消除所有视图,如下所示:
FragmentManager fm = getSupportFragmentManager();
if (fm.getBackStackEntryCount() > 0) {
fm.popBackStack(fm.getBackStackEntryAt(0).getName(), FragmentManager.POP_BACK_STACK_INCLUSIVE);
}
但我什至没有在第一个片段上得到 onResume 。
由于片段透明的另一个问题here https://stackoverflow.com/questions/16575177/fragment-is-transparent-and-shows-activity-below(这可能是正常行为,我不确定)。
我想知道我添加片段的方式是否错误?
如果不是,还有什么问题?