My app gets crashed frequently when it goes from background to foreground. Scenerio: Suppose iam playing any games and my app is in recent list and after playing,if i select
In my case I have created several custom components such as drop drow, Textinput, etc. all of them have some of the same structure as the same title, mandatory sign. all of the ids are the same and it wasn't a big issue at the moment as they were in different layouts.
but then it happens to occur this error
java.lang.ClassCastException: android.view.View$BaseSavedState cannot be cast to androidx.appcompat.widget.AppCompatSpinner$SavedState
then I change the all custom component inner components id to different. then the problem was solved to me.
This can also happen in this edge case:
If you create a custom view programmatically inside the constructor of another parent view that has the AttributeSet parameter:
public ToggleButtonDescriptive(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
this.toggleButton = new SquareToggleButton(ctx, attributeSet);
}
DO NOT pass the AttributeSet to that child View:
toggleButton = new SquareToggleButton(ctx, attributeSet);
As passing the AttributeSet causes the child view to have the same id as the parent and thus Android tries to restore the parents SavedState to that child view or vice versa. Instead omit the AttributeSet parameter altogether, like this:
toggleButton = new SquareToggleButton(ctx);
Problem Fixed in my case:
Issue in my case is :
1:I have an id in xml which have same name as a layout.
ie:in my case ,i have a custom action bar layout named as "action_bar.xml" and an id in another layout as "+id/action_bar".So this cause problem when app is not in the memory and while recreating that page .
NOTE:DONT USE SAME ID/LAYOUT NAMES in more than one time in the app.
In my case i have layout in my screen with ChipGroup and chips without id's. Then if I return to this screen from other's - I got this error "java.lang.ClassCastException: android.view.AbsSavedState$1 cannot be cast to android.widget.CompoundButton$SavedState". So just added id's to chip's and all works fine.