0

I have MainActivity and SecondActivity. in main activity implemented a viewpager and in my SecondActivity i have button. when i click on button in SecondActivity move to main activity and jump to spacific viewpager page. Please provide suitable examples.

satyan_android
  • 364
  • 4
  • 15
  • examples ?what u have tried please show some code? – Bunny Mar 14 '19 at 09:34
  • Possible duplicate of [How do I pass data between Activities in Android application?](https://stackoverflow.com/questions/2091465/how-do-i-pass-data-between-activities-in-android-application) – ADM Mar 14 '19 at 10:00
  • I tried this : In main activity. Where i implemented ViewPager. public void pageJump(int page) { viewpager.setCurrentItem(page) } Then in 2nd activity in button click. MainActivity main=new MainActivity() ; main.pageJump(50) But it not worked even in my project when i call method 1 activity to another activity its also nor worken. App build without any error but when i test the app it is crashes on button click. – Mohammad Shehzad Mar 14 '19 at 14:14

2 Answers2

0

In your code on button click write this line,

startActivity(new Intent(CurrentActivity Name, TargetActivity.class).putExtras(tempBundle)

tempBundle is a Bundle argument for identify which position of the pager you want to display at current.

Then in your target Activity get the Bundle information, and set your ViewPager.

viewPager.setCurrentItem(0); //You can set your Bundle information 0 or 1 or 3

That time your ViewPager can set according to given position.

hata
  • 11,633
  • 6
  • 46
  • 69
  • I tried this : In main activity. Where i implemented ViewPager. public void pageJump(int page) { viewpager.setCurrentItem(page) } Then in 2nd activity in button click. MainActivity main=new MainActivity() ; main.pageJump(50) But it not worked even in my project when i call method 1 activity to another activity its also nor worken. App build without any error but when i test the app it is crashes on button click. – Mohammad Shehzad Mar 14 '19 at 14:14
0

We have Two ways

  • Use Static Methods OR
  • Use interface to communicate between activities and fragments also

Communication through Interface

satyan_android
  • 364
  • 4
  • 15