屏幕方向更改后无法恢复RecyclerView状态,因为保存的状态为null。为什么?

时间:2018-07-15 22:08:25

标签: android android-fragments android-recyclerview

我有一个Recyclerview,其中有一些数据放在一个片段中。如果更改屏幕方向,则要保存状态并在屏幕方向更改时重新加载状态。

这是我的Fragment类:

public class DataUsageFragment extends Fragment {
private static final String TAG = "DataUsage";
private static final boolean debug = true;
private RecyclerView recyclerView;
private Parcelable recyclerViewState;
private DataUsageManager dataUsageManager;
private RecyclerView.LayoutManager layoutManager;

public DataUsageFragment() {
}

public static DataUsageFragment getInstance(String title) {
    Log.d(TAG, "getInstance: called");
    DataUsageFragment fragment = new DataUsageFragment();
    Bundle args = new Bundle();
    args.putString("title", title);
    fragment.setArguments(args);
    return fragment;
}

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    if (debug) Log.d(TAG, "onCreateView is called");
    return inflater.inflate(R.layout.fragment_data_usage, container, false);
}

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    if (debug) Log.d(TAG, "onViewCreated is called");

    if (getArguments() != null) {
        String title = getArguments().getString("title");
        ((AppCompatActivity) getActivity()).getSupportActionBar().setTitle(title);
    }

    Context context = view.getContext();
    dataUsageManager = new DataUsageManager(getActivity(), 0);

    // RecyclerView
    recyclerView = view.findViewById(R.id.recycler_view_data);
    if (savedInstanceState == null) {
        Log.d(TAG, "onViewCreated: savedInstanceState in onViewCreated IS NULL!");
        layoutManager = new LinearLayoutManager(context);
        recyclerView.setLayoutManager(layoutManager);
    } else {
        if (recyclerViewState == null)
            Log.d(TAG, "onViewCreated: AAAAAAAAA");
        if (recyclerView.getLayoutManager() == null)
            Log.d(TAG, "onViewCreated: BBBBBBBBB");
        recyclerViewState = savedInstanceState.getParcelable("ListState");
        recyclerView.getLayoutManager().onRestoreInstanceState(recyclerViewState);
    }

    // Data Usage
    // int spinnerItemPos = spinner.getSelectedItemPosition();

    if (savedInstanceState == null) {
        Log.d(TAG, "onViewCreated: NULLNULLNULL");
        // if current API Level < 23 (Marshmallow), use TrafficStats, else NetworkStatsManager
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
            dataUsageManager.useTrafficStats();
        } else {
            dataUsageManager.useNetworkStatsManager();
        }
    } else {
        Log.d(TAG, "onViewCreated: QQQQQQQQ");
    }

}

@Override
public void onSaveInstanceState(@NonNull Bundle outState) {
    super.onSaveInstanceState(outState);
    Log.d(TAG, "onSaveInstanceState: called");
    recyclerViewState = recyclerView.getLayoutManager().onSaveInstanceState();
    outState.putParcelable("ListState", recyclerViewState);
}

}

在onViewCreated()中,检查null时包含一些Log消息。 看来recyclerViewStaterecyclerView.getLayoutmanager()都是空的,因为它打印“ AAAAAAAAA”和“ BBBBBBBBB”。我不明白为什么。我遵循了几个SO问题,做了同样的事情,它应该可以工作。

为什么recyclerViewStaterecyclerView.getLayoutmanager()都为空?
我在这里做什么错了?

1 个答案:

答案 0 :(得分:0)

对于以后遇到类似问题的任何人...

我通过将以下内容添加到Android清单文件中解决了该问题:

<application
        <activity
            ...
            android:configChanges="orientation|screenSize">
            ...
        </activity>
</application>

在处理配置更改时,保持活动/片段的状态似乎就足够了。