从片段调用 ActivityMain 中的 AsyncTask 方法
Call an AsyncTask method in ActivityMain from fragment
之前我在主 activity 上有 AsyncTask 并调用了:
new SyncGetLocations(ActivityMain.this).execute();
现在,我将它移动到片段中,现在不知道如何从 MainActivity
中调用它。
AsyncTask 看起来很像:
private static class SyncGetLocations extends AsyncTask<Void, Void, Void> {
private WeakReference<ActivityMap> activityReference;
SyncGetLocations(ActivityMap context) {
activityReference = new WeakReference<>(context);
}
@Override
protected void onPreExecute() {
}
@Override
protected Void doInBackground(Void... voids) {
}
@Override
protected void onPostExecute(Void aVoid) {
}
}
任一
从 MainActivity 中找到您的片段:
val fragment = getSupportFragmentManager().findFragmentByTag(...) 或 findFragmentByID(...)
然后调用此片段中的方法来启动 asyncTask
fragent.callAsyncTask()
或
制作你的异步任务 public 或将其从片段中移除成为它自己的 class 并在你想要的任何地方调用它
我建议你让 asyntask 不是 activity 片段的内部 class 它你想从任何地方调用它
尝试使用类似
的方法将 Listener 附加到您的片段
public interface FragmentInteractionListener{
void onClick();
}
在你的片段中
private FragmentInteractionListener fragmentListener;
@Override
public void onDetach() {
super.onDetach();
fragmentListener = null;
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof FragmentInteractionListener) {
fragmentListener = (FragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement FragmentInteractionListener");
}
}
在 onClick 事件下这样调用
fragmentListener.onClick();
然后在您的 activity 中实现 FragmentInteractionListener
或
((SomeActivity)getActivity()).someMethod();
之前我在主 activity 上有 AsyncTask 并调用了:
new SyncGetLocations(ActivityMain.this).execute();
现在,我将它移动到片段中,现在不知道如何从 MainActivity
中调用它。
AsyncTask 看起来很像:
private static class SyncGetLocations extends AsyncTask<Void, Void, Void> {
private WeakReference<ActivityMap> activityReference;
SyncGetLocations(ActivityMap context) {
activityReference = new WeakReference<>(context);
}
@Override
protected void onPreExecute() {
}
@Override
protected Void doInBackground(Void... voids) {
}
@Override
protected void onPostExecute(Void aVoid) {
}
}
任一
从 MainActivity 中找到您的片段:
val fragment = getSupportFragmentManager().findFragmentByTag(...) 或 findFragmentByID(...)
然后调用此片段中的方法来启动 asyncTask
fragent.callAsyncTask()
或
制作你的异步任务 public 或将其从片段中移除成为它自己的 class 并在你想要的任何地方调用它
我建议你让 asyntask 不是 activity 片段的内部 class 它你想从任何地方调用它
尝试使用类似
的方法将 Listener 附加到您的片段public interface FragmentInteractionListener{
void onClick();
}
在你的片段中
private FragmentInteractionListener fragmentListener;
@Override
public void onDetach() {
super.onDetach();
fragmentListener = null;
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof FragmentInteractionListener) {
fragmentListener = (FragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement FragmentInteractionListener");
}
}
在 onClick 事件下这样调用
fragmentListener.onClick();
然后在您的 activity 中实现 FragmentInteractionListener
或
((SomeActivity)getActivity()).someMethod();