java - How move from fragment to activity when viewpager's last fragment slide? -


i want startactivity(loginactivity) when after 3rd fragment.

in position 3rd fragment, slide right left start activity.

i found question but:

@override public void onpagescrolled(int position, float positionoffset, int positionoffsetpixels) {      if(position == tutorialviewpager.getadapter().getcount() - 1){          intent intent = new intent(this, loginactivity.class);         startactivity(intent);     }  } 

in case, when move 2nd fragment 3rd fragment slide, 3rd fragment(last fragment) appeared little bit , change activity.

i want change activity when slide last fragment(not 2nd fragment 3rd fragment, slide 3rd fragment).

how can warp fragment specific activity when slide last fragment?

please me.

public class tutorialactivity extends appcompatactivity implements viewpager.onpagechangelistener {      int max_page = 3;     fragment cur_fragment = new fragment();      viewpager tutorialviewpager;      private int selectedpageindex = -1;     private boolean exitwhenscrollnextpage = false;       @override     protected void oncreate(@nullable bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_tutorial_page);          tutorialviewpager = (viewpager) findviewbyid(r.id.tutorialviewpager);         tutorialviewpager.setadapter(new adapter(getsupportfragmentmanager()));          tutorialviewpager.addonpagechangelistener(this);      }      @override     public void onpagescrolled(int position, float positionoffset, int positionoffsetpixels) {          if(position == tutorialviewpager.getadapter().getcount() - 1){              intent intent = new intent(this, loginactivity.class);             startactivity(intent);         }      }      @override     public void onpageselected(int position) {      }      @override     public void onpagescrollstatechanged(int state) {      }      private class adapter extends fragmentpageradapter{          public adapter(fragmentmanager fm) {             super(fm);         }          @override         public fragment getitem(int position) {              if(position<0 || max_page <= position){                 return null;             }              switch (position){                  case 0:                     cur_fragment = new tutorialfragmenta();                     break;                  case 1:                     cur_fragment = new tutorialfragmentb();                     break;                  case 2:                     cur_fragment = new tutorialfragmentc();                     break;               }             return cur_fragment;         }          @override         public int getcount() {             return max_page;         }     }   } 

you have modify implementation of methods of viewpager.onpagechangelistener. here used , managed solve issue.

i put here transition (loginactivity).

mviewpager.addonpagechangelistener(new viewpager.simpleonpagechangelistener() {         boolean onlastpagechanged = false;         @override         public void onpageselected(int position) {         }          @override         public void onpagescrolled(int position, float positionoffset, int positionoffsetpixels) {             int lastfrag = mquestioncollectionpageradapter.getcount() -1;              if (onlastpagechanged && position == lastfrag) {                 //next                 log.e(tag, "transition happened");             }         }          @override         public void onpagescrollstatechanged (int state) {             int lastfrag = mquestioncollectionpageradapter.getcount() -1;              int currentfragment = mviewpager.getcurrentitem();             if (currentfragment == lastfrag /*&& lastpos==lastidx*/ && state == 1) {                 onlastpagechanged = true;                 log.e(tag, "page scroll state >>>>>>");                 intent intent = new intent(this, loginactivity.class);         startactivity(intent);                 finish();             } else {                 onlastpagechanged = false;             }         }     }); 

Comments