我制作了一个NavigationDrawer
片段,其中包含Home
,Settings
,Feedback
等项目。单击主页时home
项应该打开,其中home是应用程序启动时打开的初始页面。
打开我正在使用的项目的页面的过程就是我用这段代码用其他片段替换layout
if (fragment != null) {
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.relative, fragment).commit();
menuList.setItemChecked(position, true);
menuList.setSelection(position);
getActivity().setTitle(ittems[position]);
一切正常,当我尝试点击home
按钮时,如何进入初始/起始页?
我所做的就是点击home
项目我正在初始化intent
并启动activity
,但我不喜欢它activity
和当你调用活动时它会突然关闭抽屉,不像使用我用于所有其他项目的替换过程顺利关闭抽屉,当它返回时感觉应用程序已经重新启动。
所以我想知道有没有办法回到初始页面或取消片段转换替换布局?
使用代码更新
menuList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Fragment fragment = null;
switch (position) {
case 0:Intent intent = new Intent(getActivity(), MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
getActivity().overridePendingTransition(0, 0);
startActivity(intent);
break;
case 1:
Bundle overview = new Bundle();
overview.putInt("overview", finalPage);
overview.putInt("month", tm+1);
overview.putInt("year", ty);
fragment = new OverviewFragment();
fragment.setArguments(overview);
break;
case 2:
fragment = new StatisticsFragment();
break;
case 3:
fragment= new SomeFragment();
break;
case 4:
fragment = new SettingsFragment();
break;
case 5:fragment= new FeedbackFragment();
break;
}
if (fragment != null) {
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.relative, fragment).commit();
menuList.setItemChecked(position, true);
menuList.setSelection(position);
getActivity().setTitle(ittems[position]);
}
mDrawerLayout.closeDrawer(Gravity.START);
}
});
解决
经过艰苦的一天研究后,我得到了解决这个问题的方法..我在这里张贴它,任何人都在寻找它
这就是我所做的
menuList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Fragment fragment = null;
FragmentTransaction fragmentManager = getFragmentManager().beginTransaction();
FragmentManager fragmentManager1= getFragmentManager();
switch (position) {
case 0:
while (fragmentManager1.getBackStackEntryCount()!=0){
fragmentManager1.popBackStackImmediate();
}
break;
case 1:
Bundle overview = new Bundle();
overview.putInt("overview", finalPage);
overview.putInt("month", tm+1);
overview.putInt("year", ty);
fragment = new OverviewFragment();
fragment.setArguments(overview);
break;
case 2:
fragment = new StatisticsFragment();
break;
case 3:
fragment= new SomeFragment();
break;
case 4:
fragment = new SettingsFragment();
break;
case 5:fragment= new FeedbackFragment();
break;
}
if (fragment != null) {
fragmentManager.addToBackStack("back");
fragmentManager
.replace(R.id.relative, fragment).commit();
menuList.setItemChecked(position, true);
menuList.setSelection(position);
getActivity().setTitle(ittems[position]);
}
mDrawerLayout.closeDrawer(Gravity.START);
}
});
我在这里尝试的概念是我将所有碎片放到后台堆叠中,当点击home
项目时,我正在清除完整的后台堆栈
答案 0 :(得分:0)
将此代码放入“后退”或“主页”按钮 你也可以把它放到@override方法onBackPressed()上。 希望这可以帮助你:)
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
<CurrentFragmane> fragment = (<CurrentFragmane>) getFragmentManager()
.findFragmentByTag("<CurrentFragmane>");
fragmentTransaction.remove(fragment);
fragmentTransaction.commit();
getFragmentManager().popBackStack();