我在后台堆上获得NPE。为了解决OOM问题,我读到我需要取消Fragment
内的所有引用,尤其是Bitmaps
。所以我所做的是:当Fragment
被替换时,我将每个变量设置为null,除了Adapter
RecyclerView
,因为我需要再次使用该内容。
@Override
public void onDestroyView() {
super.onDestroyView();
annihilateView();
}
onCreateView
:
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_lobi, container, false);
getActivity().getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
utilities = new Utilities(BottomLobiFragment.this.getActivity());
((NewsfeedActivity)getActivity()).hideBottomToolbarMarket();
setHasOptionsMenu(true);
userIdString = sharedPreferencesList.userIDString;
userAliasString = sharedPreferencesList.userAliasString;
loginSharedPrefsString = sharedPreferencesList.loginSharedPreference;
// mPrefs = getContext().getSharedPreferences(loginSharedPrefsString, getContext().MODE_PRIVATE);
userId = sharedPreferenceUtilities.getValue(BottomLobiFragment.this.getActivity(), loginSharedPrefsString, userIdString);
userAlias = sharedPreferenceUtilities.getValue(BottomLobiFragment.this.getActivity(), loginSharedPrefsString, userAliasString);
lobiProgressBar = (ProgressBar)rootView.findViewById(R.id.fragment_lobi_progressBar);
lobiProgressBar.getIndeterminateDrawable().setColorFilter(
getResources().getColor(R.color.miBlue),
android.graphics.PorterDuff.Mode.SRC_IN);
noPostTV = (OpenSansFont)rootView.findViewById(R.id.fragment_lobi_noPostTextView);
recyclerView = (RecyclerView)rootView.findViewById(R.id.fragment_lobi_recyclerView);
handler = new Handler();
manager = new LinearLayoutManager(BottomLobiFragment.this.getActivity(),
LinearLayoutManager.VERTICAL,
false);
recyclerView.setLayoutManager(manager);
// recyclerView.getRecycledViewPool().setMaxRecycledViews(VIEW_TYPE,0);
swipeRefreshLayout = (SwipeRefreshLayout)rootView.findViewById(R.id.fragment_lobi_swipe_container);
swipeRefreshLayout.setOnRefreshListener(this);
width = displayMetrics.widthPixels;
height = displayMetrics.heightPixels;
upperToolbarInit();
if(getArguments() != null){
Parcelable savedRecyclerLayoutState = getArguments().getParcelable(utilities.BUNDLE_RECYCLER_LAYOUT);
if(savedRecyclerLayoutState == null){
Gson gson = new Gson();
String json = sharedPreferenceUtilities.getValue(BottomLobiFragment.this.getActivity(), sharedPreferencesList.loginSharedPreference, utilities.lobbyPostListKey);
//(utilities.lobbyPostListKey, "");
PostListSharedPrefs postListApiFrmJson = gson.fromJson(json, PostListSharedPrefs.class);
if(postListApiFrmJson != null){
lobiAdapter = new LobiAdapter();
lobiAdapter.setDisplay(width, height);
recyclerView.setAdapter(lobiAdapter);
recyclerView.setLayoutManager(manager);
lobiAdapter.addList(postListApiFrmJson.posts);
counter += postListApiFrmJson.posts.size();
setIsLoaded();
setBlankLoaded();
} else {
refreshView();
}
} else {
manager.onRestoreInstanceState(savedRecyclerLayoutState);
recyclerView.setAdapter(lobiAdapter);
recyclerView.setLayoutManager(manager);
lobiProgressBar.setVisibility(View.GONE);
}
} else if(getArguments() == null){
refreshView();
}
问题是,每次用户根据Fragment Lifecycle进行backstack时都应该再次调用onCreateView
,是吗?因为我已经引用了onCreateView
中的所有内容并且它会抛出NPE,但如果我没有在onDestroyView
中将所有内容都设置为null,那么它可以正常工作......或者我只是在阅读生命周期错误?