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

  • Home
  • SEARCH
  • 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 8455237
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T12:14:05+00:00 2026-06-10T12:14:05+00:00

I have a FragmentPagerAdapter that pages through views generated by a custom SurveyPage class.

  • 0

I have a FragmentPagerAdapter that pages through views generated by a custom SurveyPage class. The final page in the pager is generated by SurveyFinishPage, which tries to access the Survey associated with the FragmentPagerAdapter and pulls all of the survey answers together to display them. The Survey object is grabbed by SurveyFinishPage via a getSurvey() method in the main Activity.

Normally this works fine and the SurveyFinishPage is able to access the same Survey object that the FragmentPagerAdapter is using. If the Activity has been killed and restored, though, the SurveyFinishPage seems to still be accessing the old Survey (or maybe a copy of it) – it displays the answers from before the Activity was discarded regardless of changes to answers to the new, rebuilt Survey.

It seems like restoring the value of aSurvey in onRestoreInstanceState() in my MainActivity should establish the survey object for everything else, since MainActivity passes its Survey object to the FragmentPagerAdapter, which uses that object to make Fragments… but instead it seems that the FragmentPagerAdapter and the SurveyFinishPage are looking at two different things.

How can I restore the state of the fragments in my FragmentPagerAdapter properly so that they all reference the same Survey object?

MainActivity.java

public class MainActivity extends FragmentActivity {
    SurveyPagerAdapter mSurveyPagerAdapter;
    ViewPager mViewPager;
    Survey aSurvey;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_poll);

        if(savedInstanceState != null) {
            testSurvey = savedInstanceState.getParcelable("survey");
        }
        else {
            testSurvey = new Survey();
        }
        mSurveyPagerAdapter = new SurveyPagerAdapter(getSupportFragmentManager(), aSurvey);
        mViewPager = (ViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(mSurveyPagerAdapter);
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putParcelable("survey", aSurvey);
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        aSurvey = (Survey)savedInstanceState.getParcelable("survey");
    }
    [...]
}

SurveyPagerAdapter.java

public class SurveyPagerAdapter extends FragmentPagerAdapter {
    private Survey survey;
    [...]

    @Override
    public Fragment getItem(int i) {
        Fragment fragment;
        Bundle args = new Bundle();

        // index 0 is the title page
        if(i == 0) {
            fragment = new SurveyTitlePage();
        }
        // final index is the finish page
        else if(i == getCount()-1) {
            fragment = new SurveyFinishPage();
        }
        // all other indices are regular poll pages
        else {
            args.putParcelable("question", survey.getQuestion(i-1));
            fragment = new SurveyPage();
            fragment.setArguments(args);
        }

        return fragment;
    }
    [...]
}

SurveyFinishPage.java

public class SurveyFinishPage extends Fragment {
    private Survey survey;
    [...]

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    setSurvey(((MainPoll)getActivity()).getSurvey());
    LinearLayout view = new LinearLayout(getActivity());
    view.setOrientation(LinearLayout.VERTICAL);

    Button finishButton = new Button(getActivity());
    finishButton.setText("FINISH");

        final TextView textView = new TextView(getActivity());
        finishButton.setOnClickListener(new Button.OnClickListener() {
            @Override
            public void onClick(View v) {
                String output = "";

                for(int i=0; i<survey.getSize(); i++) {
                    try {
                        output += survey.getQuestion(i).getPrompt() + " - " + survey.getQuestion(i).getAnswer() + "\n";
                    } catch (Exception e) {
                        output += survey.getQuestion(i).getPrompt() + " - " + "no answer\n";
                    }
                }
                textView.setText(output);
            }
        });

        view.addView(finishButton);
        view.addView(textView);

        return view;
    }
}

SurveyPage.java

public class SurveyPage extends Fragment {
        [...]

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            Bundle args = getArguments();
            Question question = args.getParcelable("question");
            View pageView = question.getQuestionView(getActivity(), container);
        return pageView;
    }
}
  • 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-10T12:14:06+00:00Added an answer on June 10, 2026 at 12:14 pm

    You could lift the Survey object to the Android application class and reference it as a singleton. That answer will likely create some debate, but it is a reasonable solution to your problem. Android guarantees there will only ever be 1 instance of your application so you can be certain (within reason) that you’ll have only 1 instance of Survey.

    http://developer.android.com/guide/faq/framework.html

    Singleton class

    You can take advantage of the fact that your application components run in the same process through the use of a singleton. This is a class that is designed to have only one instance. It has a static method with a name such as getInstance() that returns the instance; the first time this method is called, it creates the global instance. Because all callers get the same instance, they can use this as a point of interaction. For example activity A may retrieve the instance and call setValue(3); later activity B may retrieve the instance and call getValue() to retrieve the last set value.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a viewpager that pages through fragments. My FragmentPagerAdapter subclass creates a new
have written this little class, which generates a UUID every time an object of
So I have an Activity which has a FragmentPagerAdapter and inside each fragment I
I have a FragmentActivity with a FragmentPagerAdapter which holds a number of Fragment objects
I have an activity that uses a fragmentpageradapter to create an ics style actionBar.
I have an adapter that extends FragmentPagerAdapter and takes advantage of the ICS style
I have a nested inner class that extends AsyncTask to run a db query
I have a ViewPager (extends FragmentPagerAdapter ) which holds two Fragments . What I
Have you seen yahoo's ipad/tablet home page? The swipe effect for flipping through headlines
I have a ViewPager hooked up to a FragmentPagerAdapter that's displaying three fragments. The

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.