I am trying to call a method from FragmentStatePagerAdapter to add and removed tabs.
public class TabPagerAdapter extends FragmentStatePagerAdapter {
public void removeTab(int position) {
new MainActivity().tabLayout.removeTabAt(1);
notifyDataSetChanged();
}
}
Now in the main activity I want to call it so that i can remove the tab on ActivityResult
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//this gives error
new TabPagerAdapter().removeTab(1);
}
could you please help me with this ? Thank you.
Your MainActivity should have an instance of the FragmentStatePagerAdapter. You can put a method inside your FragmentStatePagerAdapter, like you have, but it should adjust your getItem() function and doesn't need an instance of the MainActivity. Then in your MainActivity, you can use dot syntax to call the method you created on the instance of FragmentStatePagerAdapter you have.
public class MainActivity extends AppCompatActivity {
private TabPagerAdapter tabPagerAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
tabPagerAdapter = new TabPagerAdapter
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
tabPagerAdapter.removeTab(1);
}
}
public class TabPagerAdapter extends FragmentStatePagerAdapter {
public void removeTab(int position) {
// adjust your FragmentStatePagerAdapter however you see fit.
}
}