How to Open a New Fragment from Another Fragment

How do I open a new fragment from another fragment?

Add following code in your click listener function,

NextFragment nextFrag= new NextFragment();
getActivity().getSupportFragmentManager().beginTransaction()
.replace(R.id.Layout_container, nextFrag, "findThisFragment")
.addToBackStack(null)
.commit();

The string "findThisFragment" can be used to find the fragment later, if you need.

Trying to Open fragment from another fragment in android studio 3.5.3

As already suggested, use getSupportFragmentManager() AND make sure that your UProfileFragment inherits from androidx.fragment.app.Fragment in case you use AndroidX or android.support.v4.app.Fragment in case you don't.

Your Activity should inherit from AppCompatActivity

How to open a fragment from another fragment using the Android Navigation Drawer?

NEW ANSWER:

Using the navigation drawer, it seems the fragment transactions happen under the NavHostFragment and its FragmentTransactionManager.
This forces us to get its childFragmentManager() and use it for checking the backStackEntryCount.

Therefore just adding the nested fragment to the parent backstack is not enough. We would need to override onBackPressed and onSupportNavigateUp and take into account the backstack of the NavHostFragment when going back and poping it.

    private fun handleNestedFragmentsBackStack(): Boolean {
val navHostChildFragmentManager = supportFragmentManager
.findFragmentById(R.id.nav_host_fragment_content_main)?.childFragmentManager

return if (navHostChildFragmentManager?.backStackEntryCount!! > 1) {
navHostChildFragmentManager.popBackStack()
false
} else {
val navController = findNavController(R.id.nav_host_fragment_content_main)
navController.navigateUp(appBarConfiguration) || super.onSupportNavigateUp()
}
}

override fun onBackPressed() {
handleNestedFragmentsBackStack()
}

override fun onSupportNavigateUp(): Boolean {
return handleNestedFragmentsBackStack()
}

Now when adding your second fragment to the parent's backstack, remember that the parent isn't the Activity, but the NavHostFragment.
You can proceed by doing the fragment transaction as suggested bellow in the initial answer.

INITIAL ANSWER:

Should work for standard fragment transactions

Adding your fragment to the backstack should solve your issue

    FragmentTransaction ft = getParentFragmentManager().beginTransaction()

ft.replace(R.id.nav_host_fragment_content_main, new MySecondFragment(), null)
ft.addToBackStack(MySecondFragment.class.getName()) // you can use a string here, using the class name is just convenient
ft.commit();

When pressing the back button, you will navigate through the back stack.

The same in Kotlin code:

    parentFragmentManager.commit {
replace(R.id.nav_host_fragment_content_main, MySecondFragment())
addToBackStack(MySecondFragment::class.java.name)
}

Opening fragment from another fragment

The FragmentTransaction.replace() method you're calling inside your listener removes all fragments, from a given container, before replacing them with the provided fragment, adding the new fragment's root View to the layout. In this particular case, the Activity's root view is a ScrollView which already has a LinearLayout child. This LinearLayout is, however, not a different fragment's layout, and as such is not removed when calling FragmentTransaction.replace(). As a result, when adding the new fragment's layout to the container view, you're effectively calling ScrollView.addView() on a ScrollView which already has a direct child. I can see two ways of solving your exception (which one fits better with your design/layout is for you to decide):

  1. Have the ProfileActivity layout be just the ScrollView (essentially delete the LinearLayout under it). This is not really ideal, since it adds a ScrollView (your fragment's root View) child to another ScrollView, making either one redundant. You should either change the fragment's root View not to be a Scrollview, or
  2. Change the ProfileActivity's root View not to be a ScrollView.

How to open a Fragment on button click from a fragment in Android

You can replace the fragment using FragmentTransaction on button click. Something like this:

  Fragment someFragment = new SomeFragment(); 
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_container, someFragment ); // give your fragment container id in first parameter
transaction.addToBackStack(null); // if written, this transaction will be added to backstack
transaction.commit();

Learn about performing fragment transactions here.

code:

  package com.rupomkhondaker.sonalibank;

import android.app.Fragment;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

public class HomeFragment extends Fragment implements View.OnClickListener {

public HomeFragment() {
}

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

View rootView = inflater.inflate(R.layout.fragment_home, container, false);

Button aboutBtn = (Button) rootView.findViewById(R.id.aboutusButton);
Button phonebookBtn = (Button) rootView.findViewById(R.id.phbookButton);

aboutBtn.setOnClickListener(this);
phonebookBtn.setOnClickListener(this);


return rootView;
}

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

case R.id.phbookButton:
fragment = new PhoneBookFragment();
replaceFragment(fragment);
break;
}
}

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


}

How to open a fragment from another fragment using binding?

Try this code plz:

FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.home_layout, new ChatFragment(), "second fragment"); //My second Fragment
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();

Open fragment from inside another fragment

Add a FrameLayout to your preferred activity's layout to call FragmentA (the fragment to be opened onClick):

<FrameLayout
android:layout_width="match_parent"
android:id="@+id/outer_frame"
android:layout_height="wrap_content">
</FrameLayout>

and then replace outer_frame (FrameLayout) with your FragmentA by doing this:

mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String topic = String.valueOf(parent.getItemAtPosition(position));

Log.d("Comunidad",topic);
getSupportFragmentManager().beginTransaction()
.replace(R.id.outer_frame, new FragmentA())
.commit();

}
});


Related Topics



Leave a reply



Submit