Could any one let me know how to open a specific fragment present in a viewpager from another activities button click event.
I am using a alert dialog which shows two options to select respective screens in viewpager like below but I am getting null pointer on clicking screentwo button which has code to open viewpagers second page.
public class MainActivity extends FragmentActivity {
int pos;
ViewPager pager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pager = (ViewPager) findViewById(R.id.viewPager);
pager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));
Button ds = (Button) findViewById(R.id.show_back);
ds.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent goIntent = new Intent(MainActivity.this,SecondActivity.class);
startActivity(goIntent);
}
});
}
private class MyPagerAdapter extends FragmentPagerAdapter {
public MyPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int pos) {
switch(pos) {
case 0: return FirstFragment.newInstance("FirstFragment, Instance 1");
case 1: return SecondFragment.newInstance("SecondFragment, Instance 1");
default: return FirstFragment.newInstance("FirstFragment, Instance 1");
}
}
@Override
public int getCount() {
return 2;
}
}
goesBack = (Button) findViewById(R.id.show_front);
goesBack.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(SecondActivity.this, AlertDialog.THEME_DEVICE_DEFAULT_LIGHT);
alertDialogBuilder.setMessage("Please choose your Favourite Screen");
alertDialogBuilder.setNegativeButton("ScreenOne",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent go = new Intent(SecondActivity.this,MainActivity.class);
startActivity(go);
}
});
alertDialogBuilder.setPositiveButton("ScreenTwo", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
// Create new fragment and transaction
SecondFragment newFragment = new SecondFragment();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.container, newFragment, "NewFragment");
transaction.addToBackStack(null);
transaction.commit();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
});
Thanks & Regards,
Aditya.J
In your onClick just pass the position of viewpager you want to show in the intent's extras, and in the viewpager activity just call pager.setCurrentItem(position)
.
So code would be something like this for your onClick
goesBack = (Button) findViewById(R.id.show_front);
goesBack.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(SecondActivity.this, AlertDialog.THEME_DEVICE_DEFAULT_LIGHT);
alertDialogBuilder.setMessage("Please choose your Favourite Screen");
alertDialogBuilder.setNegativeButton("ScreenOne",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent go = new Intent(SecondActivity.this,MainActivity.class);
// you pass the position you want the viewpager to show in the extra,
// please don't forget to define and initialize the position variable
// properly
go.putExtra("viewpager_position", 0)
startActivity(go);
}
});
alertDialogBuilder.setPositiveButton("ScreenTwo", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
// Create new fragment and transaction
/*SecondFragment newFragment = new SecondFragment();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.container, newFragment, "NewFragment");
transaction.addToBackStack(null);
transaction.commit();*/
// You don't do the above code, it will just create a new fragment
// you do this instead.
Intent go = new Intent(SecondActivity.this,MainActivity.class);
// you pass the position you want the viewpager to show in the extra,
// please don't forget to define and initialize the position variable
// properly
go.putExtra("viewpager_position", 1)
startActivity(go);
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
});
The code for your viewpager activity would be something like this.
int pos;
ViewPager pager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
int position = 0;
Bundle extras = getIntent().getExtras();
if(extras != null) {
position = extras.getInt("viewpager_position");
}
pager = (ViewPager) findViewById(R.id.viewPager);
MyPagerAdapter myPagerAdapter = new MyPagerAdapter(getSupportFragmentManager());
pager.setAdapter(myPagerAdapter);
Button ds = (Button) findViewById(R.id.show_back);
ds.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent goIntent = new Intent(MainActivity.this,SecondActivity.class);
startActivity(goIntent);
}
});
pager.setCurrentItem(position) // this way if there is no extras
// you will get first position of viewpager
// otherwise you will get the position of viewpager whichever was passed in the intent.
}
viewPager.setCurrentItem(THE_POSITION)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With