Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Fragment by Id in Activity

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);
        }
    }
}
like image 391
SiberianGuy Avatar asked May 05 '14 07:05

SiberianGuy


People also ask

How do you identify a fragment tag?

findFragmentById(R. id. fragment_container); Alternatively, you can assign a unique tag to a fragment and get a reference using findFragmentByTag() .

What is FragmentManager?

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.

How do I get the current fragment from supportFragmentManager?

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.

How do I attach a fragment to an activity?

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.


1 Answers

Use This:

 getSupportFragmentManager()
                        .findFragmentById(R.id.FileListFragment))
like image 127
Kishan Dhamat Avatar answered Sep 28 '22 16:09

Kishan Dhamat