0

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>
a_local_nobody
  • 7,947
  • 5
  • 29
  • 51
Vasant Raval
  • 257
  • 1
  • 12
  • 31

1 Answers1

1

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();
}
Ananta Raha
  • 1,011
  • 5
  • 14
  • getParentFragmentManager is giving me error – Vasant Raval Jun 10 '21 at 08:11
  • can you tell me a simple way to do it like creating a new intent or something like this and also I want to add a backstack – Vasant Raval Jun 10 '21 at 08:16
  • If your want to place Search_Fragment inside HomeFragment, then use getChildFragmentManager() instead of getParentFragmentManager(). For adding into backstack, use .addToBackStack(null) before .commit() – Ananta Raha Jun 10 '21 at 08:24