Would like to check with you something about activity and fragment lifecycle.
Assuming I have Activity A
calling Activity B
. Inside Activity B
, I have Fragment C
and Fragment D
separated by Tabs.
Fragment C
will load spinner values downloaded from web API. After user press back button to Activity A
and then to Activity B
again, i do not want to reload the spinner and call web API to populate its values.
Where should I put the code to initialize spinner only once at Fragment C
in this scenario?
Here is how I create Activity B
public class Activity_B extends custom_activity{
final Context context = this;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.search);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
viewPager.setAdapter(new SectionPagerAdapter(getSupportFragmentManager()));
tabLayout.setupWithViewPager(viewPager);
}
public class SectionPagerAdapter extends FragmentPagerAdapter {
public SectionPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new Fragment_C();
case 1:
default:
return new Fragment_D();
}
}
@Override
public int getCount() {
return 2;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return getResources().getString(R.string.fragment_c);
case 1:
default:
return getResources().getString(R.string.fragment_d);
}
}
}
}