OrientationChange 处理 Activity、Fragment、AsyncTask 和 DialogFragments?

OrientationChange handling Activity, Fragment, AsyncTask and DialogFragments?

您好,我正在考虑一起处理 ActivityFragmentAsyncTaskDialogFragments 的正确和最佳方法。

关于我认为它如何工作的简要概述:

不幸的是,这不是它的工作方式,在方向更改时,我的 DialogFragment 一直显示并且没有显示 toast。 我究竟做错了什么 ?

public class BaseFragment extends Fragment{
private static final String TAG = BaseFragment.class.getSimpleName();

protected WeakReference<AppCompatActivity> mActivity;
private TemplateDialogFragment dialogFragment;

public WeakReference<AppCompatActivity> getAppCompatActivity(){ return mActivity; }

@Override
public void onAttach(Context context) {
    if(!(context instanceof AppCompatActivity)) {
        throw new IllegalStateException(TAG + " is not attached to an AppCompatActivity.");
    }

    mActivity = new WeakReference<>((AppCompatActivity) context);

    super.onAttach(context);
}

@Override
public void onDetach() {
    mActivity = null;
    super.onDetach();
}

@Override
public void onStart() {
    super.onStart();
    showContent();
}

public void showContent(){

}

public void showDialog(String title, String content){
    dialogFragment = new TemplateDialogFragment();

    Bundle bundle = new Bundle();
    bundle.putString(TemplateDialogFragment.DIALOG_TITLE, title);
    bundle.putString(TemplateDialogFragment.DIALOG_MESSAGE, content);

    dialogFragment.setArguments(bundle);
    dialogFragment.show(getFragmentManager(), TemplateDialogFragment.FRAGMENT_TAG);
}

public void notifyTaskFinished(String result) {
    dismissDialog();

    if(mActivity != null && !mActivity.get().isFinishing()) {
        Toast.makeText(mActivity.get().getApplicationContext(), result, Toast.LENGTH_LONG).show();
    }
}

private void dismissDialog(){
    if(dialogFragment != null && dialogFragment.isAdded()) {
        dialogFragment.dismissAllowingStateLoss();
    }
}

}

...

public class TemplateFragment extends BaseFragment {
private static final String TAG = TemplateFragment.class.getSimpleName();

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    return inflater.inflate(R.layout.test_fragment, container, false);
}
@Override
public void showContent() {
    super.showContent();

    Button startTask = (Button) getAppCompatActivity().get().findViewById(R.id.button0);
    final BaseFragment instance = this;

    startTask.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            CustomAsyncTask task = new CustomAsyncTask(instance);
            EditText input = (EditText) getAppCompatActivity().get().findViewById(R.id.text0);
            task.execute(input.getText().toString());
        }
    });
}

private static class CustomAsyncTask extends AsyncTask<String, Void, String> {
    WeakReference<BaseFragment> weakBaseFragmentReference;

    private CustomAsyncTask(BaseFragment fragment) {
        weakBaseFragmentReference = new WeakReference<>(fragment);
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        weakBaseFragmentReference.get().showDialog("Executing", "Working on the request...");
    }

    @Override
    protected String doInBackground(String... params) {
        HttpURLConnection con = HttpUrlConnectionFactory.createUrlConnection("https://www.httpbin.org/bytes/" + (params[0] == null ? "1" : params[0]));
        return HttpRequester.doGet(con).getResponseAsString();
    }

    @Override
    protected void onPostExecute(String response) {
        super.onPostExecute(response);
        if(weakBaseFragmentReference.get() == null) {
            return;
        }
        weakBaseFragmentReference.get().notifyTaskFinished(response);
    }
}

}

*编辑:

经过一段时间研究这个主题后,我确信 Service 是我大部分使用领域的最佳解决方案。另外我用了很多AsyncTaskLoaders,因为有lifecycle的平滑控制....

使用进度条代替 DialogFragment。

A​​syncTask 应该只用于需要几秒钟的任务。

A​​syncTask 不遵守 Activity 生命周期,可能导致内存泄漏。

检查一些 gotchas

您可以尝试使用 AsyncTaskLoader 来避免配置更改。