Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to open a specific fragment page in viewpager from another activity button click event

Tags:

android

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

like image 965
Adithya Avatar asked Nov 10 '15 09:11

Adithya


2 Answers

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.
}
like image 128
Bhargav Avatar answered Oct 09 '22 06:10

Bhargav


viewPager.setCurrentItem(THE_POSITION)
like image 26
user5534193 Avatar answered Oct 09 '22 06:10

user5534193