Search code examples
androidandroid-fragmentsandroid-fragment-manager

Fragment is not starting from menu item when pressed


I'm trying to start a fragment from the toolbar (the toolbar is in a fragment too) but it's not starting, that was not the case when it was in the main activity it was working fine.

If You Want More Reference of any file or want to see other files please tell me I will upload it

Java Files

Home_Fragment.java

public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_home, container, false);

    MaterialToolbar materialToolbar = view.findViewById(R.id.tool_bar);
            materialToolbar.setOnMenuItemClickListener(toolbarItemClickListener);
return view;
    }
    
    private final MaterialToolbar.OnMenuItemClickListener toolbarItemClickListener =
            (MenuItem item) -> {
                Fragment selectedFragment = null;
                if (item.getItemId() == R.id.search_button) {
                    selectedFragment = new Search_Fragment();
                }
               
                return true;
            };

XML Files

tool_bar.xml(menu XML)

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/search_button"
        android:icon="@drawable/ic_baseline_search_24"
        android:title=""
        app:showAsAction="always"
        android:iconTint="@color/white"/>

</menu>

tool_bar.xml

<com.google.android.material.appbar.MaterialToolbar xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/tool_bar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:background="@color/black"
    app:itemIconSize="29dp"
    app:layout_scrollFlags="scroll|enterAlways"
    app:menu="@menu/tool_bar">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="@string/app"
        android:textColor="@color/white"
        android:textSize="23sp"
        android:textStyle="bold" />

</com.google.android.material.appbar.MaterialToolbar>

Solution

  • You've just created a new instance of your Fragment, but didn't add it to the FragmentManager, so it didn't start. You have to make a FragmentTransaction to start the fragment.

    Fragment selectedFragment = null;

    if (item.getItemId() == R.id.search_button) {
        selectedFragment = new Search_Fragment();
        getParentFragmentManager.beginTransaction()
            .replace(R.id.fragment_container, Search_Fragment.class, null) // fragment_container is the ID of the fragment holder
            .setReorderingAllowed(true)
            .commit();
    }