0

Viewpager doesn't save it's fragments state if a fragment contains a textview which has custom font set to it. How do I prevent this?

public class SansFontTextView extends TextView {
public SansFontTextView(Context context) {
    super(context);
    setUpTypeFace();
}

public SansFontTextView(Context context, AttributeSet attrs) {
    super(context, attrs);
    setUpTypeFace();
}

public SansFontTextView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    setUpTypeFace();
}

private void setUpTypeFace(){

    Typeface openSansBold = Typeface.createFromAsset(getContext().getAssets(),  "fonts/OpenSans-Bold.ttf");

    setTypeface(openSansBold);
}
}

1 Answers1

0

You have not instructed what to do with your custom component, when system saves their state, i.e. you haven't specified what to save in onSavedInstanceState() and what to restore in onRestoreInstanceState().

If your custom view wants to save that information it should override those callback in order to survive destruction. Otherwise your custom TextView will be saved as an ordinary TextView (i.e. only super.onSavedInstanceState() will be called), and your properties that you have defined in your custom class will be lost.

See how to do that in this answer.

Community
  • 1
  • 1
azizbekian
  • 60,783
  • 13
  • 169
  • 249