0
我試圖從我的各種片段打開家庭片段,如果我單擊後退按鈕(設備後退按鈕)。但不是替換片段,應用程序關閉並出現設備的菜單畫面。我無法解決發生了什麼問題。並沒有錯誤。請幫助我..片段後退按鈕代碼更改
HomeFragment:
public class HomeFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.home, container, false);
return view;
}
OneFragment:
public class OneFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_one, container, false);
OnKeyListener pressed = new OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
return true;
}
return false;
}
};
view.setOnKeyListener(pressed);
return view;
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
Fragment newFragment = new HomeFragment();
FragmentTransaction transaction = getFragmentManager()
.beginTransaction();
transaction.replace(R.id.content_frame, newFragment);
// transaction.addToBackStack(null);
transaction.commit();
return false;
}
return super.getActivity().onKeyDown(keyCode, event);
}
}
我甚至使用onBackPressed嘗試()的片段方法。但沒有期望的輸出。
twoFragment:
public class TwoFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_two, container, false);
return view;
}
public void onBackPressed() {
Fragment newFragment = new WordFragment();
FragmentTransaction transaction = getFragmentManager()
.beginTransaction();
transaction.replace(R.id.content_frame, newFragment);
// transaction.addToBackStack(null);
transaction.commit();
super.getActivity().onBackPressed();
}
}
請幫助我..告訴我,我要去哪裏錯了。 我只想說哪個片段被打開,同時單擊後退按鈕應該打開homefragment類..
什麼都沒有發生。我改變了我的代碼bt沒有收到所需的輸出 – Falguni 2014-10-09 20:06:11
您不必覆蓋KeyEvent.KEYCODE_BACK – JoaoBiriba 2014-10-09 20:21:14
即使我刪除覆蓋,仍然沒有改變。 – Falguni 2014-10-09 20:39:39