使用后退按钮返回最后一个片段

时间:2015-09-08 07:00:12

标签: android android-fragments

我想从FragmentB回到FragmentA。任何人都可以告诉我该怎么做?后退按钮只关闭我的应用程序。我想回到之前的片段。这是我的代码:

弗拉加

public class FragA extends Fragment {

    final String[] items = new String[]{"F1", "F2", "F3"
    };

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.layout, container, false);

        ListView list = (ListView) view.findViewById(R.id.fir);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, items);
        list.setAdapter(adapter);

        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                                    long arg3) {

                Fragment myFragment = null;
                switch (arg2){
                    case 0:
                        myFragment = new FragA();
                        break;
                    case 1:
                        myFragment = new FragB();
                        break;
                    case 2:
                        myFragment = new FragC();
                        break;

                }
                // update the main content by replacing fragments
                FragmentManager fragmentManager = getActivity().getSupportFragmentManager();

                fragmentManager.beginTransaction()
                        .replace(R.id.container2, myFragment)

                        .commit();
                fragmentManager.popBackStackImmediate();
            }

        });
        return view;
    }




}

FragB

public class First_frag extends Fragment {

    final String[] items = new String[]{"F1", "F2", "F3", "F4","F5"
    };

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.lay, container, false);

        ListView list = (ListView) view.findViewById(R.id.fir);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, items);
        list.setAdapter(adapter);

        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                                    long arg3) {

                Fragment myFragment = null;
                switch (arg2) {
                    case 0:
                        myFragment = new FragA();
                        break;
                    case 1:
                        myFragment = new FragB();
                        break;
                    case 2:
                        myFragment = new FragC();
                        break;
                    case 3:
                        myFragment = new FragD();
                        break;
                    case 4:
                        myFragment = new FragE();

                        break;

                }
                // update the main content by replacing fragments
                FragmentManager fragmentManager = getActivity().getSupportFragmentManager();

                fragmentManager.beginTransaction()

                        .replace(R.id.container3, myFragment)

                        .commit();


            }

        });
        return view;
    }



}

提前感谢您的帮助。对于我在这里犯下的任何错误,我深表歉意。我是Android新手编程的新手。

1 个答案:

答案 0 :(得分:0)

只需在您的Activity覆盖backPressed()方法:

     @Override
     public void onBackPressed() {
       if (getSupportFragmentManager().getBackStackEntryCount() > 0) {
           getSupportFragmentManager().popBackStack();
       }
       super.onBackPressed();
     }

并删除FragmentA类中的行:

   fragmentManager.popBackStackImmediate();