所以我实现了一个基本的嵌套片段-我将它们添加到堆栈中,以便从内部片段的onBackPressed引导我到达外部片段-但是,当发生这种情况时,外部片段没有响应。
相关代码:
TaskActivity-托管外部片段:
相关功能:
public void getTaskInfo(Task currentTask){
//Open the TaskInfoFragment for this task
TaskInfoFragment taskInfo = new TaskInfoFragment();
taskInfo.setCurrentTask(currentTask);
android.support.v4.app.FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.setCustomAnimations(R.anim.slide_in,0,0,R.anim.slide_out);
// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack
transaction.replace(R.id.frag_container, taskInfo);
transaction.addToBackStack(null);
FrameLayout frameLayout = findViewById(R.id.frag_container);
frameLayout.setClickable(true);
// Commit the transaction
transaction.commit();
}
从TaskInfoFragment开始的内部片段:
NotesFragment notesFragment = new NotesFragment();
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
FrameLayout frameLayout = rootView.findViewById(R.id.notes_fragment);
frameLayout.setClickable(true);
transaction.addToBackStack(null);
transaction.add(R.id.notes_fragment, notesFragment).commit();
TaskActivity中的OnBackPressed(托管外部片段):
public void onBackPressed() {
if(NotesFragment.isAttached()) {
FragmentManager fm = getSupportFragmentManager();
for (Fragment frag : fm.getFragments()) {
if (frag.isVisible()) {
FragmentManager childFm = frag.getChildFragmentManager();
if (childFm.getBackStackEntryCount() > 0) {
childFm.popBackStack();
return;
}
}
}
}
else {
this.setTitle(thisTaskListTitle); //Set the title to be the TaskList's title
super.onBackPressed();
}
我尝试在相关的frameLayout(属于外部片段的那一个)上设置clickable(true),但这无济于事。
有什么想法吗?
谢谢!
答案 0 :(得分:0)
显然,发生的事情是我的内部片段的框架布局仍然可点击。因此,尽管内部片段已分离,但它的框架布局仍然覆盖了外部片段并吸引了所有点击。
通过在childFm.popBackStack()之后将以下代码添加到onBackPressed来解决此问题:
FrameLayout frameLayout = findViewById(R.id.notes_fragment);
frameLayout.setClickable(false);