如何定义Backpress Action(事件)?

时间:2013-06-04 20:05:07

标签: android

如何在中定义Backpress Action class thats extends Fragment实现ActionBar.TabListener,如何定义一个反向动作?

1 个答案:

答案 0 :(得分:1)

片段没有像活动那样的onBackPressed()回调。您可以尝试让您的Activity维护(或获取)对片段的引用,并让它从onBackPressed()内调用片段。

片段代码:

public boolean onBackPressed() {
    // your special behavior here
    // return true if you have consumed the back press
}

活动代码:

public void onBackPressed() {
    MyFragment fragment = getFragmentManager().findFragmentById(/* some unique id*/);
    // could alternatively use findFragmentByTag() using a unique String
    if (fragment != null) {
        if (fragment.onBackPressed()) return;
    }

    // back press not consumed; allow usual behavior
    super.onBackPressed();
}