How is the location information not transmitted to my fragments?

I have Activityone that is pretty much unmodified from the standard Android Studio example for tabbed activity. Its FragmentPagerAdaptermodified to display all 50 of the United States, with 50 corresponding tabs displaying their names. This works until the fragment is destroyed, but when it is recreated, he did not say which tab it is on. Why is this happening?

Listed below are all the methods that I think may be part of the problem:

public class MainQuizActivity extends Activity implements ActionBar.TabListener {

    ...

    public class SectionsPagerAdapter extends FragmentPagerAdapter {

        ...

        @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
                questionFragments[position] == null
                    ? questionFragments[position] = QuestionFragment.newInstance(position)
                    : questionFragments[position];
        }

        ...
    }

    ...
}

...

public class QuestionFragment extends Fragment {

    ...

    public static QuestionFragment newInstance(int stateIndex) {
        System.out.println("Creating new instance for state #" + stateIndex);
        QuestionFragment fragment = new QuestionFragment();
        Bundle args = new Bundle();
        args.putInt(States.BUNDLE_KEY, stateIndex);
        fragment.setArguments(args);
        return fragment;
    }

    ...

    @Override
    public View onCreateView(
        LayoutInflater inflater,
        ViewGroup container,
        Bundle savedInstanceState) {
        System.out.println("~onCreateView");
        View rootView = inflater.inflate(
            R.layout.fragment_main_quiz, container, false);

        webView = (WebView)rootView.findViewById(R.id.webView);
        initState(savedInstanceState);

        return rootView;
    }

    private void initState(Bundle args) {
        if (state == null) {
            System.out.println("Bundle: " + args);
            if (args == null)
                args = getArguments();
            System.out.println("Bundle is now: " + args);
            int stateIndex = args.getInt(States.BUNDLE_KEY);
            System.out.println("Gonna be state #" + stateIndex);
            state = States.values()[stateIndex];
            System.out.println("Gonna be " + state);
        }
        else
            System.out.println("State already exists! (yay!)");

        String path = state.getImageURL();
        System.out.println("Opening image at " + path);

        webView.loadUrl(path);
        webView.setBackgroundColor(0x00000000);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        System.out.println("~onCreate");
        super.onCreate(savedInstanceState);
        if (webView == null)
            webView = (WebView)(getActivity().findViewById(R.id.webView));
        System.out.println("onCreate: webView == " + webView);
        System.out.println("onCreate: bundle == " + savedInstanceState);

        if (webView != null
            && savedInstanceState != null)
        initState(savedInstanceState);
    }

    ...

}

I saved this in a package with a key States.BUNDLE_KEY(which is "STATE"), but it does not have this key specified a second time. For debugging purposes, I tried all the methods on*that deal with loading and unloading empty:

@Override public void onResume(){
    System.out.println("~onResume");
    super.onResume();}

, , .

, , , : http://youtu.be/cmbR_2rvpX4

: http://pastebin.com/rxAP7qda

- , git: https://github.com/Supuhstar/US-State-Quiz-App


, , . , , , .

+4
1

QuestionFragment. , initState(savedInstanceState); onCreate() onCreateView(). onCreateView() onCreate():

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    System.out.println("~onCreateView");
    View rootView = inflater.inflate(R.layout.fragment_main_quiz, container, false);

    webView = (WebView)rootView.findViewById(R.id.webView);
    guessSpinner = (Spinner)getActivity().findViewById(R.id.guess_spinner);
    initState();
    initGuesser();
    return rootView;
}

initState(savedInstanceState) , :

private void initState() {
     Bundle args = getArguments();
     if (args == null) {
        throw new IllegalArgumentException("The arguments should be valid!");
     }
     System.out.println("Bundle is now: " + args);
     int stateIndex = args.getInt(States.BUNDLE_KEY); 
     System.out.println("Gonna be state #" + stateIndex);
     state = States.values()[stateIndex];
     System.out.println("Gonna be " + state);
     String path = state.getImageURL();
     System.out.println("Opening image at " + path);

     webView.loadUrl(path);
     webView.setBackgroundColor(getResources().getColor(R.color.transparent));
}
+3

All Articles