gpt4 book ai didi

android - 关于android View 寻呼机中嵌套 fragment 中的回栈的问题

转载 作者:行者123 更新时间:2023-11-29 17:51:02 25 4
gpt4 key购买 nike

我开发了一个有一个 Activity 的应用程序,在这个 Activity 中,抽屉中有很多 fragment ,在一个 fragment 中,我设置了 View 寻呼机来显示多个 fragment 实例,它的工作完美,但是当我在 View 寻呼机中打开另一个 fragment (嵌套 fragment )时,它工作但是当我按下后退按钮时它的整个应用程序关闭所以 fragment 管理器中的后退堆栈有什么问题我使用子 fragment 管理器所以我想要实现就像我按下它时打开前一个 fragment

我的代码是

    pageAdapter = new MyPageAdapter(getChildFragmentManager(), Global.DatabaseHandler.DisplayTempleList(), activityType);

pager = (ViewPager) v.findViewById(R.id.pager);
pager.setAdapter(pageAdapter);
indicator = (TabPageIndicator) v.findViewById(R.id.indicator);
indicator.setViewPager(pager);


return v;
}

// private List<Fragment> getFragments() {
//
// List<Fragment> fList = new ArrayList<Fragment>();
// //TempleLists = Global.DatabaseHandler.DisplayTempleList();
//
// for (TempleDetail templeDetail : TempleLists.Temple) {
// if (activityType == Global.About) {
// fList.add(new About_Fragment(templeDetail.temple_id, templeDetail.temple_name));
// } else if (activityType == Global.DailyDarshan) {
// fList.add(new DailyDarshan_Activity(templeDetail.temple_id, templeDetail.temple_name));
// } else if (activityType == Global.PhotoGallery) {
// fList.add(new PhotoGallery_Activity(templeDetail.temple_id, templeDetail.temple_name));
// }
// }
//
// return fList;
// }

private class MyPageAdapter extends FragmentPagerAdapter {
private HashMap<Integer, Fragment> fragments;
private TempleList TempleLists;
private String ActivityType;

//public MyPageAdapter(FragmentManager fm, List<Fragment> fragments) {
public MyPageAdapter(FragmentManager fm, TempleList templeList, String activityType) {
super(fm);
fragments = new HashMap<Integer, Fragment>();
TempleLists = templeList;
ActivityType = activityType;
}

@Override
public Fragment getItem(int position) {

if (!fragments.containsKey(position)) {
Fragment fragment = new Sub_container(TempleLists.Temple.get(position).temple_id,
TempleLists.Temple.get(position).temple_name, ActivityType);
fragments.put(position, fragment);
}

return this.fragments.get(position);
}

@Override
public int getCount() {
return TempleLists.Temple.size();
}

@Override
public CharSequence getPageTitle(int position) {
//return TempleLists.Temple.get(position % TempleLists.Temple.size()).temple_name.toUpperCase();
return TempleLists.Temple.get(position).temple_name.toUpperCase();
}
}

}


other fragment

Fragment fragment = new FullImageActivity();

FragmentTransaction transaction = getFragmentManager ().beginTransaction();
transaction.add(R.id.sub_container, fragment);
transaction.addToBackStack(null);
transaction.commit();

Bundle args = new Bundle();
args.putInt(Global.Id, position);
args.putString(Global.TempleId, getArguments().getString(Global.TempleId));
fragment.setArguments(args);

我的背压密码
    @Override
public void onBackPressed() {
FragmentManager fragmentManager = getSupportFragmentManager();
Log.i("-----------------", "---------------" + getFragmentManager().getBackStackEntryCount() + "---------");
if (fragmentManager.getBackStackEntryCount() == 0) {
if (doubleBackToExitPressedOnce) {

super.onBackPressed();
return;
}
this.doubleBackToExitPressedOnce = true;
Global.interstitial.loadAd(Global.adRequest);
Toast.makeText(this, "Press Again to Exit", Toast.LENGTH_SHORT).show();
} else {
fragmentManager.popBackStack();
removeCurrentFragment();
}
}

最佳答案

您必须在 backstack 中添加嵌套 fragment ,

 public void changePage(int id, Fragment frag) {

String backStateName = frag.getClass().getName();
changePage(id, frag, backStateName);
}



public void changePage(int id, Fragment frag, String backStateName) {

imm.hideSoftInputFromWindow(backView.getWindowToken(), 0);

transaction = fragmentManager.beginTransaction();

transaction.setCustomAnimations(R.animator.righttoleft_1, R.animator.righttoleft_2, R.animator.lefttoright_1, R.animator.lefttoright_2);

boolean fragmentPopped = fragmentManager.popBackStackImmediate (backStateName, 0);

if (!fragmentPopped){ //fragment not in back stack, create it.

transaction.replace(id, frag);
transaction.addToBackStack(backStateName);
transaction.commit();
}

}

并将此代码添加到您的 Activity 中。
@Override
public void onBackPressed() {
// TODO Auto-generated method stub


if (fragmentManager.getBackStackEntryCount() > 1) {
Log.i("MainActivity", "popping backstack");
fragmentManager.popBackStack();
} else {
Log.i("MainActivity", "nothing on backstack, calling super");
super.onBackPressed();
}
}

关于android - 关于android View 寻呼机中嵌套 fragment 中的回栈的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22854007/

25 4 0