返回先前的片段

时间:2014-11-25 12:14:15

标签: java android fragment

我正在使用片段进行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() {

    }

这是我的fragmet classm,当我按下按钮调用其他片段时:

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函数并捕获事件,但我该怎么做?

3 个答案:

答案 0 :(得分:1)

使用此代码

@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();

    }

答案 1 :(得分:0)

以下是我使用的代码。你不需要做任何事情。 Android系统处理backpress并在导航顺序中显示相应的片段

@Override
    public void onBackPressed() {
        super.onBackPressed();
        if (getFragmentManager().getBackStackEntryCount() == 0)
            finish();
}
你错过了super.onBackPressed();电话。尝试将其添加到您的代码中。

答案 2 :(得分:0)

只需创建添加和返回片段的方法。

例如,

public void setFragment(Fragment fragment, boolean backStack, String tag) {
    FragmentManager manager = getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = manager.beginTransaction();
    if (backStack) {
        fragmentTransaction.addToBackStack(tag);
    }
    fragmentTransaction.replace(R.id.fragmentAction, MainMenuFragment.newInstance(
                getString(R.string.main_menu), tag);
    fragmentTransaction.commit();
}

来自您的活动的onCreate()onResume()的致电方法。

setFragment(Your FragmentClass object, true, "tag");