嵌套的片段与backstack简历 [英] Nested Fragment with backstack Resume
问题描述
在我的应用程序有几个片段
S IN的活动
和我保持了 backStack
这些片段
。一切都是好的,但其中的一个嵌套的片段。当我把它变成 backStack
键,再由pressing后退按钮恢复中,片段看起来overllaping previous内容(子片段)。
这是正常的观点:
这是重叠视图的截图(当我恢复片段):
您可以得到不同的是,第二个文本是比较深的(这意味着子片段重叠)
我怎样才能避免这种情况?这在code为我的嵌套的片段:
公共类CompetitiveProgramming扩展SherlockProgressFragment工具
OnChapterSelectListener,OnSubChapterSelectListener { 查看mContentView;
静态公开名单<&章GT; chapterList =新的ArrayList<&章GT;();
私人ProcessTask processTask = NULL; @覆盖
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
this.setRetainInstance(真);
} @覆盖
公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,
捆绑savedInstanceState){
mContentView = inflater.inflate(
R.layout.competitive_programming_exercise,集装箱,FALSE);
返回super.onCreateView(充气器,容器,savedInstanceState);
} @覆盖
公共无效onActivityCreated(捆绑savedInstanceState){
super.onActivityCreated(savedInstanceState);
setContentShown(假);
的setContentView(mContentView);
processTask =新ProcessTask();
processTask.execute();
}
保护类ProcessTask扩展的AsyncTask<太虚,太虚,太虚> { @覆盖
保护无效doInBackground(虚空...... PARAMS){
//后台任务
返回null;
} @覆盖
保护无效onPostExecute(虚空结果){
super.onPostExecute(结果);
FragmentTransaction交易= getChildFragmentManager()
.beginTransaction();
如果(mContentView.findViewById(R.id.fragment_container)!= NULL){
getChildFragmentManager()调用BeginTransaction()
。新增(R.id.fragment_container,新ChaptersListFragment())提交()。
}其他{
transaction.add(R.id.category_fragment,新ChaptersListFragment());
transaction.add(R.id.sub_category_fragment,新SubChaptersListFragment());
transaction.add(R.id.sub_sub_category_fragment,
新SubSubChaptersListFragment());
}
器transaction.commit();
setContentShown(真);
} } 静态保护类{章
串CHAPTERTITLE;
清单<&子章GT; subchapterList; 市民章(字符串CHAPTERTITLE,列表与LT;小节> subchapterList){
this.chapterTitle = CHAPTERTITLE;
this.subchapterList = subchapterList;
} } @覆盖
公共无效onChapterSelected(INT位置){
SubChaptersListFragment subChaptersListFrag =(SubChaptersListFragment)getChildFragmentManager()
.findFragmentById(R.id.sub_category_fragment);
如果(subChaptersListFrag!= NULL){
subChaptersListFrag.updateList(位置);
}其他{
SubChaptersListFragment subChapterFragment =新SubChaptersListFragment();
捆绑ARGS =新包();
args.putInt(SubChaptersListFragment.CHAPTER_POSITION,位置);
subChapterFragment.setArguments(参数);
FragmentTransaction交易= getChildFragmentManager()
.beginTransaction();
transaction.replace(R.id.fragment_container,subChapterFragment);
// transaction.addToBackStack(NULL);
器transaction.commit();
}
} @覆盖
公共无效onSubChapterSelected(INT preV,INT位置){
SubSubChaptersListFragment subSubChaptersListFrag =(SubSubChaptersListFragment)getChildFragmentManager()
.findFragmentById(R.id.sub_sub_category_fragment);
如果(subSubChaptersListFrag!= NULL){
subSubChaptersListFrag.updateList(preV,位置);
}其他{
SubSubChaptersListFragment subSubChapterFragment =新SubSubChaptersListFragment();
捆绑ARGS =新包();
args.putIntArray(SubSubChaptersListFragment.POSITIONS,新的INT [] {preV,位置});
subSubChapterFragment.setArguments(参数);
FragmentTransaction交易= getChildFragmentManager()
.beginTransaction();
transaction.replace(R.id.fragment_container,subSubChapterFragment);
// transaction.addToBackStack(NULL);
器transaction.commit();
}
} @覆盖
公共无效的onStop(){
super.onStop();
如果(processTask = NULL&放大器;!&安培;!processTask.getStatus()= AsyncTask.Status.FINISHED){
processTask.cancel(真);
}
}}
基里尔库拉科夫是正确的。 替换
而不是添加
应使用。我编辑了code:
公共类CompetitiveProgramming扩展SherlockProgressFragment工具
OnChapterSelectListener,OnSubChapterSelectListener { 查看mContentView;
静态公开名单<&章GT; chapterList =新的ArrayList<&章GT;();
私人ProcessTask processTask = NULL;
片段chapterFragment = NULL;
片段subChapterFragment = NULL;
片段subSubChapterFragment = NULL; @覆盖
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
this.setRetainInstance(真);
} @覆盖
公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,
捆绑savedInstanceState){
mContentView = inflater.inflate(
R.layout.competitive_programming_exercise,集装箱,FALSE);
返回super.onCreateView(充气器,容器,savedInstanceState);
} @覆盖
公共无效onActivityCreated(捆绑savedInstanceState){
super.onActivityCreated(savedInstanceState);
setContentShown(假);
的setContentView(mContentView);
processTask =新ProcessTask();
processTask.execute();
}
保护类ProcessTask扩展的AsyncTask<太虚,太虚,太虚> { @覆盖
保护无效doInBackground(虚空...... PARAMS){
//后台任务
返回null;
} @覆盖
保护无效onPostExecute(虚空结果){
super.onPostExecute(结果);
FragmentTransaction交易= getChildFragmentManager()
.beginTransaction();
chapterFragment =新ChaptersListFragment();
如果(mContentView.findViewById(R.id.fragment_container)!= NULL){
transaction.replace(R.id.fragment_container,chapterFragment);
}其他{
subChapterFragment =新SubChaptersListFragment();
subSubChapterFragment =新SubSubChaptersListFragment();
transaction.replace(R.id.category_fragment,chapterFragment);
transaction.replace(R.id.sub_category_fragment,subChapterFragment);
transaction.replace(R.id.sub_sub_category_fragment,subSubChapterFragment);
}
器transaction.commit();
setContentShown(真);
} } 静态保护类{章
串CHAPTERTITLE;
清单<&子章GT; subchapterList; 市民章(字符串CHAPTERTITLE,列表与LT;小节> subchapterList){
this.chapterTitle = CHAPTERTITLE;
this.subchapterList = subchapterList;
} } @覆盖
公共无效onChapterSelected(INT位置){
SubChaptersListFragment subChaptersListFrag =(SubChaptersListFragment)getChildFragmentManager()
.findFragmentById(R.id.sub_category_fragment);
如果(subChaptersListFrag!= NULL){
subChaptersListFrag.updateList(位置);
}其他{
SubChaptersListFragment subChapterFragment =新SubChaptersListFragment();
捆绑ARGS =新包();
args.putInt(SubChaptersListFragment.CHAPTER_POSITION,位置);
subChapterFragment.setArguments(参数);
FragmentTransaction交易= getChildFragmentManager()
.beginTransaction();
transaction.replace(R.id.fragment_container,subChapterFragment);
// transaction.addToBackStack(NULL);
器transaction.commit();
}
} @覆盖
公共无效onSubChapterSelected(INT preV,INT位置){
SubSubChaptersListFragment subSubChaptersListFrag =(SubSubChaptersListFragment)getChildFragmentManager()
.findFragmentById(R.id.sub_sub_category_fragment);
如果(subSubChaptersListFrag!= NULL){
subSubChaptersListFrag.updateList(preV,位置);
}其他{
SubSubChaptersListFragment subSubChapterFragment =新SubSubChaptersListFragment();
捆绑ARGS =新包();
args.putIntArray(SubSubChaptersListFragment.POSITIONS,新的INT [] {preV,位置});
subSubChapterFragment.setArguments(参数);
FragmentTransaction交易= getChildFragmentManager()
.beginTransaction();
transaction.replace(R.id.fragment_container,subSubChapterFragment);
// transaction.addToBackStack(NULL);
器transaction.commit();
}
} @覆盖
公共无效的onStop(){
super.onStop();
如果(processTask = NULL&放大器;!&安培;!processTask.getStatus()= AsyncTask.Status.FINISHED){
processTask.cancel(真);
}
}}
希望这将有助于!
In my application there are several fragment
s in an activity
and I am maintaining a backStack
for these fragment
. Everything is okay, but there is a nested fragment among them. When I put it into backStack
and again resume in by pressing back button, the fragment looks overllaping previous content (child fragment).
This is the normal view:
This is the screenshot of overlapping view (when I resume the fragment):
You can get the difference as the second one's text is more deep (that means child fragment is overlapping)
How can I avoid that? This the code for my nested fragment:
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);
}
}
}
Kirill Kulakov is right. replace
instead of add
should be used. I edited the code:
public class CompetitiveProgramming extends SherlockProgressFragment implements
OnChapterSelectListener, OnSubChapterSelectListener {
View mContentView;
static public List<Chapter> chapterList = new ArrayList<Chapter>();
private ProcessTask processTask = null;
Fragment chapterFragment = null;
Fragment subChapterFragment = null;
Fragment subSubChapterFragment = 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();
chapterFragment = new ChaptersListFragment();
if (mContentView.findViewById(R.id.fragment_container) != null) {
transaction.replace(R.id.fragment_container, chapterFragment);
} else {
subChapterFragment = new SubChaptersListFragment();
subSubChapterFragment = new SubSubChaptersListFragment();
transaction.replace(R.id.category_fragment, chapterFragment);
transaction.replace(R.id.sub_category_fragment, subChapterFragment);
transaction.replace(R.id.sub_sub_category_fragment, subSubChapterFragment);
}
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);
}
}
}
Hope this would help!
这篇关于嵌套的片段与backstack简历的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!