title: Spinner value disappear after layout visibility is changed + specific flows
body: <p>I have a custom <code>list view</code> with layout contain two layouts, called upper and bottom.</p>
  
  <p>upper layout contain spinner, set and remove buttons.
  bottom layout contain <code>text view</code> and back button.</p>
  
  <p>By default bottom layout is in <code>GONE</code> state and when the user clicks on <code>set</code> button <code>upper</code> layout is <code>GONE</code> and bottom is <code>VISIBLE</code> (clicking on back button in bottom layout will return upper bottom back).</p>
  
  <p>my problem is spinner value is disappear after specific flows:</p>
  
  <p><strong>Flow 1:</strong></p>
  
  <ol>
  <li>Add two items</li>
  <li>Click on SET button on the first item</li>
  <li>Remove the second item</li>
  <li>Click on 'Back' button on the first item</li>
  </ol>
  
  <p><a href="https://i.stack.imgur.com/gibWl.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/gibWl.png" alt="Spinner value is disappear"></a></p>
  
  <p><strong>Flow 2:</strong></p>
  
  <ol>
  <li>Click on SET</li>
  <li>Rotate screen</li>
  <li>Click on Back</li>
  </ol>
  
  <p>Just to be clear, spinner values are exist and if drop it down you'll found the names (and I have <code>android:configChanges="keyboardHidden|orientation|screenSize"</code> in my manifest).</p>
  
  <p>So, why spinner value is disappear after those flows  ?</p>
  
  <p>Here is my code:</p>
  
  <p><strong>Names.java</strong></p>
  
  <pre><code>public class Names
  {
      private String name;
      private int nameIndex;
      private Boolean isNameOnTop;
  
  
      public Names()
      {
          name = "";
          isNameOnTop = true;
      }
  
      public String getName() {
          return name;
      }
  
      public void setName(String name) {
          this.name = name;
      }
  
      public int getNameIndex() {
          return nameIndex;
      }
  
      public void setNameIndex(int nameIndex) {
          this.nameIndex = nameIndex;
      }
  
      public Boolean getIsNameOnTop() {
          return isNameOnTop;
      }
  
      public void setIsNameOnTop(Boolean isNameOnTop) {
          this.isNameOnTop = isNameOnTop;
      }
  }
  </code></pre>
  
  <p><strong>MainActivity.Java</strong></p>
  
  <pre><code>public class MainActivity extends Activity
  {
      ArrayList&lt;Names&gt; namesArray = new ArrayList&lt;&gt;();
      ListView lvNames;
      ListviewAdapter adapter;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          lvNames = (ListView) findViewById(R.id.listView);
          adapter = new ListviewAdapter(this, namesArray);
          lvNames.setAdapter(adapter);
      }
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
          getMenuInflater().inflate(R.menu.menu_main, menu);
          return true;
      }
  
      @Override
      public boolean onOptionsItemSelected(MenuItem item) {
          int id = item.getItemId();
          if (id == R.id.action_add)
          {
              namesArray.add(new Names());
              adapter.notifyDataSetChanged();
              return true;
          }
          return super.onOptionsItemSelected(item);
      }
  }
  </code></pre>
  
  <p><strong>ListviewAdapter.java</strong></p>
  
  <pre><code>public class ListviewAdapter extends BaseAdapter
  {
      public Activity context;
      public LayoutInflater inflater;
      private ArrayList&lt;Names&gt; namesID;
      private boolean isDeleted;
  
      public ArrayList&lt;Names&gt; getNamesID() {
          return namesID;
      }
  
      public void setNamesID(ArrayList&lt;Names&gt; namesID) {
          this.namesID = namesID;
      }
  
      // Constructor
      public ListviewAdapter(Activity context, ArrayList&lt;Names&gt; names)
      {
          super();
          setNamesID(names);
  
          this.context = context;
          this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
      }
  
      @Override
      public int getCount() {
          return getNamesID().size();
      }
  
      @Override
      public Names getItem(int position) {
          return getNamesID().get(position);
      }
  
      @Override
      public long getItemId(int position) {
          return 0;
      }
  public class ViewHolder
      {
          RelativeLayout relativeLayout_Upper;
          RelativeLayout relativeLayout_Bottom;
  
          Spinner spNames;
          Button btn_set, btn_remove, btn_back;
          TextView tvChosen;
  
          int index;
      }
  
      @Override
      public View getView(final int i, View view, final ViewGroup viewGroup) {
          final ViewHolder holder;
          if (view == null) {
              holder = new ViewHolder();
              view = inflater.inflate(R.layout.listview_row, null);
  
              holder.relativeLayout_Upper = (RelativeLayout) view.findViewById(R.id.lvRow_upper_layout);
              holder.relativeLayout_Bottom = (RelativeLayout) view.findViewById(R.id.lvRow_bottom_layout);
              holder.spNames = (Spinner) view.findViewById(R.id.spNames);
              holder.btn_set = (Button) view.findViewById(R.id.btn_set);
              holder.btn_remove = (Button) view.findViewById(R.id.btn_remove);
              holder.btn_back = (Button) view.findViewById(R.id.btn_back);
              holder.tvChosen = (TextView) view.findViewById(R.id.tv_chosen);
              view.setTag(holder);
          }
          else
              holder = (ViewHolder) view.getTag();
          holder.index = i;
  
          if (isDeleted)
          {
              holder.spNames.setSelection(getItem(holder.index).getNameIndex());
              holder.tvChosen.setText("Chosen: " + getItem(holder.index).getName());
  
              if (getItem(holder.index).getIsNameOnTop())
              {
                  holder.relativeLayout_Upper.setVisibility(View.VISIBLE);
                  holder.relativeLayout_Bottom.setVisibility(View.GONE);
              }
              else
              {
                  holder.relativeLayout_Upper.setVisibility(View.GONE);
                  holder.relativeLayout_Bottom.setVisibility(View.VISIBLE);
              }
          }
  
          // pop spinner names
          String[] names = new String[]{"Tom", "Ben", "Gil", "Adam", "Moshe", "Adi", "Michael", "Yasmin", "Jessica", "Caroline", "Avi", "Yael"};
          final ArrayAdapter&lt;String&gt; spNamesAdapter = new ArrayAdapter&lt;String&gt;
                  (view.getContext(), android.R.layout.simple_spinner_dropdown_item, names);
          spNamesAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
          holder.spNames.setAdapter(spNamesAdapter);
  
          holder.spNames.setSelection(getItem(holder.index).getNameIndex());
          holder.spNames.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
              @Override
              public void onItemSelected(AdapterView&lt;?&gt; parent, View view, int position, long id) {
                  //holder.spNames.setTag(position);
                  getItem(holder.index).setNameIndex(position);
              }
  
              @Override
              public void onNothingSelected(AdapterView&lt;?&gt; parent) {
  
              }
          });
  
          holder.btn_set.setTag(i);
          holder.btn_set.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  getItem(holder.index).setName(holder.spNames.getSelectedItem().toString());
                  int position = (Integer) v.getTag();
                  holder.tvChosen.setText("Chosen: " + getItem(position).getName());
                  holder.relativeLayout_Upper.setVisibility(View.GONE);
                  holder.relativeLayout_Bottom.setVisibility(View.VISIBLE);
                  getItem(holder.index).setIsNameOnTop(false);
              }
          });
  
          // remove
          holder.btn_remove.setTag(i);
          holder.btn_remove.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  int position = (Integer) v.getTag();
                  namesID.remove(position);
                  notifyDataSetChanged();
                  isDeleted = true;
              }
          });
  
          // back
          holder.btn_back.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  holder.relativeLayout_Upper.setVisibility(View.VISIBLE);
                  holder.relativeLayout_Bottom.setVisibility(View.GONE);
                  getItem(holder.index).setIsNameOnTop(true);
              }
          });
  
          return view;
      }
  }
  </code></pre>
  
  <p><strong>activity_main.xml:</strong> contain <code>ListView</code> only.</p>
  
  <p><strong>upper_view.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:orientation="horizontal" android:layout_width="match_parent"
      android:layout_height="match_parent"&gt;
  
      &lt;Spinner
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:id="@+id/spNames" /&gt;
  
      &lt;Button
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="SET"
          android:id="@+id/btn_set" /&gt;
  
      &lt;Button
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="REMOVE"
          android:id="@+id/btn_remove" /&gt;
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p><strong>bottom_view.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:orientation="horizontal" android:layout_width="match_parent"
      android:layout_height="match_parent"&gt;
  
      &lt;TextView
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:textAppearance="?android:attr/textAppearanceLarge"
          android:text="Chosen:"
          android:id="@+id/tv_chosen"
          android:layout_marginRight="20dp" /&gt;
  
      &lt;Button
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="BACK"
          android:id="@+id/btn_back" /&gt;
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p><strong>listview_row.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:orientation="vertical"
      android:layout_width="match_parent"
      android:layout_height="match_parent"&gt;
  
      &lt;RelativeLayout
          android:orientation="vertical"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:id="@+id/lvRow_upper_layout"&gt;
  
          &lt;include
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              layout="@layout/upper_view"
              android:id="@+id/includeRow_register"
              android:layout_alignParentLeft="true"
              android:layout_marginLeft="0dp"
              android:layout_alignParentTop="true"
              android:layout_marginTop="0dp" /&gt;
      &lt;/RelativeLayout&gt;
  
      &lt;RelativeLayout
          android:orientation="vertical"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:id="@+id/lvRow_bottom_layout"
          android:visibility="gone"&gt;
  
          &lt;include
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              layout="@layout/bottom_view"
              android:id="@+id/includeRow_showData"
              android:layout_alignParentLeft="true"
              android:layout_marginLeft="0dp"
              android:layout_alignParentTop="true"
              android:layout_marginTop="0dp" /&gt;
      &lt;/RelativeLayout&gt;
  &lt;/LinearLayout&gt;
  </code></pre>