Search code examples
androidandroid-fragmentsactionbarsherlock

Call a method in a drawer fragment from the base activity


I need to call method methodX from fragmentY, which is a drawer from action bar sherlock which means it is not called dynamically. and methodX is called from activityZ.

i set the tag on fragmentY like this:

private static final String TAG = "fragmentY";

and i called methodX on activityZ like this:

fragmentY fragment = (fragmentY ) getSupportFragmentManager().findFragmentByTag("fragmentY");
fragment.methodX(arg1, R.drawable.ic_launcher);

i also set the fragment on the id like this:

android:id="@+id/fragYid"

then i called methodX on activityZ like this:

fragmentY fragment = (fragmentY ) getSupportFragmentManager().findFragmentById("fragYid");
fragment.methodX(arg1, R.drawable.ic_launcher);

but both returns a null pointer exception. so i guess it doesn't see the fragment? but it compiles so i'm really confused right now.


Solution

  • Found out how:

    Key is to declare the fragment globally and instantiate it in onCreate.