1

I have a 2 fragments, one is my main fragment that I initialize in the main activity, and the other is one that I want to instantiate when a button is clicked on the first fragment. I am having issues adding fragment 2, but I can replace with no problem. The error I get is one I've seen many times asked online in different ways, but I cannot find a solution to specifically instantiate the 2nd fragment from the 1st fragment dynamically/programmatically.

Is there something I'm missing in my code?

In My activity:

    public void showFragment_1(){

        this.rl = new FrameLayout(this);
        this.rl.setLayoutParams(new FrameLayout.LayoutParams((int)Globals.screen_width,(int)(Globals.screen_height)));
        this.rl.setId((int)133);
        mainFragment = new MainFragment(rl);
        setContentView(rl);

        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();

        DisplayMetrics displayMetrics = new DisplayMetrics();
        getWindow().getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
        Globals.screen_height = displayMetrics.heightPixels;
        Globals.screen_width = displayMetrics.widthPixels;
        rl.setY(displayMetrics.heightPixels * 0.08f);
        rl.setElevation(5.1f);
        ft.add(rl.getId(),mainFragment, "MainFragment");
        ft.addToBackStack("MainFragment");
        ft.commitAllowingStateLoss();
    }

In Fragment 1:

private View.OnClickListener displayNewFragment = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int i = 0;
                int id = 0;

                for (Fragment frag : Globals.frag_mgr.getFragments()){
                    if (frag.getTag() == "MainFragment"){
                        FrameLayout fragment_container = new FrameLayout(context);
                        fragment_container.setLayoutParams(new FrameLayout.LayoutParams((int)Globals.screen_width,(int)(Globals.screen_height)));
                        fragment_container.setId((int)2020);
                        switch (v.getId()){
                            // Test for add - not working
                            case 0:
                                LinearLayout ll = new LinearLayout(context);
                                ll.setOrientation(LinearLayout.HORIZONTAL);
                                ll.setId((int)12345);
                                NewFragment newFragment = new NewFragment(frameLayout);
                                getChildFragmentManager().beginTransaction().add(frag_container.getId(), newFragment).addToBackStack(null).commit();
                                break;
                            // Test for replace working, but the view is changing fragment 1
                            case 1:
                                NewFragment newFragment = new NewFragment(fragment_container);
                                getActivity().getSupportFragmentManager().beginTransaction().replace(fragment1.getId(),newFragment).addToBackStack(null).commit();
                                break;
                        }

                    }
                    i+=1;
                }
            }
        };

NOTE : I'm sharing my xml in the case that I need it, but if possible I rather not. I prefer working dynamically.

xml for fragment1:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainFragment" />

xml for fragment 2:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/frag_new"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".NewFragment">

    <!-- TODO: Update blank fragment layout -->
    <FrameLayout
        android:id="@id/frag_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="@string/hello_blank_fragment" />

</FrameLayout>
1QuickQuestion
  • 729
  • 2
  • 9
  • 25

0 Answers0