Android and androidsdk
Dry goods (1)
First, create an abstract class BackHandledFragment. This class has an abstract method onBackPressed (). All BackHandledFragment subclasses process their respective consumption logic for the Back event in the onBackPressed method. OnBackPressed returns a Boolean value. The host FragmentActivity checks whether the Sub-Fragment consumes a Back event based on the return value of this method. In addition, the host FragmentActivity maintains a reference of the current Fragment. When you press the Back key, the host Activity determines whether the current Fragment needs to consume the event, only Fragment consumption is required.
Public abstract class BackHandledFragment extends Fragment {protected BackHandledInterface mBackHandledInterface; /*** all subclasses that inherit the BackHandledFragment class will implement the logic after the physical Back key is pressed in this method * FragmentActivity captures the physical return key click event and first asks if Fragment consumes the event * if there is no Fragment message, FragmentActivity will consume this event by itself */protected abstract boolean onBackPressed (); @ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstance State); if (! (GetActivity () instanceof BackHandledInterface) {throw new ClassCastException ("Hosting Activity must implement BackHandledInterface");} else {this. mBackHandledInterface = (BackHandledInterface) getActivity () ;}@ Override public void onStart () {super. onStart (); // tell the FragmentActivity that the current Fragment is at the top of the stack mBackHandledInterface. setSelectedFragment (this );}}
The host FragmentActivity must inherit BackHandledIntegerface. The sub-Fragment informs the host FragmentActivity that it is the Fragment visible on the current screen.
public interface BackHandledInterface { public abstract void setSelectedFragment(BackHandledFragment selectedFragment); }
Therefore, in onCreate of Fragment, it will determine whether the host FragmentActivity has inherited this interface. In the onStart () method of Fragment, this interface is called to tell the host that FragmentActivity is the Fragment visible on the current screen.
The host FragmentActivity can judge and process the Back event in the onBackPressed () method.
public class MainActivity extends FragmentActivity implements BackHandledInterface{ private BackHandledFragment mBackHandedFragment; private boolean hadIntercept; @Override public void setSelectedFragment(BackHandledFragment selectedFragment) { this.mBackHandedFragment = selectedFragment; } @Override public void onBackPressed() { if(mBackHandedFragment == null || !mBackHandedFragment.onBackPressed()){ if(getSupportFragmentManager().getBackStackEntryCount() == 0){ super.onBackPressed(); }else{ getSupportFragmentManager().popBackStack(); } } } }Dry Goods (2)
In the onResume of Fragment, View the listener. (Pay attention to setFocusableInTouchMode () and requestFocus ())
@Overridepublic void onResume() { super.onResume(); getView().setFocusableInTouchMode(true); getView().requestFocus(); getView().setOnKeyListener(new View.OnKeyListener() { @Override public boolean onKey(View v, int keyCode, KeyEvent event) { if (event.getAction() == KeyEvent.ACTION_UP && keyCode == KeyEvent.KEYCODE_BACK){ // handle back button return true; } return false; } });}I am the dividing line of tiantiao
Reference: http://vinsol.com/blog/2014/10/01/handling-back-button-press-inside-fragments/
Http://stackoverflow.com/questions/22552958/handling-back-press-when-using-fragments-in-android