silverFoxA
silverFoxA

Reputation: 4669

How to access a method in fragment class from parent Activity?

I am implementing a database as I need to prepare the list or update it everytime I make some changes. I have a fragment on top of mainActivity and I can't perform the operations within the MainActivity so some of the operations are to be done in MainActivity and others in the fragment class. So in the fragment class which extends fragment has a method called preparelist(), which updates the database and populate the data. In my mainactivity I am performing a delete operations using the default overflow menu item, but here I need to call the preparelist() method in order to display the performed operations or the app has to be closed in order to diplay the operation that has been performed

I have tried the following code which is on the web

MyFragment fragment= (MyFragment)getSupportFragmentManager().findFragmentById(R.id.frag);
( (MyFragment)fragment).prepareList();

but this shows error

java.lang.NullPointerException: Attempt to invoke virtual method...on a null object reference

Basically what I want to know is how do I call the preparelist() method within my MainActivity without making any other abstract class or anything like that

Upvotes: 0

Views: 1475

Answers (2)

user4774371
user4774371

Reputation:

Instead, of finding fragment by ID, find it by TAG, for that use need two changes. See below code

FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.id_of_frame_layout_in_xml, myFragment ,"ADD_A_TAG_NAME_HERE");
        fragmentTransaction.commit();

Replace ADD_A_TAG_NAME_HERE with the TAG name and id_of_frame_layout_in_xml will be the framelayout in xml where you will add/replace your fragment

Now you can find fragment by TAG

MyFragment fragment= (MyFragment)getSupportFragmentManager().findFragmentByTAG("ADD_A_TAG_NAME_HERE");

One good way of creating TAG is as follows

public class MyFragment extends Fragment {

public static final String TAG = MyFragment.class.getSimpleName();
...

}

So your statement will be like:

getSupportFragmentManager().beginTransaction();
            fragmentTransaction.replace(R.id.id_of_frame_layout_in_xml, myFragment, MyFragment.TAG);

and

MyFragment fragment= (MyFragment)getSupportFragmentManager().findFragmentByTAG(MyFragment.TAG);

Upvotes: 0

Sufian
Sufian

Reputation: 6555

Solution 1

Call public method of your Fragment from your Activity:

MyFragment fragment= (MyFragment)getSupportFragmentManager().findFragmentById(R.id.frag);
if(fragment != null)
    ( (MyFragment)fragment).prepareList();
else
    Toast.makeText(this, "fragment is null", Toast.LENGTH_SHORT).show();

Solution 2

Move the functionality of delete to your fragment:

  1. move your delete operations in your fragment,
  2. in your fragment, write:

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        setHasOptionsMenu(true);        //this fragment can now override
                                    //options menu
    }
    
  3. now do the same as you do in your activity, overriding the onCreateOptionsMenu(), onOptionsItemSelected etc. Please note that the method signatures differ to that of Activity's. See Fragment documentation.

Upvotes: 1

Related Questions