Finding Fragment View on Activity. Android findFragmentByTag(String tag) returns null

13,318

Solution 1

I have resolved the problem, it seems that based on the life cycle of both the activity and the fragment by the time the findFragmentById() is executed in the onCreate() the fragment has not been 'actually created', I tested this by adding a simple button to the layout and making a method that when clicked on the button the spinner will be filled with data.

But of course I don't want the user to click a 'Load' button to fill the spinner, instead I have implemented on my fragment the public void onActivityCreated() method.

For reference here is my code of the fragment:

 Fragment fr = new Fragment(){

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(layoutID, container, false);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        Spinner spinner = (Spinner) this.getView().findViewById(R.id.city_spinner);
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(context, R.array.city_list, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);
    }

 };

Solution 2

I had the same problem I solved it by calling the findFragmentByTag() in the

protected void onStart()
{
}

life cycle method.

Share:
13,318
DanyBoricua
Author by

DanyBoricua

Updated on June 04, 2022

Comments

  • DanyBoricua
    DanyBoricua almost 2 years

    I'm trying to get a Spinner View from an added Fragment to my activity but it seems that I cannot get the fragment nor the view.

    I have an Activity which adds a Fragment to it's LinearLayout, the fragment's layout is based on the 'extra' that comes from the intent of the Activity. Everything is displayed correctly and I can see all the views of the fragment but for some reason when I call findFragmentByTag(String tag) it returns null and thus I cannot call getView().

    Here is how I am adding the fragment code to my activity:

    ...onCreate(){
        ...
        FragmentManager fm = getFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
    
        Fragment fr = new Fragment(){
            @Override
            public View onCreateView(LayoutInflater inflater,
                    ViewGroup container, Bundle savedInstanceState) {
                return inflater.inflate(layoutID, container, false);
            }
        };
        ft.add(R.id.formsListActivity_Container, fr, "form_fragment_tag");
    
        ft.commit();
        ...
    

    }

    Then I try to get such fragment and get a the spinner view to add the string array:

    Fragment f = fm.findFragmentByTag("form_fragment_tag");
    Spinner spinner = (Spinner) f.getView().findViewById(R.id.city_spinner);
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, 
            R.array.city_list, android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(adapter);
    

    But I get a null exception because findFragmentByTag() returns null

    What am I doing wrong? I've verified the code a few times and the tags are the same. I've also make sure the spinner's id is in the XML layout file correctly, and the fragment is loading the correct XML layout because I can see it (If a comment the get view from fragment part).

    • JDJ
      JDJ almost 10 years
      I know this post is old, but if you place the Fragment in its own class, things will be much easier.
  • DanyBoricua
    DanyBoricua about 12 years
    I changed it, but still returns null, I read the docs for the method and that last boolean says is: attachToRoot - Whether the inflated hierarchy should be attached to the root parameter? If false, root is only used to create the correct subclass of LayoutParams for the root view in the XML. I really don't understand what it means.
  • Jacob Phillips
    Jacob Phillips about 12 years
    I'm guessing your container is null so the fragment is never added.
  • Andrew Kozak
    Andrew Kozak about 12 years
    Congrats on the fix! When you are able, please make sure to mark your answer as 'accepted' so that others may learn from your success. Cheers~
  • rmirabelle
    rmirabelle over 11 years
    Given so many lifecycle methods available to fragments, choosing the correct timing for various actions is very challenging. onActivityCreated seems to have worked for me as well