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 7585643
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T19:13:33+00:00 2026-05-30T19:13:33+00:00

I’m trying to build a Fragment that holds a gridview for a nice UI.

  • 0

I’m trying to build a Fragment that holds a gridview for a nice UI. I have used some of the sample code to try and build this.

Do I really need to keep track and save the state of my fragments for orientation switches (apparently I do) or am I missing something? From the sound of it (it being the FragmentPagerAdapter docs) it seems like the FragmentPagerAdapter saves all states for me…

Suffice to say, here is my problem(I have 4 fragments with some textviews in them):

1. When I tilt the screen on Fragment0, the underlying data disappears and will not reappear until I move to Fragment2.

2. When I tilt the screen on Fragment1, the underlying data of it AND Fragment0 disappears and Fragment1’s data wont reappear until I go to Fragment3 BUT Fragment0’s data will reappear when I go to Fragment2 (just like case 1 above).

3. When I tilt the screen on Fragment2, the underlying data of it AND Fragment1 disappears and Fragment1 reappears if I go to Fragment3 and Fragment2 will reappear if I go to Fragment0.

4. When I tilt Fragment3 only Fragment2 disappears.

I’m obviously doing something wrong so here is my code:

Here is my onCreate(or some of it atleast):

mAdapter = new FragPagerAdapter(getSupportFragmentManager());
    mPager = (ViewPager)findViewById(R.id.pager);
    mPager.setAdapter(mAdapter);

Here is my FragmentPagerAdapter:

private class FragPagerAdapter extends FragmentPagerAdapter {


            public FragPagerAdapter(FragmentManager fm) {
                            super(fm);

            }

            @Override
            public int getCount() {
                  return numFrags;
            }

            @Override
            public Fragment getItem(int position) {

                return ArrayFragment.newInstance(position, mItem2.get(position));

            }

}

Here is my Fragment (with my ArrayAdapter):

public static class ArrayFragment extends Fragment {

    int mNum;
    private ArrayList<Item> mItems;
    private GridAdapter myGridAdapter;
    private static GridView mGridView;

    static ArrayFragment newInstance(int num, ArrayList<Item> items) {
        ArrayFragment f = new ArrayFragment();

        Bundle args = new Bundle();
        args.putInt("num", num);
        args.putSerializable("items", items);
        f.setArguments(args);

        return f;
    }


    @SuppressWarnings("unchecked")
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mNum = getArguments() != null ? getArguments().getInt("num") : 1;
        mItems = (ArrayList<Item>) (getArguments() != null ? getArguments().getSerializable("items") : null);
        myGridAdapter = new GridAdapter(getActivity().getApplicationContext(), R.layout.grid_item, mItems);

    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        //View v = inflater.inflate(R.layout.fragment_pager_list, container, false);
        View v = inflater.inflate(R.layout.grid, container, false);
        View tv = v.findViewById(R.id.text);
        ((TextView)tv).setText("Fragment #" + mNum);

        mGridView = (GridView) v.findViewById(R.id.gridview);
        return v;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

    }

    @Override
    public void onStart()
    {
        super.onStart();
        mGridView.setAdapter(myGridAdapter); 
        mGridView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {
                Toast.makeText(getActivity(), mItems.get(arg2).getSecret(), Toast.LENGTH_LONG).show();                  
            }

        });
    }


    public class GridAdapter extends ArrayAdapter<Item> {
        private int count;
        private LayoutInflater inflater;
        private Context context;
        private ArrayList<Item> items;

        public GridAdapter(Context context, int resource,
                ArrayList<Item> items_) {
            super(context, resource);
            count = items_.size();
            items = items_;

            inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            // TODO Auto-generated constructor stub
        }

        @Override
        public int getCount()
        {
            return count;
        }

        @Override
        public Item getItem(int position)
        {
             return items.get(position);
        }

        @Override
        public long getItemId(int position)
        {
                return position;
        }

        public void setItems(ArrayList<Item> items)
        {
            this.items = items;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent)
        {
                View v = convertView;
                try
                {
                        v = inflater.inflate(R.layout.grid_item, null);
                }
                catch (NullPointerException npe)
                {
                        npe.printStackTrace();

                }                    

                Item item = items.get(position);

                try
                {
                        TextView name = (TextView) v.findViewById(R.id.grid_att_name);
                        TextView value = (TextView) v.findViewById(R.id.grid_att_value);


                        name.setText(item.getName());   
                        value.setText(item.getValue());
                }
                catch (NullPointerException npe)
                {
                        npe.printStackTrace();

                }

                return v;
        }
    }
}
  • 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-05-30T19:13:35+00:00Added an answer on May 30, 2026 at 7:13 pm

    I fixed this by using a Fragment State Adapter and I had to add android:configChanges="orientation" to the activity tag of the activity that housed the fragments.

    • 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 have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have some data like this: 1 2 3 4 5 9 2 6
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I'm trying to create an if statement in PHP that prevents a single post

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.