Whenever I open my application for the first time, there are three fragments displayed correctly in 3 containers I have in my xml file. Upon clicking a tab on the action bar, one of the containers’ fragment is swapped for another.
The Problem
Whenever I open the app, swap to the second tab, use the back arrow to exit the app, then reopen the app and re-select the second tab, the fragment is blank. Nothing appears, and the same problem occurs with the third tab also. Why aren’t the second and third fragments being displayed?
I solved it by using a
public static boolean startedvalue in each of my fragments. In each fragment’s class, in theonCreateView()I setstarted = true;and in theonDestroyView()I setstarted = false;In the
onTabSelected()method of my main activity, I read thisbooleanvalue, and if it istrue, I attach the fragment back to its container, but if it isfalse, I create a new instance of the particular fragment and add it to the container.