0

I've done some research on this already, and I seem to understand the solutions suggested out there. This is not a repeat question, I'm just having trouble implementing these solutions to my viewpager/fragment code.

So here is my "FirstFragment.java" that sets up my view pager, and the fragment it calls is "UserFragment.java". Basically what I'm doing is providing a user defined number of forms created as pages in my view pager.

I of course, am experiencing the orientation change issue, where fragment gets created a couple times, and eventually lost. I would like to fix it.

My code for FirstFragment.java is:

import java.util.ArrayList;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.util.SparseArray;

public class FirstFragment extends FragmentActivity 
  {

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_viewpager);

        // Check whether the activity is using the layout version with
        // the fragment_container FrameLayout. If so, we must add the first fragment
        int size = getIntent().getExtras().getInt("numass1");
        ViewPager vp = (ViewPager) findViewById(R.id.viewPager);



            if (savedInstanceState != null) {
                return;
            }

            ArrayList<Fragment> lf = new ArrayList<Fragment>();
            for(int count=0; count<size; count ++){

                Fragment f = new UserFragment();
                lf.add(f);

            }

 FragmentAdapter hello = new FragmentAdapter(getSupportFragmentManager() , lf);
            vp.setAdapter(hello);

        }

    SparseArray<Question> questions = new SparseArray<Question>();
    public void saveData(Question quest){
        ViewPager vp = (ViewPager) findViewById(R.id.viewPager);
           questions.put(vp.getCurrentItem(), quest);

    }
    public ViewPager getPager() {
        ViewPager vp = (ViewPager) findViewById(R.id.viewPager);
        return vp;
    }

    }

Just incase, my FragmentAdapter.java code is:

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.view.ViewGroup;

public class FragmentAdapter extends FragmentPagerAdapter{


    private List<Fragment> fragments;



    public FragmentAdapter(FragmentManager fragmentManager, ArrayList<Fragment> fragments) {
        super(fragmentManager);
            this.fragments = fragments;
    }

    @Override
    public Fragment getItem(int position) {

        Fragment fragment = fragments.get(position);
        Bundle args = new Bundle();
        args.putInt("page_position", position + 1);
        args.putInt("size", fragments.size());
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void destroyItem (ViewGroup container, int position, Object object)
    {
        super.destroyItem(container, position, object);
    }

    @Override
    public int getCount() {

       return this.fragments.size();
    }
}

Please keep in mind, I am clearly a noob, and I'm trying to learn. So code snippets, and explanations would be awesome!

Thank you very much for your time and help.

EDIT: Please help ...

Leon92
  • 505
  • 1
  • 9
  • 15

1 Answers1

-1

If you add

android:configChanges="orientation|keyboardHidden"

in <activity> node of your file AndroidManifest.xml your activity will not reset when the orientation changed.

Is it what you wanted?

SafaOrhan
  • 690
  • 9
  • 19
  • I added that, under for 'FirstFragment' in my AndroidManifest.xml, the problem still exists. The issue, is mainly caused by every time the orientation changes, onCreate, creates my entire view pager with all my fragments. But, fragment adapter, also tries to restore the last page. So in the end everything gets lost. Well this is as I understand it, at the moment. So I guess this does not fix it! Thank you for your suggestion, if you have any other ideas let me know! – Leon92 Jul 23 '14 at 03:27
  • 2
    -1, This is incredibly wrong. You should never use `android:configChanges` to fix problems like this. Please read [this question/answer](http://stackoverflow.com/q/7818717/844882) for more information. – Alex Lockwood Jul 27 '14 at 06:37