简体   繁体   English

嵌套的RouterPagerAdapter没有在轮换时恢复状态

[英]Nested RouterPagerAdapter's not restoring state on rotation

The root controller, which has a RouterPagerAdapter , hosts controller A which has its own RouterPagerAdapter . 根控制器有一个RouterPagerAdapter ,它拥有一个控制器A,它有自己的RouterPagerAdapter

Controller A1 is hosted by controller A 控制器A1由控制器A托管

[ Root Controller ]
[   A   ] [   B   ]
[A1] [A2]

Upon rotation, Root Controller, A and A1 receive their onSaveInstanceState/onSaveViewState , but neither A nor A1 is receives it onRestoreInstanceState/onRestoreViewState . 在旋转时,根控制器,A和A1接收它们的onSaveInstanceState/onSaveViewState ,但A和A1都不接收onRestoreInstanceState/onRestoreViewState

Why is this so? 为什么会这样? What is the correct implementation to ensure that all children will have their state restored? 确保所有儿童恢复状态的正确实施是什么?

onSaveViewState will be called, but onRestoreViewState will not be called due to the viewPager.setAdapter(null); onSaveViewState将被调用,但onRestoreViewState不会因被要求在viewPager.setAdapter(null); line. 线。

The example in Conductor's docs sets the adapter to null when the view is being destroyed. 在销毁视图时,Conductor的文档中的示例将适配器设置为null。

Override protected void onDestroyView(@NonNull View view) {
    viewPager.setAdapter(null);
    super.onDestroyView(view);
  }

Because of this, RouterPagerAdapter will call configureRouter to which the router does not have a root controller, and will new Cont() 正因为如此, RouterPagerAdapter将调用configureRouter到该router 具有根控制器,并且将new Cont()

if (!router.hasRootController()) {
      router.setRoot(RouterTransaction.with(new Cont()));
    }

Also note, when nesting a viewPager within another, both should have unique ids, else on rotation will overwrite the save state of the parent viewPager , and will show the wrong current item. 另请注意,将viewPager嵌套在另一个内时,两者都应具有唯一ID,否则在旋转时将覆盖父viewPager的保存状态,并将显示错误的当前项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM