我正在尝试从 fragment
BackPressed
完成我的 Activity 。但是 Key Listener
根本没有被调用。我已经搜索过但是大多数时候都是关于 EditText
的问题。下面是我的代码:
public class ProductFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.product_fragment, container,false);
view.setOnKeyListener( new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if (keyCode == KeyEvent.KEYCODE_BACK) {
getActivity().finish();
return true;
}
return false;
}
});
}
最佳答案
也许你可以覆盖 onBackPressed()在举办 Activity 上。它会给出你正在寻找的结果。查看:http://developer.android.com/reference/android/app/Activity.html#onBackPressed()
关于android - fragment keyListener 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23581495/