Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8785139
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T21:16:37+00:00 2026-06-13T21:16:37+00:00

I’m working with tabs in android, according to my requirement I need to open

  • 0

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.

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;
    }

}

File xml.

<?xml version="1.0" encoding="utf-8"?>
<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" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="30dip"
        android:layout_gravity="center_vertical"
        android:orientation="horizontal"
        android:paddingRight="10dip" >

        <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" />

        <View
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1" />

        <Button
            android:id="@+ordem_encerramento/cancelar"
            style="@style/ButtonNovo"
            android:layout_marginLeft="10dip"
            android:text="Cancelar" />

        <Button
            android:id="@+ordem_encerramento/enviar"
            style="@style/ButtonNovo"
            android:layout_marginLeft="10dip"
            android:text="Enviar" />
    </LinearLayout>

    <View
        android:layout_width="fill_parent"
        android:layout_height="1dip"
        android:background="@xml/menu_bg" />

    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_marginBottom="40dip" >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical"
            android:padding="10dip" >

            <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" >

                <LinearLayout
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:orientation="vertical" >

                    <TabWidget
                        android:id="@android:id/tabs"
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content" />

                    <FrameLayout
                        android:id="@android:id/tabcontent"
                        android:layout_width="fill_parent"
                        android:layout_height="fill_parent" >

                        <FrameLayout
                            android:id="@+ordem_encerramento/dados_cliente"
                            android:layout_width="fill_parent"
                            android:layout_height="fill_parent" />

                        <FrameLayout
                            android:id="@+ordem_encerramento/defeito_falha"
                            android:layout_width="fill_parent"
                            android:layout_height="fill_parent" />

                        <FrameLayout
                            android:id="@+ordem_encerramento/servico"
                            android:layout_width="fill_parent"
                            android:layout_height="fill_parent" />

                        <FrameLayout
                            android:id="@+ordem_encerramento/material"
                            android:layout_width="fill_parent"
                            android:layout_height="fill_parent" />

                        <FrameLayout
                            android:id="@+ordem_encerramento/observacao"
                            android:layout_width="fill_parent"
                            android:layout_height="fill_parent" />
                    </FrameLayout>
                </LinearLayout>
            </TabHost>
        </LinearLayout>
    </ScrollView>

</LinearLayout>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T21:16:38+00:00Added an answer on June 13, 2026 at 9:16 pm

    Thanks to all who tried to help me!
    I do not have much experience with android but from what I saw, the classes must be executed according to the hierarchy proposed by android. That is, in my case I have a main activity which calls the mine fragments and through one of the fragments I had to call a DialogFragment and this would inflate DialogFragment several Views (my tabs), so I have the following structure:

    Activity > Fragment > DialogFragment > Views

    Based on this solution for my case was thus:

    import android.app.Activity;
    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;
    
    public class OrdemEncerramentoFragment 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 TabHost mTabHost;
    
        private Ordem ordem;
        private IClasseBO classeBO;
        private IDispacher dispatch;
        private IFragmentCo ifrag;
    
        private int mCurrentTab;
    
        public OrdemEncerramentoFragment(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) {
                    OrdemEncerramentoFragment.this.dismiss();
                }
            });
    
            enviar.setOnClickListener(new OnClickListener() {
    
                @Override
                public void onClick(View v) {
                    View view = mTabHost.getChildAt(0);
                    new DefeitoFalhaView().getDadosDefeitoFalha(view);
                }
            });
    
            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(0);
    
        }
    
        private void setupTabs() {
            mTabHost.setup(); // importante!
            mTabHost.addTab(inserirAba(TAB_DADOS_CLIENTE, R.string.tab_dados_cliente, new DadosClienteView().createView(getActivity())));
            mTabHost.addTab(inserirAba(TAB_DEFEITO_FALHA, R.string.tab_defeito_falha, new DefeitoFalhaView().createView(getActivity())));
            mTabHost.addTab(inserirAba(TAB_SERVICOS, R.string.tab_servico, new ServicoView().createView(getActivity())));
            mTabHost.addTab(inserirAba(TAB_MATERIAIS, R.string.tab_material, new MaterialView().createView(getActivity())));
            mTabHost.addTab(inserirAba(TAB_OBSERVACAO, R.string.tab_observacao, new ObservacaoView().createView(getActivity())));
        }
    
        private TabSpec inserirAba(String tag, int labelId, final View view) {
            TabHost.TabSpec spec = mTabHost.newTabSpec(tag);
    
            spec.setContent(new TabHost.TabContentFactory() {
              public View createTabContent(String tag) {
                return(view);
              }
            });
            spec.setIndicator(buildTabIndicator(labelId));
            return spec;
        }
    
        private View buildTabIndicator(int msg) {
             View indicator=LayoutInflater.from(mTabHost.getContext()).inflate(R.layout.tabs_bg_view, null);
             TextView tv=(TextView)indicator.findViewById(R.id.tabsText);
    
             tv.setText(msg);
    
             return(indicator);
         }
    
    
        @Override
        public void onTabChanged(String tabId) {
            Log.d(TAG, "onTabChanged(): tabId=" + tabId);
        }
    
        public View getmRoot() {
            return mRoot;
        }
    
    }  
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
this is what i have right now Drawing an RSS feed into the php,
I have a small JavaScript validation script that validates inputs based on Regex. I
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
This could be a duplicate question, but I have no idea what search terms
I know there's a lot of other questions out there that deal with this
I need a function that will clean a strings' special characters. I do NOT

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.