onBackPressed无法正常工作

时间:2014-10-25 01:34:52

标签: java android android-fragments back-button

我试图让后退按钮处理片段。 即时通讯使用onBackPressed ..但每个编译和单击后退按钮, 屏幕总是转到主屏幕,而不是转到上一个片段 我的代码有什么问题?

之前谢谢

这是我的代码

public class FragmentScheduleRoom extends Fragment{
    ListView list;
    // Array of strings storing country names
    String[] room = new String[] {
            "304",
            "305",
            "306",
            "307",
            "308",
    };

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        Log.d("ZZZ", "ada di oncreateView Schedule room");
        super.onCreate(savedInstanceState);
        View rootView = inflater.inflate(R.layout.fragment_layout_schedule_room, container,
     

假);               最终清单> aList = new ArrayList>();

        for(int i=0;i<5;i++) {
            HashMap<String, String> hm = new HashMap<String, String>();
            hm.put("room", room[i]);
            aList.add(hm);
            // R.layout.listview_layout defines the layout of each item
            list = (ListView) rootView.findViewById(R.id.list);
            ListAdapter adapter = new SimpleAdapter(getActivity(), aList, R.layout.list_schedule_room, new String[]{"room"}, new
     

INT [] {R.id.room});

            list.setAdapter(adapter);
            list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view,
                                        int position, long id) {
                    String room = ((TextView) view.findViewById(R.id.room)).getText().toString();
//                    Intent in = new Intent(getActivity(),FragmentScheduleDetail.class);
//                    in.putExtra("txt",day);
                    String selectday = getArguments().getString("selectday");
                    Fragment fragment = new FragmentScheduleDetail();
                    Bundle data = new Bundle();
                    data.putString("selectroom", room);
                    data.putString("selectday", selectday);
                    fragment.setArguments(data);
                    FragmentManager frgManager = getFragmentManager();
                    Log.d("tag", "ROOM SELECT: " + data.getString("selectroom"));
                    frgManager.beginTransaction().replace(R.id.content_frame, fragment)
                            .commit();
                    //Toast.makeText(getActivity(), "You Clicked at " + aList.get(+position).get("day"), Toast.LENGTH_SHORT).show();
                }
            });
        }
        return rootView;
    }
    public void onBackPressed() {
        //super.onBackPressed();
        //Toast.makeText(getApplicationContext(), "click",2000).show();
        Fragment fragment = new FragmentScheduleDay();
        FragmentManager frgManager = getFragmentManager();
        frgManager.beginTransaction().replace(R.id.content_frame, fragment)
                .commit();
    }

}

3 个答案:

答案 0 :(得分:0)

看一下getFragmentManager()。popBackStack()方法

答案 1 :(得分:0)

不需要那样做,只需这样做

 public void onBackPressed() {
    super.onBackPressed();
}

然后在您之前的片段FragmentScheduleDay中,在调用下一个片段(FragmentScheduleRoomFragmentScheduleRoom

时,您将transaction.addToBackStack(null);执行此操作 编辑1:主要是片段替换函数detaches()和attaches()片段..所以要使用你的样式..使用detach(旧片段);然后附上(新片段);然后commit()它将工作..这是正确的代码格式..

尝试让我知道它是否解决了你的问题

答案 2 :(得分:0)

你可以试试这个:

public boolean onKeyDown(int keyCode, KeyEvent event) {
    if ((keyCode == KeyEvent.KEYCODE_BACK)) {
        // do your task here
    }
    return super.onKeyDown(keyCode, event);
}

希望这对你有用。这对我有用。