0

I am having trouble opening a fragment from within another fragment on the click of a button. Everything seems to make sense (to me) and I have tried playing about with my code (changing the layouts, replacing fragments etc) but nothing is working.

Here is my RoleFragment.java (The fragment which contains the button)

public class RolesFragment extends Fragment implements View.OnClickListener {

GridView gridView;
ArrayList<Players> playersList;
MyAdapter adapter;

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

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

    gridView = (GridView) view.findViewById(R.id.gv_players);
    Button nightround = (Button) view.findViewById(R.id.buttonNightRound);

    nightround.setOnClickListener(this);

    DatabaseHelper databaseHelper = new DatabaseHelper(getActivity());
    playersList = new ArrayList<Players>();

    playersList = databaseHelper.getPlayers();
    adapter = new MyAdapter(getActivity(), playersList);
    gridView.setAdapter(adapter);

    return view;
}


@Override
public void onClick(View v) {
    Fragment fragment = null;
    switch (v.getId()) {
        case R.id.buttonNightRound:
            fragment = new NightRound();
            replaceFragment(fragment);
            break;

    }

}

public void replaceFragment(Fragment someFragment) {
    FragmentTransaction transaction = getFragmentManager().beginTransaction();
    transaction.replace(R.id.fragment_container, someFragment);
    transaction.addToBackStack(null);
    transaction.commit();
}

 }

And this is my fragment_viewroles.xml file.

<RelativeLayout 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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.tuss.mafia.GameActivity" >

<Button
    android:id="@+id/buttonNightRound"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Night Round"
    android:onClick="FragmentNightRoundClick"
    android:clickable="true"
    android:layout_weight="2"/>


<GridView
    android:id="@+id/gv_players"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:numColumns="auto_fit"
    android:stretchMode="columnWidth"
    android:columnWidth="150dp"
    android:horizontalSpacing="10dp"
    android:verticalSpacing="10dp"
    android:gravity="center"
    android:layout_below="@id/buttonNightRound">
</GridView>

</RelativeLayout>

The trouble is, when I click the button nothing happens.

Tuss
  • 57
  • 1
  • 9
  • See this link to go a fragment from a fragment by clicking: https://stackoverflow.com/a/57753406/11675817 – M Karimi Sep 02 '19 at 08:20

2 Answers2

0

Try something like the following:

     Fragment fragment = OtherFragment.newInstance();
            android.support.v4.app.FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();
            transaction.replace(R.id.container_layout, fragment, "OtherFragment");// give your fragment container id in first parameter
            transaction.addToBackStack(null);  // if written, this transaction will be added to backstack
            transaction.commit();
Valdio
  • 101
  • 1
  • 6
0

There some problems here. First, you have to add a container with the id R.id.fragment_container inside your fragment like FrameLayout which will store your new fragment.

If your want to open a fragment as a new screen, you have to put it inside a new activity. Fragments are piece of screens and should not be used without activities or view pagers.

Have a look at the Android deverlopers page: http://developer.android.com/training/basics/fragments/communicating.html#DefineInterface

Basically, you define an interface in your Fragment A, and let your Activity implement that Interface. Now you can call the interface method in your Fragment, and your Activity will receive the event. Now in your activity, you can call your second Fragment to update the textview with the received value

// You Activity implements your interface
public class YourActivity implements FragmentA.TextClicked{
    @Override
    public void sendText(String text){
        // Get Fragment B
        FraB frag = (FragB)
            getSupportFragmentManager().findFragmentById(R.id.fragment_b);
        frag.updateText(text);
    }
}


// Fragment A defines an Interface, and calls the method when needed
public class FragA extends Fragment{

    TextClicked mCallback;

    public interface TextClicked{
        public void sendText(String text);
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

        // This makes sure that the container activity has implemented
        // the callback interface. If not, it throws an exception
        try {
            mCallback = (TextClicked) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                + " must implement TextClicked");
        }
    }

    public void someMethod(){
        mCallback.sendText("YOUR TEXT");
    }

    @Override
    public void onDetach() {
        mCallback = null; // => avoid leaking, thanks @Deepscorn
        super.onDetach();
    }
}

// Fragment B has a public method to do something with the text
public class FragB extends Fragment{

    public void updateText(String text){
        // Here you have it
    }
}
Luiz Fernando Salvaterra
  • 4,192
  • 2
  • 24
  • 42