Skip to content Skip to sidebar Skip to footer

Android Studio Call Fragment Method From Other Fragment Or Activity

New Question abaut this: Android Studio Refresh Error I want call a Method in a Fragment from another Fragment. I have already tried this: (Tab3Storage) (getSupportFragmentManage

Solution 1:

What your telling android with this getSupportFragmentManager().findFragmentById(R.id.tab3storage)) is to get the view of the fragment, not actually the code of it

To do that simply create the fragment and call the function:

myFragmentfragment=newmyFragment();
fragment.Storagerefresh();

Solution 2:

/**Use following Spinet of Code**/

Note: Below "activity_framelayout"is your Activity FrameLayout on which all your related Fragment is Attached. And Here i used "FrameLayout"
you can use any Layout either "RelativeLayout"or"LinearLayout" depends on your choice.

<FrameLayout
        android:id="@+id/activity_framelayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

FrameLayout activity_framelayout = (FrameLayout) findViewById(R.id.activity_framelayout);

Fragment fragment = new Target_Fragment();
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

if(activity_framelayout.getChildCount() > 0 &&   activity_framelayout.getChildAt(0) != null)
{
   activity_framelayout.removeAllViews();
}
fragmentTransaction.add(R.id.activity_framelayout, fragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();

Solution 3:

The right solution is described in android documentation. Communicating with Other Fragments. As per the docs, you can define an interface in the Fragment class and implement it within the Activity, then invoke the interface as per the event you desired, so that receiver side will capture that event.

Post a Comment for "Android Studio Call Fragment Method From Other Fragment Or Activity"