Fragments starting twice

后端 未结 1 1781
抹茶落季
抹茶落季 2021-01-24 02:01

I have this app in which im using fragments, but somehow everytime I start the app, every activity(fragment) is started two times. I\'m banging my head about this but I cannot f

相关标签:
1条回答
  • 2021-01-24 02:15

    Maybe using a newInstance method will correct this starting of two fragments. Ad populum I see alot of newInstance code when I'm looking at fragment examples.

    public class MainFragment extends Fragment {
    
    public static Fragment newInstance(int instance# pass in receipieces and cats here) {
        MainFragment f = new MainFragment();
        // Supply num input as an argument.
        Bundle args = new Bundle();
        args.putParcelableArrayList("all", recepies);
        args.putSerializable("cats", cats);
        args.putParcelableArrayList(Constants.MAINRECEPIES, mainRecepies);
        args.putBoolean("search", true);
        fragment.setArguments(args);
        return f;
    }
    

    meanwhile back in the activity that shows the fragment use new instance to start the fragment.

    private void showMainFragment() {
        Fragment fragment = MainFragment.newInstacne(instance# pass in recipies and cats here);
    
        FragmentManager fragmentManager = getFragmentManager();
        fragmentManager.beginTransaction().add(R.id.content_frame, fragment)
                .commit();
    
    }
    
    0 讨论(0)
提交回复
热议问题