onBackPress()的通用片段监听器

时间:2015-09-04 09:21:38

标签: android onclicklistener onbackpressed

这个问题可能重复,但我需要了解听众的工作原理。假设我有MyActivity()MyFragment()。当我按下后退按钮在片段类中运行方法doStuff()时,我需要创建一个监听器。 我试着像smth一样:

监听器:

    public interface OnBackPressListener {
    void doBack();
    }
MyActivity类中的代码:

 private OnBackPressListener onBackPressListener;

    ...   

       @Override
        public void onBackPressed() { 
            onBackPressedListener.doBack();
        }
MyFragment类中的

代码:

        public class MyFragment extends Fragment implements OnBackPressListener {

       ...

     @Override
        public void doBack() {
           doStuff();
        }

    private void doStuff(){
    //my work
    }


}

但是我得到了NullPointerException

09-04 05:34:04.637  13074-13074/com.example E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.NullPointerException
            at com.example.ui.activity.MyActivity.onBackPressed(MyActivity.java:370)
            at android.app.Activity.onKeyUp(Activity.java:2159)
            at android.view.KeyEvent.dispatch(KeyEvent.java:2647)
            at android.app.Activity.dispatchKeyEvent(Activity.java:2389)
            at android.support.v7.internal.view.WindowCallbackWrapper.dispatchKeyEvent(WindowCallbackWrapper.java:49)
            at android.support.v7.app.AppCompatDelegateImplBase$AppCompatWindowCallbackBase.dispatchKeyEvent(AppCompatDelegateImplBase.java:258)
            at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1860)
            at android.view.ViewRootImpl$ViewPostImeInputStage.processKeyEvent(ViewRootImpl.java:3791)
            at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3774)
            at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3379)
            at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3429)
            at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3398)
            at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3483)
            at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3406)
            at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3540)
            at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3379)
            at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3429)
            at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3398)
            at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3406)
            at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3379)
            at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3429)
            at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3398)
            at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3516)
            at android.view.ViewRootImpl$ImeInputStage.onFinishedInputEvent(ViewRootImpl.java:3666)
            at android.view.inputmethod.InputMethodManager$PendingEvent.run(InputMethodManager.java:1982)
            at android.view.inputmethod.InputMethodManager.invokeFinishedInputEventCallback(InputMethodManager.java:1698)
            at android.view.inputmethod.InputMethodManager.finishedInputEvent(InputMethodManager.java:1689)
            at android.view.inputmethod.InputMethodManager$ImeInputEventSender.onInputEventFinished(InputMethodManager.java:1959)
            at android.view.InputEventSender.dispatchInputEventFinished(InputEventSender.java:141)
            at android.os.MessageQueue.nativePollOnce(Native Method)
            at android.os.MessageQueue.next(MessageQueue.java:132)
            at android.os.Looper.loop(Looper.java:124)
            at android.app.ActivityThread.main(ActivityThread.java:5103)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)

这370行是:

   onBackPressedListener.doBack();

1 个答案:

答案 0 :(得分:0)

只需覆盖容器活动中的onBackPressed()方法:

     @Override
    public void onBackPressed() {
      //do your stuff here.....
       super.onBackPressed();
}