8

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);
        }
    }
}
0xC0DED00D
  • 19,522
  • 20
  • 117
  • 184
SiberianGuy
  • 24,674
  • 56
  • 152
  • 266
  • Go to this [http://stackoverflow.com/questions/8532462/how-to-get-the-fragment-instance-from-the-fragmentactivity?rq=1](http://stackoverflow.com/questions/8532462/how-to-get-the-fragment-instance-from-the-fragmentactivity?rq=1) – M D May 05 '14 at 07:24
  • Why you are using `var`in java? Javascript is another language :) Try like: `getSupportFragmentManager().findFragmentById`if you use support library. – Hardy May 05 '14 at 07:28
  • @Hardy This is Xamarin... not Javascript! – Blo May 05 '14 at 07:30
  • Can you try this : `FileListFragment fragment = (FileListFragment)SupportFragmentManager.FindFragmentById(Resource.Id.FileListFragment);` – Manish Mulimani May 08 '14 at 00:14
  • @ManishMulimani, tried it - doesn't help – SiberianGuy May 08 '14 at 13:25
  • Where are you trying to use `fileListFragment`? The scope of `fileListFragment` ends at the end of `onCreate` method. Try to manually debug or put some log statements to check whether `fileListFragment` is valid. – Manish Mulimani May 09 '14 at 01:28
  • Hi @Idsa do you see my updated answer? Does it help you? You should really try the last solution with `as FileListFragment;`, I think this is the missing part which occured the issue. Let me know. – Blo May 15 '14 at 08:15

7 Answers7

4

Use This:

 getSupportFragmentManager()
                        .findFragmentById(R.id.FileListFragment))
Kishan Dhamat
  • 3,746
  • 2
  • 26
  • 36
2

I'm not currently using Xamarin, however I undelete my answer because I think I'm near to the solution and this can help you. I found several tutorials or GitHub projects using Xamarin. These searches don't give me the right solution, but I think they give me the clue for your issue. Try this code:

using Android.App;
using Android.OS;
using Android.Support.V4.App;
// using Fragment = Android.Support.V4.App.Fragment;

namespace com.xamarin.recipes.filepicker
{
    [Activity(Label = "Choose file", MainLauncher = true, Icon = "@drawable/icon")]
    public class FilePickerActivity : FragmentActivity
    {
        // use Android.Support.V4.App.Fragment below
        private FileListFragment mFileListFragment; 

        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            SetContentView(Resource.Layout.ChooseAudio);

            mFileListFragment = (FileListFragment)SupportFragmentManager.FindFragmentById(Resource.Id.fileListFragment);
            // Avoid any confusion: change its id in your layout by "fileListFragment"
        }
    }
}   

I changed the var which seems undefined with Android.Support.V4.App.Fragment by using the fragment class FileListFragment as the error says: Unknown identifier.

Note: be sure that FileListFragment extends Android.Support.V4.App.Fragment as follows:

public class FileListFragment : Android.Support.V4.App.Fragment
{
  ...
}  

To use a var, I guess you need to convert it via as Android.Support.V4.App.Fragment (a comparator), like this:

var fileListFragment = SupportFragmentManager
                       .FindFragmentById(Resource.Id.fileListFragment)
                       as FileListFragment; // you need this line
Blo
  • 11,903
  • 5
  • 45
  • 99
1

Try this code. I'm not sure but I think it should works.

var fileListFragment = SupportFragmentManager.FindFragmentById<FileListFragment>(Resource.Id.FileListFragment);
erakitin
  • 11,437
  • 5
  • 44
  • 49
  • There is no typed parameter in FindFragmentById – SiberianGuy May 07 '14 at 14:53
  • @Idsa There is the typed parameter in 'FragmentManager' [documentation](http://docs.xamarin.com/guides/android/platform_features/fragments/part_2_-_managing_fragments/#1.communicating-with-fragments). I thought that 'SupportFragmentManager' has the same method. – erakitin May 07 '14 at 15:02
  • 1
    Seems like documentation is outdated – SiberianGuy May 08 '14 at 13:25
0

Did you tried to get fragment like this

 Fragment fileListFragment = SupportFragmentManager.findFragmentById(R.id.FileListFragment);
AndroidDev
  • 1,191
  • 3
  • 16
  • 35
0

You already know that the method you've tried won't work, so I am not going to tell say that once again. Please note that the view of the Fragment is not accessible like this. Try creating a project with tab navigation using ADT and then look inside the code, it will clarify your concepts. The wizard will also generate a dummy tab fragment class for you, which will look like

public static class FragHomeUI extends Fragment

The fragment view will be created and accessible inside following member of the class:

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState)
{
            View rootView = inflater.inflate(R.layout.fragment_home, container,
                    false);

Would say again, create a few sample projects through ADT wizard and observe the code carefully.

Naeem A. Malik
  • 995
  • 4
  • 19
  • Did you read question and comments before answering? This question is about [Xamarin](http://xamarin.com/android)! This is not the right way to create a fragment using this cross-plateform development. – Blo May 13 '14 at 21:29
0

It might Help.

FragmentManager fm = getSupportFragmentManager();
Fragment fragment_byID = fm.findFragmentById(R.id.fragment_id);
//OR
Fragment fragment_byTag = fm.findFragmentByTag("fragment_tag");
cracker
  • 4,900
  • 3
  • 23
  • 41
  • *Same as the others:* Did you read question and comments before answering? This question is about [Xamarin](http://xamarin.com/android)! This is not the right way to use this cross-plateform development. – Blo May 14 '14 at 04:46
  • It is also possible for the Activity to use the FragmentManager to find Fragments Like : var emailList = FragmentManager.FindFragmentById(Resource.Id.email_list_fragment); – cracker May 14 '14 at 04:51
  • what exactly you are not able to do? – cracker May 14 '14 at 05:12
0

Please try this code in Xamarin.Android 4.12.02001:

FileListFragment fileListFragment = Android.Runtime.Extensions.JavaCast<FileListFragment>(SupportFragmentManager.FindFragmentById(Resource.Id.id_file_list_fragment));

imknown
  • 68
  • 1
  • 11