本文共 4194 字,大约阅读时间需要 13 分钟。
本文翻译自:
I have some fragments in my activity 我的活动中有一些碎片
[1], [2], [3], [4], [5], [6]
And on Back Button Press I must to return from [2] to [1] if current active fragment is [2], or do nothing otherwise. 如果当前活动片段为[2],则在返回按钮上按I必须从[2]返回到[1],否则不执行任何操作。
What is the best practise to do that? 最佳做法是什么?
EDIT : Application must not return to [2] from [3]...[6] 编辑 :应用程序不得从[3] ... [6]返回到[2]
参考:
I'd rather do something like this: 我宁愿做这样的事情:
private final static String TAG_FRAGMENT = "TAG_FRAGMENT";private void showFragment() { final Myfragment fragment = new MyFragment(); final FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); transaction.replace(R.id.fragment, fragment, TAG_FRAGMENT); transaction.addToBackStack(null); transaction.commit();}@Overridepublic void onBackPressed() { final Myfragment fragment = (Myfragment) getSupportFragmentManager().findFragmentByTag(TAG_FRAGMENT); if (fragment.allowBackPressed()) { // and then you define a method allowBackPressed with the logic to allow back pressed or not super.onBackPressed(); }}
I'm working with SlidingMenu and Fragment, present my case here and hope helps somebody. 我正在使用SlidingMenu和Fragment,在此介绍我的情况,希望对您有所帮助。
Logic when [Back] key pressed : 按下[返回]键时的逻辑:
SlidingMenu not shows, current Fragment is #0, do the original [Back] key does. 滑动菜单未显示,当前片段为#0,执行原始的[返回]键。
public class Main extends SherlockFragmentActivity { private SlidingMenu menu=null; Constants.VP=new ViewPager(this); //Some stuff... @Override public void onBackPressed() { if(menu.isMenuShowing()) { menu.showContent(true); //Close SlidingMenu when menu showing return; } else { int page=Constants.VP.getCurrentItem(); if(page>0) { Constants.VP.setCurrentItem(page-1, true); //Show previous fragment until Fragment#0 return; } else {super.onBackPressed();} //If SlidingMenu is not showing and current Fragment is #0, do the original [Back] key does. In my case is exit from APP } } }
The most ideal way of doing this is found here: 最理想的方法是在这里找到:
public class MyActivity extends Activity{ //... //Defined in Activity class, so override @Override public void onBackPressed() { super.onBackPressed(); myFragment.onBackPressed(); }}public class MyFragment extends Fragment{ //Your created method public static void onBackPressed() { //Pop Fragments off backstack and do your other checks }}
Or you could use getSupportFragmentManager().getBackStackEntryCount()
to check what to do: 或者,您可以使用getSupportFragmentManager().getBackStackEntryCount()
来检查该怎么做:
@Override public void onBackPressed() { logger.d("@@@@@@ back stack entry count : " + getSupportFragmentManager().getBackStackEntryCount()); if (getSupportFragmentManager().getBackStackEntryCount() != 0) { // only show dialog while there's back stack entry dialog.show(getSupportFragmentManager(), "ConfirmDialogFragment"); } else if (getSupportFragmentManager().getBackStackEntryCount() == 0) { // or just go back to main activity super.onBackPressed(); } }
if you overide the onKey method for the fragment view you're gonna need : 如果您为片段视图覆盖onKey方法,则将需要:
view.setFocusableInTouchMode(true); view.requestFocus(); view.setOnKeyListener(new View.OnKeyListener() { @Override public boolean onKey(View v, int keyCode, KeyEvent event) { Log.i(tag, "keyCode: " + keyCode); if( keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) { Log.i(tag, "onKey Back listener is working!!!"); getFragmentManager().popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE); return true; } return false; } });
转载地址:http://kelgj.baihongyu.com/