Jay
Jay

Reputation: 5084

Calling Fragment's method from Activity doesn't work?

I followed this question and tried my hand at calling a method which is in my Fragment. I'm trying to call the method from an activity. However it's not recognizing the fragment's method. Here's my code:

XML:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/peoplefragment">

    <ListView
        android:id="@+id/searchpeople_list"
        android:layout_height="fill_parent"
        android:layout_width="match_parent"
        android:scrollbars="none"
        android:background="#fff">
    </ListView>

</RelativeLayout>

Fragment Code:

    public class SearchPeopleTab extends Fragment {

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
        {          
            View v = inflater.inflate(R.layout.activity_search_people_tab, container, false);
            View rootView = inflater.inflate(R.layout.activity_search_people_tab, container, false);
            return rootView;
        }

        public static void UpdateResults(String requestSearch)
        {
               new GetSearchResults(requestSearch).execute();
        }

class GetSearchResults extends AsyncTask<Void, Void, Void> {

        String requestSearch;

        GetSearchResults(String searchtext)
        {
            this.requestSearch = searchtext;
        }

        @Override
        protected Void doInBackground(Void... params) {
    }

Activity Code: (Calling the Fragment's method)

 private void PopulateResults() {

        FragmentManager manager = (FragmentManager) getSupportFragmentManager();
        Fragment fragment = manager.findFragmentById(R.id.peoplefragment);
        fragment.UpdateResults(requestSearch); //thats the method in the fragment. 

}

The 'UpdateResults()' part is underlined and the following message is given as an error:

Cannot resolve method UpdateResults()

Looks like it can't find the method. What am I doing wrong?

Upvotes: 1

Views: 1682

Answers (2)

Samrat Dutta
Samrat Dutta

Reputation: 1737

  1. Remove the keyword static from the method.

  2. And also, store the fragment in the SearchPeopleTab reference variable, that you created.

    You don't really need the line to store the FragmentManager, you can directly use getSupportFragmentManager();

    //FragmentManager fm = (FragmentManager) getSupportFragmentManager();
    SearchPeopleTab fragment = (SearchPeopleTab) getSupportFragmentManager().findFragmentById(R.id.peoplefragment);
    fragment.UpdateResults();
    

When static methods are used, they are called using the class name. When you want a method to be called on specific objects, the method should not be static.

Upvotes: 1

Andrew Fielden
Andrew Fielden

Reputation: 3899

You need to cast the Fragment to your defined class

private void PopulateResults() {

    FragmentManager manager = (FragmentManager) getSupportFragmentManager();
    SearchPeopleTab fragment = (SearchPeopleTab)manager.findFragmentById(R.id.peoplefragment);
    fragment.UpdateResults(); //thats the method in the fragment. 

}

Upvotes: 1

Related Questions