• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Confused how to get an EditText field from a non-selected tab

3plconnor

Lurker
Hi all,

I've just started on using the Tabbed Activity in Android. So far, everything seems to be working as it should. I can change the layout depending on the tab position and so forth. When swiping between the tabs, the input fields appear to retain their data as I need. Now I've run into the issue of needing to get the input fields from each tab including the one that isn't selected.

Is there any clean way of doing this, or would I have to create a fragment class for each of the tabs and start setting data for that.

Here is what I've got so far,

Java:
public class A_ItemSpecs extends AppCompatActivity {

    /**
     * The {@link android.support.v4.view.PagerAdapter} that will provide
     * fragments for each of the sections. We use a
     * {@link FragmentPagerAdapter} derivative, which will keep every
     * loaded fragment in memory. If this becomes too memory intensive, it
     * may be best to switch to a
     * {@link android.support.v4.app.FragmentStatePagerAdapter}.
     */
    private SectionsPagerAdapter mSectionsPagerAdapter;

    /**
     * The {@link ViewPager} that will host the section contents.
     */
    private ViewPager mViewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_a__item_specs);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        // Create the adapter that will return a fragment for each of the three
        // primary sections of the activity.
        mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

        // Set up the ViewPager with the sections adapter.
        mViewPager = (ViewPager) findViewById(R.id.container);
        mViewPager.setAdapter(mSectionsPagerAdapter);

        TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);

        mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
        tabLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(mViewPager));
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_a__item_specs, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {
        /**
         * The fragment argument representing the section number for this
         * fragment.
         */
        private static final String ARG_SECTION_NUMBER = "section_number";

        public PlaceholderFragment() {
        }

        /**
         * Returns a new instance of this fragment for the given section
         * number.
         */
        public static PlaceholderFragment newInstance(int sectionNumber) {
            PlaceholderFragment fragment = new PlaceholderFragment();
            Bundle args = new Bundle();
            args.putInt(ARG_SECTION_NUMBER, sectionNumber);
            fragment.setArguments(args);
            return fragment;
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {

            View rootView = null;

            switch(getArguments().getInt(ARG_SECTION_NUMBER)) {
                case 1:
                    rootView = inflater.inflate(R.layout.fragment_a__itemspecs_title, container, false);
                   
                    break;
                case 2:
                    rootView = inflater.inflate(R.layout.fragment_a__itemspecs_brand, container, false);

                    break;
                case 3:
                  
                    break;
                case 4:
                   
                    break;
                case 5:
                 
                    break;
                case 6:
                  
                    break;
            }

            return rootView;
        }
    }

    /**
     * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
     * one of the sections/tabs/pages.
     */
    public class SectionsPagerAdapter extends FragmentPagerAdapter {

        public SectionsPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            // getItem is called to instantiate the fragment for the given page.
            // Return a PlaceholderFragment (defined as a static inner class below).
            return PlaceholderFragment.newInstance(position + 1);
        }

        @Override
        public int getCount() {
            // Show 3 total pages.
            return 6;
        }
    }

There are tutorials that talk about passing data from fragment to fragment but I just need to get all the Tab inputs at the end. If anyone could point me in the right direction how to do this, it would be much appreciated.
 
A way of doing this would be to use the Activity as a store for all the common data you need to access across all the tabs. The parent Activity is accessible in your Fragments by calling getActivity(). Implement setter/getter methods for the data you need to share.
But really this is a bit of a hack, and suggests that your UI model isn't quite right. Are you sure that a tabbed view is a suitable implementation for your requirements?
 
A way of doing this would be to use the Activity as a store for all the common data you need to access across all the tabs. The parent Activity is accessible in your Fragments by calling getActivity(). Implement setter/getter methods for the data you need to share.
But really this is a bit of a hack, and suggests that your UI model isn't quite right. Are you sure that a tabbed view is a suitable implementation for your requirements?

Thanks for your answer. My main aim is the app in question has a list of sections with questions in them that need to be answered. Each tab will be it's own category with their own few questions. Once this is done, I can get the data from the input fields in the tab. Is this the best approach or is there something I'm missing?
 
Back
Top Bottom