0

I have a MainActivity with one fragment. I have a ListView in pager which is inside fragment of MainActivity. Now when device orientation is changed ListView is not showing any items that were shown before :

MainFragment.java :

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container,
                    false);

    if (myGrp == null)
                myGrp = new ArrayList<List<String>>();

    mActivityAdapter = new MyGroupsAdapter(myContext, myGrp);

    mSectionsPagerAdapter = new SectionsPagerAdapter(
                    myContext.getSupportFragmentManager());
            mViewPager = (ViewPager) rootView.findViewById(R.id.pager);
            mViewPager.setAdapter(mSectionsPagerAdapter);

    mViewPager.setCurrentItem(1);
    }

    public class SectionsPagerAdapter extends FragmentPagerAdapter {

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

            @Override
            public Fragment getItem(int position) {
                Fragment fragment = new DummySectionFragment();
                Bundle args = new Bundle();
                args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position + 1);
                fragment.setArguments(args);
                return fragment;
            }

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

            @Override
            public CharSequence getPageTitle(int position) {
                Locale l = Locale.getDefault();
                switch (position) {
                case 0:
                    return getString(R.string.title_section3).toUpperCase(l);
                case 1:
                    return getString(R.string.title_section2).toUpperCase(l);
                case 2:
                    return getString(R.string.title_section1).toUpperCase(l);
                }
                return null;
            }
        }

    public View onCreateView(LayoutInflater inflater,
                    final ViewGroup container, Bundle savedInstanceState) {
                View dummyView = null;
                if (getArguments().getInt(ARG_SECTION_NUMBER) == 1) {
                    dummyView = inflater.inflate(R.layout.inbox_fragment,
                            container, false);
                    ListView mListView = (ListView) dummyView
                            .findViewById(R.id.inbox_listview);
                    mListView.setAdapter(inboxAdapter);
                    mListView
                            .setOnItemLongClickListener(new OnItemLongClickListener() {

                                @Override
                                public boolean onItemLongClick(AdapterView<?> arg0,
                                        View arg1, int position, long id) {
                                    MainFragment fragment = new MainFragment();
                                    fragment.inboxItem_onLongClick(id);
                                    return false;
                                }
                            });
                } else if (getArguments().getInt(ARG_SECTION_NUMBER) == 2) {
                    dummyView = inflater.inflate(R.layout.chat_fragment, container,
                            false);
                    ListView mListView = (ListView) dummyView
                            .findViewById(R.id.chatGroupList);
                    mListView.setAdapter(mActivityAdapter);
                } else if (getArguments().getInt(ARG_SECTION_NUMBER) == 3) {
                            //any things 
                      }
    }
}



@Override
    public void onResume() {
        super.onResume();
        SharedPreferences prfs = myContext.getSharedPreferences("user_info",
                Context.MODE_PRIVATE);
        GroupsSaved = prfs.getBoolean("GroupsSaved", false);

        if (myGrp == null || myGrp.size() == 0)
            getMyGroups();

        Global.nowChatGrpID = 0;
        mActivityAdapter.notifyDataSetChanged();
    }
Vini.g.fer
  • 11,639
  • 16
  • 61
  • 90
saman
  • 305
  • 2
  • 4
  • 17

3 Answers3

0

Replace this code :

myContext.getSupportFragmentManager();

to

getChildFragmentManager();

may this will work.

If you want to have a fragment which behaves as a container of fragments you must use the getChildFragmentManager method of the fragment. If you use the getSupportFragmentManager you will basically use the fragment manager which behaves the way the activity lifecycle goes, not the way your fragment does.

For example I had a fragment which contained a ViewPager – it is called CollectionsFragment. So I had 3 fragments displayed as tabs in it: AllCollectionsFragment, MyCollectionsFragment, FavouriteCollectionsFragment. And I gave the getActivity().getSupportFragmentManager() to the FragmentStatePagerAdapter which I was using.

So this was causing the following behaviour – the onDestroyView/onDestroy/onDetach/onStop methods of the 3 tab fragments not to be called. When I changed to use the getChildFragmentManager everything was OK.

If you want you can check the docs for the two methods:

getChildFragmentManager():

Return a private FragmentManager for placing and managing Fragments inside of this Fragment.

getSupportFragmentManager():

Return the FragmentManager for interacting with fragments associated with this fragment’s activity.

Bhavesh Rangani
  • 1,490
  • 12
  • 23
0

Probably your activity is reloaded when you change orientation, try to add this to your manifest:

     <activity android:name=".yourActivity" 
      android:configChanges="keyboardHidden|orientation|screenSize"
...

Solve your problem?

Fabio Venturi Pastor
  • 2,519
  • 3
  • 19
  • 32
0

To ensure that your Activity does not reload every time orientation changes, you should declare android:configChanges="keyboardHidden|orientation|screenSize" in your manifest. This ensures the state of the 'Activity' is retained and not reloaded after orientation changes.

Emzor
  • 1,380
  • 17
  • 28