我正在使用片段执行Android应用程序,但是我以前没有使用片段的经验。

我有一个主要的FragmentActivity,我在其中加载一个主视图,然后将我的片段类称为:

public class MainActivity extends FragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main_view);
        FragmentTransaction FT = getFragmentManager().beginTransaction();

        if (findViewById(R.id.fragmentAction) != null) {

            FT.replace(R.id.fragmentAction, MainMenuFragment.newInstance(
                    getString(R.string.main_menu), getApplicationContext()));

        }

        FT.addToBackStack(null);
        FT.commit();
    }
@Override
    public void onBackPressed() {

    }


这是我脆弱的课堂,当我按下按钮调用其他片段时:

public class MainMenuFragment extends Fragment {

        private static String my_description = "";
        private static Context my_context = null;

        public static MainMenuFragment newInstance(String description,
                Context context) {
            my_description = description;
            my_context = context;
            MainMenuFragment f = new MainMenuFragment();
            return f;
        }

        public MainMenuFragment() {

        }



        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            super.onCreateView(inflater, container, savedInstanceState);
            View view = null;

            if (my_description.compareTo(getString(R.string.main_menu)) == 0) {

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

            }

            return view;
        }



        @Override
        public void onActivityCreated(Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);

            if (my_description.compareTo(getString(R.string.main_menu)) == 0) {

                Button new_user = (Button) getView().findViewById(
                        R.id.button_newUser);
                new_user.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {

                        FragmentTransaction FT = getFragmentManager()
                                .beginTransaction();
                        FT.replace(R.id.fragmentAction, LdapFragment.newInstance(
                                getString(R.string.new_user), my_context));
                        FT.commit();

                    }
                });



            }

        }


    }


我的问题是:当我按回键时,我该怎么做才能返回到以前的片段?我在FragmentActivity中添加onBackPressed函数并捕获事件,但是我该怎么办?

最佳答案

使用此代码

@Override
    public void onBackPressed() {
        // TODO Auto-generated method stub
        if (getSupportFragmentManager().getBackStackEntryCount() == 0) {
            this.finish();
        } else {
            getSupportFragmentManager().popBackStack();
            removeCurrentFragment();
        }
    }



    public void removeCurrentFragment() {
        FragmentTransaction transaction = getSupportFragmentManager()
                .beginTransaction();
        Fragment currentFrag = getSupportFragmentManager().findFragmentById(
                R.id.fragment_container);
        setFragName("NONE");
        if (currentFrag != null) {
            setFragName(currentFrag.getClass().getSimpleName());
        }
        if (currentFrag != null) {
            transaction.remove(currentFrag);
        }
        transaction.commit();

    }

10-05 23:35