简体   繁体   中英

Android: Load activity or intent from fragment page

I'm trying to load an activity from within a fragment. I've simply used the base android sample code and added a few different layouts. The code below works partially, however, the intent/activity starts when swiping to page 3 and not just page 4. I think it has also started once when swiping from page 3 to page 2. I assume this has something to do with android wanting to preload everything on the pages to make swiping seamless.

Is there a way to load an activity when swiping from one fragment page to another? Or maybe some roundabout way to have the activity launch only after I've actually swiped to the appropriate page? I'm not getting any errors other than the undesirable behavior.

public static class DummySectionFragment extends Fragment {
    /**
     * The fragment argument representing the section number for this
     * fragment.
     */
    public static final String ARG_SECTION_NUMBER = "section_number";

    public DummySectionFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {


        if (getArguments().getInt(ARG_SECTION_NUMBER)==2){
            LayoutInflater inflater2 = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View mDialogView = inflater2.inflate(R.layout.activity_main2, null);
            return mDialogView;
        } else if (getArguments().getInt(ARG_SECTION_NUMBER)==3){
            LayoutInflater inflater2 = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View mDialogView = inflater2.inflate(R.layout.activity_main3, null);
            return mDialogView;
        } else if (getArguments().getInt(ARG_SECTION_NUMBER)==4){
             LayoutInflater inflater2 = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
             View mIntentView = inflater2.inflate(R.layout.page_four_layout, null);
             Intent intent = new Intent(getActivity().getApplication(), PageFourActivity.class);
             startActivity(intent);
             return mIntentView;
        } else {

            // Create a new TextView and set its text to the fragment's section
            // number argument value.
            TextView textView = new TextView(getActivity());
            textView.setGravity(Gravity.CENTER);
            textView.setText(Integer.toString(getArguments().getInt(
                ARG_SECTION_NUMBER)));
            return textView;
        }


    }

If you are using a ViewPager with Fragment s, I think the ViewPager.SimpleOnPageChangeListener should help you. Look at onPageSelected function. Like you guessed, when using ViewPager, android usually loads the pages on either side. This number can be set using setOffscreenPageLimit() . You can try setting it to 0 or something, not tested.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM