I have the following layout:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
class="com.xamarin.recipes.filepicker.FileListFragment"
android:id="@+id/FileListFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</FrameLayout>
After that I am trying to get access to FileListFragment in Activity:
var fileListFragment = SupportFragmentManager.FindFragmentById(Resource.Id.FileListFragment);
but I get "Unknown identifier: fileListFragment" in Watches for fileListFragment
Update.
Activity code:
using Android.Support.V4.App;
using Android.OS;
namespace com.xamarin.recipes.filepicker
{
[Android.App.Activity(Label = "Choose file", MainLauncher = true, Icon = "@drawable/icon")]
public class FilePickerActivity : FragmentActivity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.ChooseAudio);
var fileListFragment = (FileListFragment)SupportFragmentManager.FindFragmentById(Resource.Id.FileListFragment);
}
}
}
findFragmentById(R. id. fragment_container); Alternatively, you can assign a unique tag to a fragment and get a reference using findFragmentByTag() .
Android FragmentManager A FragmentManager manages Fragments in Android, specifically it handles transactions between fragments. A transaction is a way to add, replace, or remove fragments.
To get the current fragment that's active in your Android Activity class, you need to use the supportFragmentManager object. The supportFragmentManager has findFragmentById() and findFragmentByTag() methods that you can use to get a fragment instance.
Add a fragment to an activity You can add your fragment to the activity's view hierarchy either by defining the fragment in your activity's layout file or by defining a fragment container in your activity's layout file and then programmatically adding the fragment from within your activity.
Use This:
getSupportFragmentManager()
.findFragmentById(R.id.FileListFragment))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With