Skip to content

Commit

Permalink
解决打开 MainTabActivity 崩溃;解决 Activity 与 Fragment 复杂嵌套下可能 tag 冲突导致 Fragm…
Browse files Browse the repository at this point in the history
…ent 显示异常
  • Loading branch information
TommyLemon committed May 5, 2023
1 parent d9240ef commit cf1e0c9
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public void selectFragment(int position) {
}

Fragment fragment = fragments[position];
String tag = TAG + "-fragment-" + position;
String tag = getClass().getName() + "-fragment-" + position;
if (fragment == null) {
fragment = fragmentManager.findFragmentByTag(tag);
}
Expand Down Expand Up @@ -145,7 +145,7 @@ public void selectFragment(int position) {
}

if (fragment.isAdded() == false) {
fragmentTransaction.add(R.id.flBaseTabFragmentContainer, fragment, tag);
fragmentTransaction.add(getFragmentContainerResId(), fragment, tag);
}
FragmentTransaction ft = fragmentTransaction.show(fragment);
try { // cannot perform this action after savedInstance
Expand Down Expand Up @@ -244,7 +244,9 @@ public void initData() {// 必须调用
/**获取Fragment容器的id
* @return
*/
public abstract int getFragmentContainerResId();
public int getFragmentContainerResId() {
return R.id.flBaseTabFragmentContainer;
}

/**获取新的Fragment
* @param position
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ public void selectFragment(int position) {
}

Fragment fragment = fragments[position];
String tag = TAG + "-fragment-" + position;
String tag = getClass().getName() + "-fragment-" + position;
if (fragment == null) {
fragment = fragmentManager.findFragmentByTag(tag);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ public void selectFragment(int position) {
}

Fragment fragment = fragments[position];
String tag = TAG + "-fragment-" + position;
String tag = getActivity().getClass().getName() + "-" + getClass().getName() + "-fragment-" + position;
if (fragment == null) {
fragment = fragmentManager.findFragmentByTag(tag);
}
Expand Down

0 comments on commit cf1e0c9

Please sign in to comment.