Fragment響應返回鍵

Activty能夠直接響應返回鍵,而Fragment卻不行,可用以下方式:ide

  建立一個抽象類BackHandledFragment,該類中有一個抽象方法onBackPress(),全部BackHandledFragment的子類在onBackPress()中處理各自對Back事件的響應邏輯。onBackPress()返回boolean值,宿主FragmentActivity將會根據該方法的返回值判斷子Fragment是否有消費Back事件。此外,宿主FragmentActivity還會保持一份當前Fragment的引用,當用戶按下Back鍵時,宿主Activity會判斷當前Fragment是否須要消費改時間,若是沒有Fragment消費纔會本身消費。this

/**
 * Fragment響應返回鍵
 * @author Administrator
 *
 */
public abstract class BackHandledFragment extends Fragment{
    protected BackHandledInterface mBackHandledInterface; 
    public abstract boolean onBackPressed();
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if(!(getActivity() instanceof BackHandledInterface)){
            throw new ClassCastException("Hosting Activity must implement BackHandledInterface");
        }else{
            this.mBackHandledInterface = (BackHandledInterface)getActivity();
        }
    }
    
    @Override
    public void onStart() {
        super.onStart();
        //告訴FragmentActivity,當前Fragment在棧頂
        mBackHandledInterface.setSelectedFragment(this);
    }
}

宿主FragmentActivity需實現BackHandledIntegerface,子Fragment會經過該接口告訴宿主FragmentActivity本身是當前屏幕可見的Fragment.spa

public interface BackHandledInterface {
    public abstract void setSelectedFragment(BackHandledFragment selectedFragment);
}

因此在Fragment的onCreate()中會判斷宿主FragmentActivity是否已實現了該接口。在Fragment的onStart()方法中就會調用該接口告訴宿主Fragment本身是當前屏幕可見的Fragment。code

宿主FragmentActivity就能夠在onBackPressed()方法中對Back時間進行判斷處理了。blog

 public class MainActivity extends FragmentActivity implements BackHandledInterface{
      private BackHandledFragment mBackHandedFragment;
      private boolean hadIntercept;  
      private Button mBtn;
      
      @Override
      protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);
         this.mBtn = (Button)findViewById(R.id.btn);
         this.mBtn.setOnClickListener(new OnClickListener() {
             
             @Override
             public void onClick(View v) {
                 MyFragment f = new MyFragment();
                 FragmentManager fm = getSupportFragmentManager();
                 FragmentTransaction ft = fm.beginTransaction();
                 ft.replace(R.id.container, f);
                 ft.addToBackStack("tag");
                 ft.commit();
             }
         });
     }
 
     @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();
             }
         }
     }
 }

在Fragment中的使用接口

public class MyFragment extends BackHandledFragment {
      private boolean hadIntercept;
      @Override
      public View onCreateView(LayoutInflater inflater,
              @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
          return getLayoutInflater(savedInstanceState).inflate(R.layout.fragment_a, null);
      }
     
     @Override
     protected boolean onBackPressed() {
         if(hadIntercept){
             return false;
         }else{
             Toast.makeText(getActivity(), "Click MyFragment", Toast.LENGTH_SHORT).show();
             hadIntercept = true;
             return true;
         }
     }
 
 }
相關文章
相關標籤/搜索