title: How to restore fragment states while using bottom navigation view in android?
body: <p>I am trying to do restoring the fragment view state values(text view,..etc) when back from second fragment in android bottom navigation view.</p>
  
  <p>MyActivity.java</p>
  
  <pre><code>public class LandingPage extends AppCompatActivity implements ProductFragment.AddToCart {
  
      Fragment fragment;
      TextView txtCart;
      Button btnCart;
      ItemData itemData;
      ArrayList&lt;ItemData&gt; itemDataList;
  
      private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener = new BottomNavigationView.OnNavigationItemSelectedListener() {
  
          @Override
          public boolean onNavigationItemSelected(@NonNull MenuItem item) {
  
              switch (item.getItemId()) {
                  case R.id.nav_home:
                      fragment = new ProductFragment();
                      loadFragment(fragment);
                      return true;
                  case R.id.nav_profile:
                      fragment = new ReportsFragment();
                      loadFragment(fragment);
                      return true;
                  case R.id.nav_logout:
                      LogOut();
                      return true;
              }
              return true;
          }
      };
   @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_landing_page);
  
          txtCart = findViewById(R.id.txt_cart);
          btnCart = findViewById(R.id.btn_cartImg);
  
          itemDataList = new ArrayList&lt;&gt;();
  
          BottomNavigationView bottomNavigationView = findViewById(R.id.navigation);
          bottomNavigationView.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
          loadFragment(new ProductFragment());
  }
   private void loadFragment(Fragment fragment) {
          FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
          transaction.replace(R.id.fragment_container, fragment);
          transaction.commit();
      }
  }
  </code></pre>
  
  <p>ProductFragment.java</p>
  
  <pre><code>public class ProductFragment extends Fragment {
  
  
      private OnFragmentInteractionListener mListener;
  
      Button btnCart1,btnCart2,btnCart3;
      AddToCart addToCart;
      TextView txtItem1Des,txtItem1Price,txtItem2Des,txtItem2Price,txtItem3Des,txtItem3Price;
      Button btnCart1Inc,btnCart1Dec,btnCart2Inc,btnCart2Dec,btnCart3Inc,btnCart3Dec;
      TextView cart1txt,cart2txt,cart3txt;
      String itemDes,itemPrice,itemImg,itemCount;
      int quantity1,quantity2,quantity3;
  
      public ProductFragment() {
  
      }
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
  
      }
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
                               Bundle savedInstanceState) {
          // Inflate the layout for this fragment
          View view = inflater.inflate(R.layout.fragment_product, container, false);
  
          btnCart1 = view.findViewById(R.id.cart1);
          txtItem1Des = view.findViewById(R.id.cart1_desc);
          txtItem1Price = view.findViewById(R.id.cart1_price);
          btnCart1Inc = view.findViewById(R.id.cart1Inc);
          btnCart1Dec = view.findViewById(R.id.cart1Dec);
          cart1txt = view.findViewById(R.id.cart1txt);
  
          btnCart2 = view.findViewById(R.id.cart2);
          txtItem2Des = view.findViewById(R.id.cart2_desc);
          txtItem2Price = view.findViewById(R.id.cart2_price);
          btnCart2Inc = view.findViewById(R.id.cart2Inc);
          btnCart2Dec = view.findViewById(R.id.cart2Dec);
          cart2txt = view.findViewById(R.id.cart2txt);
  
          btnCart3 = view.findViewById(R.id.cart3);
          txtItem3Des = view.findViewById(R.id.cart3_desc);
          txtItem3Price = view.findViewById(R.id.cart3_price);
          btnCart3Inc = view.findViewById(R.id.cart3Inc);
          btnCart3Dec = view.findViewById(R.id.cart3Dec);
          cart3txt = view.findViewById(R.id.cart3txt);
  
          quantity1 = Integer.parseInt(cart1txt.getText().toString());
          quantity2 = Integer.parseInt(cart2txt.getText().toString());
          quantity3 = Integer.parseInt(cart3txt.getText().toString());
  
          buttonInitialization();
          addingToCart();
  
          return view;
      }
  
      private void addingToCart() {
          btnCart1Dec.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                  if(quantity1 &lt;= 1){
                      btnCart1.setVisibility(View.VISIBLE);
                      btnCart1Dec.setVisibility(View.INVISIBLE);
                      btnCart1Inc.setVisibility(View.INVISIBLE);
                      cart1txt.setVisibility(View.INVISIBLE);
                      addToCart.removeItemFromCart(1);
                  }else {
                      quantity1 = quantity1-1;
                      cart1txt.setText(String.valueOf(quantity1));
                      addToCart.itemCount(cart1txt.getText().toString(),"1");
                  }
  
              }
          });
          btnCart1Inc.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                  quantity1 = quantity1+1;
                  cart1txt.setText(String.valueOf(quantity1));
                  addToCart.itemCount(cart1txt.getText().toString(),"1");
              }
          });
          btnCart2Dec.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                  if(quantity2 &lt;= 1){
                      btnCart2.setVisibility(View.VISIBLE);
                      btnCart2Dec.setVisibility(View.INVISIBLE);
                      btnCart2Inc.setVisibility(View.INVISIBLE);
                      cart2txt.setVisibility(View.INVISIBLE);
                      addToCart.removeItemFromCart(2);
                  }else {
                      quantity2 = quantity2-1;
                      cart2txt.setText(String.valueOf(quantity2));
                      addToCart.itemCount(cart2txt.getText().toString(),"2");
                  }
  
              }
          });
          btnCart2Inc.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                  quantity2 = quantity2+1;
                  cart2txt.setText(String.valueOf(quantity2));
                  addToCart.itemCount(cart2txt.getText().toString(),"2");
              }
          });
          btnCart3Dec.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                  if(quantity3 &lt;= 1){
                      btnCart3.setVisibility(View.VISIBLE);
                      btnCart3Dec.setVisibility(View.INVISIBLE);
                      btnCart3Inc.setVisibility(View.INVISIBLE);
                      cart3txt.setVisibility(View.INVISIBLE);
                      addToCart.removeItemFromCart(3);
                  }else {
                      quantity3 = quantity3-1;
                      cart3txt.setText(String.valueOf(quantity3));
                      addToCart.itemCount(cart3txt.getText().toString(),"3");
                  }
  
              }
          });
          btnCart3Inc.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                  quantity3 = quantity3+1;
                  cart3txt.setText(String.valueOf(quantity3));
                  addToCart.itemCount(cart3txt.getText().toString(),"3");
              }
          });
      }
  
  
      private void buttonInitialization() {
          btnCart1.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                      itemImg = "";
                      itemPrice = txtItem1Price.getText().toString().trim();
                      itemDes = txtItem1Des.getText().toString().trim();
                      itemCount = cart1txt.getText().toString().trim();
                      addToCart.cartCount("",itemPrice,itemDes,itemCount,"1");
                      btnCart1.setVisibility(View.INVISIBLE);
                      btnCart1Dec.setVisibility(View.VISIBLE);
                      btnCart1Inc.setVisibility(View.VISIBLE);
                      cart1txt.setVisibility(View.VISIBLE);
              }
          });
          btnCart2.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                  itemImg = "";
                  itemPrice = txtItem2Price.getText().toString().trim();
                  itemDes = txtItem2Des.getText().toString().trim();
                  itemCount = cart2txt.getText().toString().trim();
                  addToCart.cartCount("",itemPrice,itemDes,itemCount,"2");
                  btnCart2.setVisibility(View.INVISIBLE);
                  btnCart2Dec.setVisibility(View.VISIBLE);
                  btnCart2Inc.setVisibility(View.VISIBLE);
                  cart2txt.setVisibility(View.VISIBLE);
              }
          });
          btnCart3.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                  itemImg = "";
                  itemPrice = txtItem3Price.getText().toString().trim();
                  itemDes = txtItem3Des.getText().toString().trim();
                  itemCount = cart3txt.getText().toString().trim();
                  addToCart.cartCount("",itemPrice,itemDes,itemCount,"3");
                  btnCart3.setVisibility(View.INVISIBLE);
                  btnCart3Dec.setVisibility(View.VISIBLE);
                  btnCart3Inc.setVisibility(View.VISIBLE);
                  cart3txt.setVisibility(View.VISIBLE);
              }
          });
      }
  
      public void onButtonPressed(Uri uri) {
          if (mListener != null) {
              mListener.onFragmentInteraction(uri);
          }
      }
  
      @Override
      public void onAttach(Context context) {
          super.onAttach(context);
          addToCart = (AddToCart) context;
      }
  
  
      @Override
      public void onDetach() {
          super.onDetach();
          mListener = null;
      }
      public interface OnFragmentInteractionListener {
          void onFragmentInteraction(Uri uri);
      }
     public interface AddToCart{
          void cartCount(String item_img,String item_price,String item_desc,String item_count,String item_pos);
          void removeItemFromCart(int removeItem);
          void itemCount(String itemCount,String position);
      }
  
  }
  </code></pre>
  
  <p>Here in ProductFragment iam going to add values to text view. When i was coming from ReportFragment the view values are going to be cleared in ProductFragment.</p>