I'm still new to Android (and coding) and this is my first time working with Swipe views, i.e. ViewPagers and adapters. My activity has 3 fragments. When I move from Fragment A to Fragment B, all is fine. But when I move to Fragment C, onStop
gets called for Fragment A. When I return to Fragment A, all of my views have been restored except for a few. The EditText
views are fine, but other views have problems...
- I have a couple of TextViews that I enable/disable. These states are not retained.
- Other TextViews can be visible/invisible. These states are not retained.
- I don't believe the TextView texts are being retained either.
It was my impression that onSaveInstanceState
saves views. Do I have to save these states manually? Since these are Fragments
, should that be done through arguments
?
Here is my Fragment onCreateView
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
rootView = inflater.inflate(
R.layout.activity_grade_calculator, container, false);
Bundle args = getArguments();
if(args.containsKey(PERSISTENT_FIRSTLOAD_BUNDLE_KEY)) {
firstLoad = args.getBoolean(PERSISTENT_FIRSTLOAD_BUNDLE_KEY);
}
context = this.getActivity();
if(firstLoad) {
bindAll();
setInitialViews();
setListeners();
}
firstLoad = false;
return rootView;
}
Here is MyPagerAdapter
@Override
public Fragment getItem(int i) {
//System.out.println("We are trying to get item " + i);
switch(i) {
case 0:
Fragment fragment = new FinalCalcFragment();
return fragment;
case 1:
Fragment fragment1 = new NewGradeCalcFragment();
return fragment1;
case 2:
Fragment fragment3 = new NewGradeCalcFragment();
return fragment3;
default:
return null;
}
}
Here are some TextView
s that are not behaving nicely.
<TextView
android:id="@+id/tv_grade_calc_forgrade"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/radioGroup1"
android:layout_marginTop="4dp"
android:text="@string/tv_grade_calc_forgrade" />
<TextView
android:id="@+id/tv_grade_calc_ifscore"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/radioGroup1"
android:layout_marginTop="4dp"
android:text="@string/tv_grade_calc_ifscore" />
<TextView
android:id="@+id/tv_grade_calc_result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@id/tv_grade_calc_ifscore"
android:layout_marginRight="10dp"
android:layout_marginTop="-7dp"
android:textSize="28sp" />
Edit: Btw, I've played around with both FragmentPagerAdapter
and FragmentStatePagerAdapter
. I don't fully understand what they do differently, but neither seem to give me what I want.