简体   繁体   中英

How can I start a new activity in a ScreenSlidePagerAdapter?

I Have a FragmentActivity where I have a ScreenSlidePagerAdapter that shows many fragments with the getItem() method, for swipe between fragments. I want that once the user finalized swipping the fragments (from 0 to 15 for example) there's going to open a new activity showing the results of what the user had done navigating the fragments (it's a course).

What I need is an intent for open a New Activity after the fragments.

This is my code:

    package activities;

import android.content.Intent;
import android.net.Uri;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity; 
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;

import com.flixarts.ar.*.R;
import com.flixarts.ar.*.fragment1;
import com.flixarts.ar.*.fragment10;
import com.flixarts.ar.*.fragment11;
import com.flixarts.ar.*.fragment12;
import com.flixarts.ar.*.fragment13;
import com.flixarts.ar.*.fragment14;
import com.flixarts.ar.*.fragment15;
import com.flixarts.ar.*.fragment2;
import com.flixarts.ar.*.fragment3;
import com.flixarts.ar.*.fragment4;
import com.flixarts.ar.*.fragment5;
import com.flixarts.ar.*.fragment6;
import com.flixarts.ar.*.fragment7;
import com.flixarts.ar.*.fragment8;
import com.flixarts.ar.*.fragment9;
import com.flixarts.ar.*.menuscreen;

public class saludos extends FragmentActivity implements      fragment1.OnFragmentInteractionListener, fragment2.OnFragmentInteractionListener, fragment3.OnFragmentInteractionListener, fragment4.OnFragmentInteractionListener {

private TextView espacioFrases;
private TextView espacioFrasesTraducidas;
private Button next;
private Button buttonHome;
private ViewPager mPager;
private PagerAdapter pagerAdapter;
private static final int NUM_PAGES = 15;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_saludos);
 mPager = (ViewPager) findViewById(R.id.pager);
    pagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
    mPager.setAdapter(pagerAdapter);



}
@Override
public void onBackPressed(){
    if (mPager.getCurrentItem() == 0) {
        super.onBackPressed();
    }
    else {
        mPager.setCurrentItem(mPager.getCurrentItem() -1);
    }
}

@Override
public void onFragmentInteraction(Uri uri) {

}

private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
    public ScreenSlidePagerAdapter(FragmentManager fm){
        super(fm);
    }
    @Override
    public Fragment getItem(int position){
        switch (position){
            case 0: return new fragment1();
            case 1: return new fragment2();
            case 2: return new fragment3();
            case 3: return new fragment4();
            case 4: return new fragment5();
            case 5: return new fragment6();
            case 6: return new fragment7();
            case 7: return new fragment8();
            case 8: return new fragment9();
            case 9: return new fragment10();
            case 10: return new fragment11();
            case 11: return new fragment12();
            case 12: return new fragment13();
            case 13: return new fragment14();
            case 14: return new fragment15();
            //Here I want to start new activity
            default: return new fragment1();
        }
    }

    @Override
    public int getCount(){
        return NUM_PAGES;
    }
}

I've trying create an intent with getActivity() but this method wasn't loaded in Android Studio (maybe this is old).

You should write the startactivity code inside last showing fragment of adapter. (If you are using instances of same fragment, then you have to pass the position from adapter to fragment while creating it.

Intent intent = new Intent(getContext(), ThirdActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);

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