Android View onSaveInstanceState not called

后端 未结 2 1350
野趣味
野趣味 2021-02-07 10:06

I am working with CustomView which extends some Android view like FrameLayout. In my layout I use a ViewPager with a custom PagerAdapter.

The problem is that my

2条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-02-07 10:35

    The easy answer : it's because I was not setting id to my view (custom or not). Android didn't manage the onSaveInstanceState/onRestoreInstanceState if no id is set to the view.

    So as my customView was extending base View, with not extra property, setting an ID to the view solved the issue and so onSaveInstanceState/onRestoreInstanceState are called as it should.

    So to summary, use one of this approach :

    from XML

    
    
    

    OR from Java

    yourCustomView.setId(R.id.myCustomView);
    

    is last case, you add static id to res/values/ids.xml

提交回复
热议问题