怎么没有将位置信息传递给我的片段?

时间:2014-07-29 20:20:38

标签: android android-fragments tabview

我有一个Activity,在标签式活动的默认Android Studio示例中基本上没有修改。其FragmentPagerAdapter被修改为显示所有50个美国,其中50个相应的标签显示其名称。这种方法一直有效,直到一个片段被销毁,但重新创建时,它没有被告知它在哪个标签上。为什么会这样?

以下是我认为可能成为问题一部分的所有方法:

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);
    }

    ...

}

我使用密钥States.BUNDLE_KEY"STATE")将其保存在捆绑包中,但是它给出的捆绑包第二次没有该密钥。出于调试目的,我覆盖所有处理加载和卸载的on*方法,例如:

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

当然,我还有更多的调试输出。

我希望我录制的视频有助于说明问题:http://youtu.be/cmbR_2rvpX4

视频的控制台转储位于此pastebin中:http://pastebin.com/rxAP7qda

而且,如果所有这些仍然没有帮助,那么这里的 git https://github.com/Supuhstar/US-State-Quiz-App


即便在这之后,我觉得我没有提供正确的信息。如果您认为可以做得更好,请少花些或更多。

1 个答案:

答案 0 :(得分:3)

您未在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));
}