app/src/main/java/me/zhanghai/android/douya/network/RequestFragment.java

(开头部分) 5KB

这里只显示每个文件的开头 60 行。登录后可以解锁完整代码。

/*
 * Copyright (c) 2015 Zhang Hai <Dreaming.in.Code.ZH@Gmail.com>
 * All Rights Reserved.
 */

package me.zhanghai.android.douya.network;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.util.Log;

import com.android.volley.Response;
import com.android.volley.VolleyError;

import me.zhanghai.android.douya.app.TargetedRetainedFragment;
import me.zhanghai.android.douya.util.FragmentUtils;

/**
 * An one-shot Fragment for performing a {@link Request} safely across Activity re-creation.
 *
 * <p>Response will only be delivered when this Fragment is in resumed state. This Fragment will be
 * automatically removed once response delivery is done.</p>
 *
 * <p>Response is not guaranteed to be delivered if this fragment (and its hosting activity) gets
 * destroyed, because it is impossible to continue tracking the request in this case. The recreated
 * instance of this fragment will then remove itself.</p>
 *
 * @param <T> The type of parsed response the request expects.
 */
public class RequestFragment<T, S> extends TargetedRetainedFragment {

    private static final String TAG = RequestFragment.class.getName();

    private Request<T> mRequest;
    private S mRequestState;

    public static <T, S> void startRequest(Request<T> request, S requestState,
                                           FragmentActivity targetActivity, int requestCode) {
        RequestFragment<T, S> fragment = new RequestFragment<>(request, requestState);
        fragment.targetAtActivity(requestCode);
        fragment.startRequest();
        FragmentUtils.add(fragment, targetActivity);
    }

    public static <T, S> void startRequest(Request<T> request, S requestState,
                                           FragmentActivity targetActivity) {
        startRequest(request, requestState, targetActivity, REQUEST_CODE_INVALID);
    }

    public static <T, S> void startRequest(Request<T> request, S requestState,
                                           Fragment targetFragment, int requestCode) {
        RequestFragment<T, S> fragment = new RequestFragment<>(request, requestState);
        fragment.targetAtFragment(targetFragment, requestCode);
        FragmentActivity activity = targetFragment.getActivity();
        fragment.startRequest();
        FragmentUtils.add(fragment, activity);
    }

后面还有 93 行代码,解锁后查看完整代码

24 小时内免费解锁 3 个项目,之后 1 积分/个。 规则说明

AI 解读

登录后可用,每次 10 积分,解读结果公开显示在下面。

还没有人解读过这个文件。