После нескольких экспериментов!
Вот лучшее решение, которое я нашел!
Всякий раз, когда Fragment перезагружается, вызывается функция onCreate, которая возвращает представление, поэтому вместо создания нового экземпляра просматривать каждый раз, возвращать предыдущий экземпляр!
public class AnyFragment extends Fragment {
//View of Fragment
private View viewObj=null;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
//insted of inflating new view it will check for the previous view as well
if(viewObj==null){
viewObj= inflater.inflate(R.layout.fragment_output_sequeleye, container, false);
}
return viewObj;
}
}
Также в деятельности вместо того, чтобы каждый раз создавать новый экземпляр, используйте только один экземпляр, созданный изначально, как показано ниже!
public class AnyActivity extends AppCompatActivity {
//instance created
AnyFragment anyFrag;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
//initializing the intsance
anyFrag=new AnyFragment()
//using the privious ariable again insted of making new instance
loadFragment(anyFrag,R.id.CONTAINERID);
}
//Your function which loads the fragment
public void loadFragment(Fragment targetFragment, int containerId) {
supportFragmentManager.beginTransaction().replace(containerId, targetFragment, targetFragment.getClass().getName()).commit();
}
}