在我的应用程序中有几个fragment
s ,activity
我正在backStack
为这些s 维护一个fragment
。一切正常,但其中有一个嵌套的片段。当我将其放入backStack
并通过按后退按钮再次恢复时,该片段看起来与先前的内容(子片段)重叠。这是正常的视图:
这是重叠视图的屏幕截图(当我恢复片段时):
您可以得到差异,因为第二个文本更深(这意味着子片段重叠)
我怎样才能避免这种情况?这是我的嵌套片段的代码:
public class CompetitiveProgramming extends SherlockProgressFragment implements
OnChapterSelectListener, OnSubChapterSelectListener {
View mContentView;
static public List<Chapter> chapterList = new ArrayList<Chapter>();
private ProcessTask processTask = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setRetainInstance(true);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mContentView = inflater.inflate(
R.layout.competitive_programming_exercise, container, false);
return super.onCreateView(inflater, container, savedInstanceState);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setContentShown(false);
setContentView(mContentView);
processTask = new ProcessTask();
processTask.execute();
}
protected class ProcessTask extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
// background task
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
FragmentTransaction transaction = getChildFragmentManager()
.beginTransaction();
if (mContentView.findViewById(R.id.fragment_container) != null) {
getChildFragmentManager().beginTransaction()
.add(R.id.fragment_container, new ChaptersListFragment()).commit();
} else {
transaction.add(R.id.category_fragment, new ChaptersListFragment());
transaction.add(R.id.sub_category_fragment, new SubChaptersListFragment());
transaction.add(R.id.sub_sub_category_fragment,
new SubSubChaptersListFragment());
}
transaction.commit();
setContentShown(true);
}
}
static protected class Chapter {
String chapterTitle;
List<SubChapter> subchapterList;
public Chapter(String chapterTitle, List<SubChapter> subchapterList) {
this.chapterTitle = chapterTitle;
this.subchapterList = subchapterList;
}
}
@Override
public void onChapterSelected(int position) {
SubChaptersListFragment subChaptersListFrag = (SubChaptersListFragment) getChildFragmentManager()
.findFragmentById(R.id.sub_category_fragment);
if (subChaptersListFrag != null) {
subChaptersListFrag.updateList(position);
} else {
SubChaptersListFragment subChapterFragment = new SubChaptersListFragment();
Bundle args = new Bundle();
args.putInt(SubChaptersListFragment.CHAPTER_POSITION, position);
subChapterFragment.setArguments(args);
FragmentTransaction transaction = getChildFragmentManager()
.beginTransaction();
transaction.replace(R.id.fragment_container, subChapterFragment);
// transaction.addToBackStack(null);
transaction.commit();
}
}
@Override
public void onSubChapterSelected(int prev, int position) {
SubSubChaptersListFragment subSubChaptersListFrag = (SubSubChaptersListFragment) getChildFragmentManager()
.findFragmentById(R.id.sub_sub_category_fragment);
if (subSubChaptersListFrag != null) {
subSubChaptersListFrag.updateList(prev, position);
} else {
SubSubChaptersListFragment subSubChapterFragment = new SubSubChaptersListFragment();
Bundle args = new Bundle();
args.putIntArray(SubSubChaptersListFragment.POSITIONS, new int[]{prev, position});
subSubChapterFragment.setArguments(args);
FragmentTransaction transaction = getChildFragmentManager()
.beginTransaction();
transaction.replace(R.id.fragment_container, subSubChapterFragment);
// transaction.addToBackStack(null);
transaction.commit();
}
}
@Override
public void onStop() {
super.onStop();
if (processTask != null && processTask.getStatus() != AsyncTask.Status.FINISHED) {
processTask.cancel(true);
}
}
}