Reputation: 177
I have main activity that contains major parameters that I need to pass to all the application activities. (username - User Privilege - Team .. ETC).
I know how to do that except in an activity that contains three fragments, that are displayed using pager and tablayout. How can I pass the parameters to all these fragments? .. should I pass it to the activity then pass it to the three fragments as a second step? if yes, how can I pass to the fragments in the below code:
Second Activity Java:
public class DCODatabase extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dcodatabase);
Toolbar toolbar = (Toolbar) findViewById(R.id.DCODatabaseToolbar);
setSupportActionBar(toolbar);
assert toolbar != null;
toolbar.setLogo(R.drawable.dco1);
//Receving bundle from main activity// --> Working
Bundle extras = getIntent().getExtras();
TabLayout tabLayout = (TabLayout) findViewById(R.id.tab);
assert tabLayout != null;
tabLayout.addTab(tabLayout.newTab().setText("General Reports"));
tabLayout.addTab(tabLayout.newTab().setText("Report Display"));
tabLayout.addTab(tabLayout.newTab().setText("New Report"));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
final PagerAdapter adapter = new PagerAdapter
(getSupportFragmentManager(), tabLayout.getTabCount(),extras);
assert viewPager != null;
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
}
Pager Adapter:
public class PagerAdapter extends FragmentStatePagerAdapter{
int mNumOfTabs;
private final Bundle fragmentBundle;
public PagerAdapter(FragmentManager fm, int mNumOfTabs, Bundle bundle) {
super(fm);
this.mNumOfTabs = mNumOfTabs;
this.fragmentBundle = bundle; ---> Receving in Adapter.
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
DCOGeneralReports tab1 = new DCOGeneralReports();
tab1.setArguments(fragmentBundle); --> Sending to fragement
return tab1;
case 1:
DCOReportDisplay tab2 = new DCOReportDisplay();
return tab2;
case 2:
DCO_New_Report tab3 = new DCO_New_Report();
return tab3;
default:
return null;
}
}
@Override
public int getCount() {
return mNumOfTabs;
}
}
Fragment Example:
public class DCOGeneralReports extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.dco_general_report, container, false);
// here the problem. (SavedInstance is null! --> Not Receving the data)
Name = savedInstanceState.getString("Name");
UserGroup = savedInstanceState.getString("UserGroup");
Team = savedInstanceState.getString("Team");
Upvotes: 1
Views: 92
Reputation: 1169
Inside activity class when you create an instance of adapter you can pass the argument, so that you can retrieve that value in adapter's constructor.
Inside getItem()
method of PagerAdapter try this
Bundle =new Bundle();
b.putString("key","value");
tab1.setArgument(b);
return tab1;
And also there is no need of passing argument for each activity till you need it in final activity.you can write it by using getIntent.putExtra()
so that later it can be retrieved by using the getExtra("key")
In order to pass the value create an instance like this
ArrayList demo=new ArrayList();
demo.add("A");
final PagerAdapter adapter = new PagerAdapter
(getSupportFragmentManager(), tabLayout.getTabCount(),demo);
And in adapter class change the constructor
ArrayList frag;
public PagerAdapter(FragmentManager fm, int mNumOfTabs,ArrayList frag) {
super(fm);
this.mNumOfTabs = mNumOfTabs;
this.frag = frag;
}
Upvotes: 1