Hello,
Welcome to our Microsoft Q&A platform!
If you want to transfer fragment1 to fragment2, please use the following code, do not change code order. For example, current is fragment1, then I navigate to fragment2
public class Fragment1 : Fragment
{
public override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Create your fragment here
}
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View view= inflater.Inflate(Resource.Layout.layout1, container, false);
Button button1 = view.FindViewById<Button>(Resource.Id.button1);
button1.Click += Button1_Click;
return view;
}
private void Button1_Click(object sender, EventArgs e)
{
FragmentTransaction transaction = this.FragmentManager.BeginTransaction();
Fragment2 fragment = new Fragment2();
transaction.AddToBackStack(null);
transaction.Replace(Resource.Id.FramePage, fragment);
transaction.Commit();
}
}
In the fragment2, you can click the back button to back the fragment1, or click button execute this.FragmentManager.PopBackStack();
public class Fragment2 : Fragment
{
public override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Create your fragment here
}
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View view= inflater.Inflate(Resource.Layout.layout2, container, false);
Button popBtn = view.FindViewById<Button>(Resource.Id.popBtn);
popBtn.Click += PopBtn_Click;
return view;
}
private void PopBtn_Click(object sender, EventArgs e)
{
this.FragmentManager.PopBackStack();
}
}
Best Regards,
Leon Lu
If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.