1. A fragment is created. It does the work. There are views that are shown or disappear during actions in the fragment.
  2. I switch to another enemy - replaceFragment.
  3. I am returning back - replaceFragment. View that are shown or disappear during actions in a fragment cease to be displayed setVisibility (View.VISIBLE) Initialization of the view takes place in onCreateView (findViewById)

Hiding and showing the View takes place in the context of Retrofit network requests: before the execution of the request, the View is hidden, and depending on the result:

private void callRequestBilling() { if (callBilleng != null && callBilleng.isExecuted()) callBilleng.cancel(); Retrofit retrofit = new Retrofit.Builder() .baseUrl("http:") .addConverterFactory(GsonConverterFactory.create()) .build(); IRtfAPI service = retrofit.create(IRtfAPI.class); callBilleng = service.calcBilling("application/json",new DeclarationWrapper())); callBilleng.enqueue( new Callback<RowDataWrapper>() { @Override public void onResponse(Call<RowDataWrapper> call, Response<RowDataWrapper> response) { RowDataWrapper calculator = response.body(); if (calculator!=null) { Double dTotalSum = 0d; String strTotalSum = roundTotalSum(dTotalSum); tvCalcSum.setText(strTotalSum); frPreCalcSum1.setVisibility(View.VISIBLE); frPreCalcError.setVisibility(View.GONE); layoutResults.setVisibility(View.VISIBLE); layoutResults.postDelayed(new Runnable() { @Override public void run() { scrollView1.setHeightBottomLayout(layoutResults.getHeight()); } }, 100); tvResultCostDelivery.setText(strTotalSum); } else { frPreCalcSum1.setVisibility(View.GONE); layoutResults.setVisibility(View.GONE); } } @Override public void onFailure(Call<RowDataWrapper> call, Throwable t) { Log.d(TAG, "Calc error: " + t.getMessage()); } } ); } public void doCalcRequest() { try { if (validateForm()) { btnWarningPodSum.setVisibility(View.GONE); btnWarningCost.setVisibility(View.GONE); frPreCalcError.setVisibility(View.GONE); frPreCalcSum1.setVisibility(View.VISIBLE); callRequestBilling(); } else { frPreCalcError.setVisibility(View.VISIBLE); frPreCalcSum1.setVisibility(View.GONE); } } catch(Exception e) { Log.d(TAG, "doCalcRequest error: " + e.getMessage()); } } 

In all cases (before and after the re-creation of the fragment) the request is executed successfully, only the View is not shown.

The problem is solved by creating a fragment of a singleton, but - this is not correct, in my opinion.

    1 answer 1

    You need to store the fragment state somewhere so that it can be restored when it is recreated.

    For this:

    1. Put flags in the fragment for view visibility.
    2. Change their values ​​in the query process.
    3. Use these flags to change views.
    4. Save these values ​​in onSaveInstanseState .
    5. The saved values ​​will be at the re-creation of the fragment in the savedInstanseState argument in the onCeate and onCreateView - pull them out and set the required states to the views.
    • When the MainFragmentManager.replaceFragment () is called, the onSaveInstanceState for the replaced fragment is not executed. - Illya
    • @Illya, well ... Perhaps you need not to replace a new instance of a fragment, but to return to an existing one. By tag find his / his transaction and reset the stack of fragments to this fragment. - Yuriy SPb