The difference between adding and replacing is that adding the previous fragment does not pause or stop. where, when we try to replace a fragment, the previous one goes through a pause cycle and stops. but when we push it onto the stack, the previous fragment is saved on top of the stack. And retrieved and loaded into events such as back-press.
So, if your solution requires your user interface to never stop or unload, you should just hide it and add a fragment on top of it. Or if its OK to unload the fragment and the user interface can be regenerated using some parameters. Thus, you can have some parameter in the fragment memory itself, which determines what statistics it should adhere to. By default, it will try to save a specific default state. But you can create your own object and attach it to the fragment.
As with onSaveInstanceState, it will be called when the action containing the fragment is closed. Events such as home tapping.
eg
public class MainActivityFragment1 extends Fragment { int alreadyCreated = 0; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { if (alreadyCreated == 1) {
source share