Android Studio call fragment method from other fragment or activity

时光总嘲笑我的痴心妄想 提交于 2020-01-07 08:31:52

问题


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) (getSupportFragmentManager().findFragmentById(R.id.tab3storage)).Storagerefresh();

but I have a NullPointerException error. How I can make this or a calling form a activity to the Fragment, because the calling from the Fragment to the Activity works already fine.

Code:

Main Activity:

public void refreshAll(){

    Tab3Storage tab3Storage = new Tab3Storage();
    tab3Storage.Storagerefresh();

    Tab4Gravel tab4Gravel = new Tab4Gravel();
    tab4Gravel.Gravelrefresh();
}

This is the Fragment Code:

totalMoney = loadData("totalMoney");
    totalMoneyDisplay.setText("$ " + totalMoney);

    totalGravel = loadData("totalGravel");
    totalGravelDisplay.setText(totalGravel + " Gravel");

    Storage_Level = loadData("storageLevel");
    if (Storage_Level == 0){
        Storage_Level = 1;
    }
    if(Storage_Level == 1){
        Storage_Capacity = Storage_Level1;
    }
    if(Storage_Level == 2){
        Storage_Capacity = Storage_Level2;
    }
    if(Storage_Level == 3){
        Storage_Capacity = Storage_Level3;
    }
    if(Storage_Level == 4){
        Storage_Capacity = Storage_Level4;
    }
    saveData("storageLevel", Storage_Level);

    Storage_Filled = totalGravel;
    storageCapacityDisplay.setText(Storage_Filled + "/" + Storage_Capacity);
    SellGravelButton.setText("Sell: $" + totalGravel);

    storageUpgradebtn.setText("Level " + Storage_Level + ":\n$" + Storage_Capacity / 2 );

load Data Code:

private long loadData(String name){

    SharedPreferences shared = this.getActivity().getPreferences(Context.MODE_PRIVATE);
    long value = shared.getLong(name, 0);
    return value;
}

回答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:

myFragment fragment = new myFragment();
fragment.Storagerefresh();



回答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();



回答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.



来源:https://stackoverflow.com/questions/44682624/android-studio-call-fragment-method-from-other-fragment-or-activity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!