Reputation: 306
I've created tab view using the code example on the android web. http://developer.android.com/training/implementing-navigation/lateral.html
I've added this code to call a the setTitle method which would set the title.
mViewPager.addOnPageChangeListener(
new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
FragmentInterface f = (FragmentInterface) mSectionsPagerAdapter
.getItem(position);
f.setTitle();
}
});
In my fragment class, I've define setTitle as
@Override
public void setTitle() {
System.out.println("Title " + cellMain);
if (cellMain != null) {
getActivity().getActionBar()
.setTitle(cellMain.cellData.getIPWithPort());
}
}
and I also have a button which does :
public void connect(View view) {
System.out.println("Connect " + cellMain);
if (cellMain == null) {
cellMain = CellMain.createAndroid(this,
getActivity().getFilesDir().toString());
getActivity().getActionBar()
.setTitle(cellMain.cellData.getIPWithPort());
}
First I click the button which calls connect, It prints out "Connect null" which is expected as it has not been initialized.
Pressing the button again print out "Connect cell.CellMain@....".
Now I change the view by selecting another tab and changing back to the same tab.
setTitle() method prints out "Title null" which is not expected as cellMain is initialized. Pressing of the button still prints out "Connect cell.CellMain@...."
Why does my setTitle method give a null value for my variable?
Upvotes: 0
Views: 1431
Reputation: 306
I've notice the mSectionsPagerAdapter.getItem(position) returns a new instance of the fragment which results in the variable always being null.
I've manually instantiated each fragment in the constructor and return them accordingly.
public class SectionsPagerAdapter extends FragmentPagerAdapter {
String[] classes;
Fragment[] fragments;
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
classes = new String[]{CellView.class.getName(),
ControlPanelView.class.getName()};
fragments = new Fragment[classes.length];
for (int i = 0; i < classes.length; i++) {
fragments[i] = Fragment.instantiate(context, classes[i]);
}
}
@Override
public Fragment getItem(int position) {
//Used to be this line
//return Fragment.instantiate(context, classes[i])
return fragments[position];
}
Upvotes: 1
Reputation: 734
Guess it's due to the fragments lifecycle inside the ViewPager.
The ViewPager has a limit of pages that are active, being those the current one and it's closer fragments (e.g: if the limit is 3, you will have active the current fragment shown, it's next and it's previous). The rest will got through the onPause() method.
The same happens when resuming a fragment. When you bring a fragment to the front, the fragments in that ranged that aren't active will go through the onResume method.
Depending on the number of fragments you have, you can think in different solutions.
If you have two or three fragments, change the offsetLimit of the viewPager to contain all of them.
viewPager.setOffscreenPageLimit(3);
If you have more fragments, think about saving it's state on the onPause() method, and restoring it on the onResume() of each fragment.
Upvotes: 1