title: Android change tab without losing data
body: <p>I'm working with tabs in android, according to my requirement I need to open a popup containing 5 tabs. That is, I have a Fragment where I have to open a DialogFragment and this DialogFragment need to show my 5 tabs. So far so quiet! Could enter each content on their respective tabs.
  But the problem is that when I change the tab, the values ​​that were entered in another tab are clean. example:
  I go into tab1 fills any value in a text field and then when I switch to tab2 tab1 back to the value it had before entered is lost.
  Given this scenario, how do I retain the values ​​that were filled to the brim change? Follow the code below to explain further.</p>
  
  <pre><code>import android.app.Activity;
  import android.content.Context;
  import android.os.Bundle;
  import android.support.v4.app.DialogFragment;
  import android.util.Log;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.View.OnClickListener;
  import android.view.ViewGroup;
  import android.widget.Button;
  import android.widget.Spinner;
  import android.widget.TabHost;
  import android.widget.TabHost.OnTabChangeListener;
  import android.widget.TabHost.TabSpec;
  import android.widget.TextView;
  import br.com.company.R;
  import br.com.company.beans.Ordem;
  import br.com.company.dispatcher.IDispacher;
  import br.com.company.fragment.helper.IFragmentCo;
  import br.com.company.bo.IClasseBO;
  
  public class TabsFragment extends DialogFragment implements OnTabChangeListener {
  
      private static final String TAG = "FragmentTabs";
      public static final String TAB_DADOS_CLIENTE = "dadosCliente";
      public static final String TAB_DEFEITO_FALHA = "defeitoFalha";
      public static final String TAB_SERVICOS = "servico";
      public static final String TAB_MATERIAIS = "material";
      public static final String TAB_OBSERVACAO = "observacao";
  
      private View mRoot;
      private Button enviar;
      private Button cancelar;
      private Spinner classe;
      private TabHost mTabHost;
  
      private Ordem ordem;
      private IClasseBO classeBO;
      private IDispacher dispatch;
      private IFragmentCo ifrag;
  
      private int mCurrentTab;
  
      public TabsFragment(IFragmentCo ifrag, Ordem ordem) {
          this.ordem = ordem;
          this.ifrag = ifrag;
      }
  
      @Override
      public void onAttach(Activity activity) {
          super.onAttach(activity);
      }
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  
          mRoot = inflater.inflate(R.layout.ordem_encerramento, null);
  
          enviar = (Button) mRoot.findViewById(R.ordem_encerramento.enviar);
  
          cancelar = (Button) mRoot.findViewById(R.ordem_encerramento.cancelar);
  
          cancelar.setOnClickListener(new OnClickListener() {
  
              @Override
              public void onClick(View v) {
                  TabsFragment.this.dismiss();
              }
          });
  
          mTabHost = (TabHost) mRoot.findViewById(android.R.id.tabhost);
          setupTabs();
          return mRoot;
      }
  
      @Override
      public void onActivityCreated(Bundle savedInstanceState) {
          super.onActivityCreated(savedInstanceState);
          setRetainInstance(true);
  
          mTabHost.setOnTabChangedListener(this);
          mTabHost.setCurrentTab(mCurrentTab);
  
          mTabHost.getTabContentView().addView(addView(R.layout.dados_cliente));
  
      }
  
      private void setupTabs() {
          mTabHost.setup(); // importante!
  
          mTabHost.addTab(newTab(TAB_DADOS_CLIENTE, R.string.tab_dados_cliente, R.ordem_encerramento.dados_cliente));
          mTabHost.addTab(newTab(TAB_DEFEITO_FALHA, R.string.tab_defeito_falha, R.ordem_encerramento.defeito_falha));
          mTabHost.addTab(newTab(TAB_SERVICOS, R.string.tab_servico, R.ordem_encerramento.servico));
          mTabHost.addTab(newTab(TAB_MATERIAIS, R.string.tab_material, R.ordem_encerramento.material));
          mTabHost.addTab(newTab(TAB_OBSERVACAO, R.string.tab_observacao, R.ordem_encerramento.observacao));
      }
  
      private View addView(int resource) {
          LayoutInflater layoutInflater = (LayoutInflater)getActivity().getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
          View view = layoutInflater.inflate(resource, null);
          return view;
      }
  
      private TabSpec newTab(String tag, int labelId, int tabContentId) {
          Log.d(TAG, "buildTab(): tag=" + tag);
  
          View view = LayoutInflater.from(mTabHost.getContext()).inflate(R.layout.tabs_bg_view, null);
          TextView tv = (TextView) view.findViewById(R.id.tabsText);
          tv.setText(labelId); 
  
          TabSpec tabSpec = mTabHost.newTabSpec(tag);
          tabSpec.setIndicator(view);
          tabSpec.setContent(tabContentId);
          return tabSpec;
      }
  
      @Override
      public void onResume() {
          super.onResume();
      }
  
      @Override
      public void onTabChanged(String tabId) {
          Log.d(TAG, "onTabChanged(): tabId=" + tabId);
          View view = null;
          if (TAB_DADOS_CLIENTE.equals(tabId)) {
              mTabHost.getTabContentView().removeAllViews();
              mTabHost.getTabContentView().addView(addView(R.layout.dados_cliente));
              mTabHost.setCurrentTab(0);
          } else if (TAB_DEFEITO_FALHA.equals(tabId)) {   
              view = new DefeitoFalhaView().createView(getActivity());
              mTabHost.getTabContentView().addView(view);
              mTabHost.setCurrentTab(1);
          } else if (TAB_SERVICOS.equals(tabId)) {
              mTabHost.getTabContentView().removeAllViews();
              mTabHost.getTabContentView().addView(addView(R.layout.servico));
              mCurrentTab = 2;
          } else if (TAB_MATERIAIS.equals(tabId)) {
              mTabHost.getTabContentView().removeAllViews();
              mTabHost.getTabContentView().addView(addView(R.layout.materiais));
              mCurrentTab = 3;
          } else if (TAB_OBSERVACAO.equals(tabId)) {
              mTabHost.getTabContentView().removeAllViews();
              mTabHost.getTabContentView().addView(addView(R.layout.observacao));
              mCurrentTab = 4;
          }
  
      }
  
      public View getmRoot() {
          return mRoot;
      }
  
  }
  </code></pre>
  
  <p>File xml.</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:background="@color/White"
      android:orientation="vertical" &gt;
  
      &lt;LinearLayout
          android:layout_width="fill_parent"
          android:layout_height="30dip"
          android:layout_gravity="center_vertical"
          android:orientation="horizontal"
          android:paddingRight="10dip" &gt;
  
          &lt;TextView
              android:layout_width="wrap_content"
              android:layout_height="fill_parent"
              android:gravity="center_vertical"
              android:paddingLeft="10dip"
              android:text="Devolver OS"
              android:textColor="@color/Black" /&gt;
  
          &lt;View
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_weight="1" /&gt;
  
          &lt;Button
              android:id="@+ordem_encerramento/cancelar"
              style="@style/ButtonNovo"
              android:layout_marginLeft="10dip"
              android:text="Cancelar" /&gt;
  
          &lt;Button
              android:id="@+ordem_encerramento/enviar"
              style="@style/ButtonNovo"
              android:layout_marginLeft="10dip"
              android:text="Enviar" /&gt;
      &lt;/LinearLayout&gt;
  
      &lt;View
          android:layout_width="fill_parent"
          android:layout_height="1dip"
          android:background="@xml/menu_bg" /&gt;
  
      &lt;ScrollView
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:layout_marginBottom="40dip" &gt;
  
          &lt;LinearLayout
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:orientation="vertical"
              android:padding="10dip" &gt;
  
              &lt;TabHost
                  xmlns:android="http://schemas.android.com/apk/res/android"
                  android:id="@android:id/tabhost"
                  android:layout_width="fill_parent"
                  android:layout_height="fill_parent"
                  android:background="#EFEFEF" &gt;
  
                  &lt;LinearLayout
                      android:layout_width="fill_parent"
                      android:layout_height="fill_parent"
                      android:orientation="vertical" &gt;
  
                      &lt;TabWidget
                          android:id="@android:id/tabs"
                          android:layout_width="fill_parent"
                          android:layout_height="wrap_content" /&gt;
  
                      &lt;FrameLayout
                          android:id="@android:id/tabcontent"
                          android:layout_width="fill_parent"
                          android:layout_height="fill_parent" &gt;
  
                          &lt;FrameLayout
                              android:id="@+ordem_encerramento/dados_cliente"
                              android:layout_width="fill_parent"
                              android:layout_height="fill_parent" /&gt;
  
                          &lt;FrameLayout
                              android:id="@+ordem_encerramento/defeito_falha"
                              android:layout_width="fill_parent"
                              android:layout_height="fill_parent" /&gt;
  
                          &lt;FrameLayout
                              android:id="@+ordem_encerramento/servico"
                              android:layout_width="fill_parent"
                              android:layout_height="fill_parent" /&gt;
  
                          &lt;FrameLayout
                              android:id="@+ordem_encerramento/material"
                              android:layout_width="fill_parent"
                              android:layout_height="fill_parent" /&gt;
  
                          &lt;FrameLayout
                              android:id="@+ordem_encerramento/observacao"
                              android:layout_width="fill_parent"
                              android:layout_height="fill_parent" /&gt;
                      &lt;/FrameLayout&gt;
                  &lt;/LinearLayout&gt;
              &lt;/TabHost&gt;
          &lt;/LinearLayout&gt;
      &lt;/ScrollView&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>