A page in the project contains a ViewPager. The adapter is the implementation class of FragmentPagerAdapter. When the page is selected, it will request the data of the page.

private int currentPosition; //viewPager protected void onCreate(Bundle savedInstanceState) {... List<Fragment> fragmentList = new ArrayList();for(int i = 0; i< mData.size(); i++){ fragmentList.add(MyFragment.newInstance(i)); }... viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) { @Override public Fragment getItem(int i) {return fragmentList.get(i);
    	}
		@Override
		public int getCount() {
			returnfragmentList.size(); }}); viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int i, float} @override public void onPageSelected(int I) {public void onPageSelected(int I) { requestData(); } @Override public void onPageScrollStateChanged(int i) { } }); . } public void onRequestDataSuccess(Data successData){FragmentList.get (currentPosition).setData(successData); }Copy the code

There is no problem with the normal logic. However, when the application switches to the background and is killed due to insufficient system resources, the fragment displays blank when the icon or task list is clicked to return the APP, that is, setData is invalid. By printing logs during fragment creation and onCreateView of the Fragment method, you can find that the fragment printed in onCreateView is not in the created fragmentList when the system kills the app and creates it again. The fragment that is actually created in the viewpager is not in the fragmentList

Finally, a problem was found in the instantiateItem method of the FragmentPagerAdapter

public Object instantiateItem(@NonNull ViewGroup container, int position) {
    if(this.mCurTransaction == null) { this.mCurTransaction = this.mFragmentManager.beginTransaction(); } long itemId = this.getItemId(position); String name = makeFragmentName(container.getId(), itemId); Fragment fragment = this.mFragmentManager.findFragmentByTag(name); // This fragment is not null when the activity is rebuiltif(fragment ! = null) { this.mCurTransaction.attach(fragment); }else {
        fragment = this.getItem(position);
        this.mCurTransaction.add(container.getId(), fragment, makeFragmentName(container.getId(), itemId));
    }
    if(fragment ! = this.mCurrentPrimaryItem) { fragment.setMenuVisibility(false);
        fragment.setUserVisibleHint(false);
    }
    return fragment;
}
Copy the code

The makeFragmentName code is as follows

private static String makeFragmentName(int viewId, long id) {
    return "android:switcher:" + viewId + ":" + id;
}
Copy the code

When an activity is rebuilt, fragments that have been added are restored. When a viewpager instantiates the current fragment, not all fragments are returned via getItem(). Some fragments are returned directly from added fragments with viewId+index as the key. So when an activity is rebuilt, it will iterate through the creation of fragments to add to the fragmentList, whereas the fragments that are actually displayed in the viewpager are those that are returned directly from added fragments.

The solutions are as follows:

Add the makeFragmentName method to the activity

private String makeFragmentName(int viewId, long id) {
    return "android:switcher:" + viewId + ":" + id;
}
Copy the code

If a fragment with the makeFragmentName method as its key has been added, the fragment will be returned and added to the fragmentList

List<Fragment> fragmentList = new ArrayList();
for(int i = 0; i< mData.size(); i++){ Fragment fragmentByTag = getSupportFragmentManager().findFragmentByTag(makeFragmentName(R.id.view_pager, i));if(fragmentByTag ! = null){ fragmentList.add(fragmentByTag); }else{ fragmentList.add(MyFragment.newInstance(i)); }}Copy the code

One more note: Fragment creation must be passed through the setArguments method; otherwise, the parameters will be lost when the fragment is rebuilt

Reference:

Android background kill series www.jianshu.com/p/00fef8872…

Stackoverflow.com/questions/4…