怎么在一个fragment or 任意类中操作另一个fragment中的方法

2025-05-01 18:01:49
推荐回答(1个)
回答1:

1 如果在fragment中要操作一个fragment,首要要得到这个对象,如何得到?使用getActivity中的FragmentMnager的getFragmentByTag,然后就可以使用这个fragment的对象来操作他的方法了。
2 如何在任意类中操作一个fragment,首先要得到环境参数,如何得到?

在 activity
中:

private static WeakReference actionButtonActivty = null;

actionButtonActivty = new WeakReference(this);

从activity中将这个actionButtonActivity对象传递到这个任意类中
asyncTask.setActivity(actionButtonActivty);

在 任意类
中:
private static WeakReference actionButtonActivty;

public void setActivity(
WeakReference actionButtonActivty) {
this.actionButtonActivty = actionButtonActivty;
}

/**
* this method is invoked on the UI thread after the background computation
* finishes. The result of the background computation is passed to this step
* as a parameter.
*/
@Override
protected void onPostExecute(Bitmap result) {
super.onPostExecute(result);

FragmentManager fm = actionButtonActivty.get().getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
BFragmentTab_one_event_details bt_det = (BFragmentTab_one_event_details) fm
.findFragmentByTag("2_det");
bt_det.setEvidenceImage(result);
bt_det.setButtonClickable();
ft.addToBackStack(null).commit();

}