简体   繁体   English

每个控制器在方向更改时调用onSavedInstance()吗?

[英]Every controller calls onSavedInstance() on orientation changes?

I have used conductor library to control my views. 我已经使用导体库来控制我的视图。 It was designed in only one orientation now I trying to make it in both. 现在,我只尝试在两个方向上进行设计。

I have used onSaveInstanceState() to save the state of my views. 我使用过onSaveInstanceState()保存视图状态。 I found out that every time I change the controller and rotate the screen then onSavedInstance() on both controllers is called which crash my application. 我发现,每次更换控制器并旋转屏幕时,就会调用两个控制器上的onSavedInstance(),这会使我的应用程序崩溃。 How can I save instance of the current controller shown on the screen. 如何保存屏幕上显示的当前控制器的实例。

Application Flow A -> B -> C BaseController First Screen A-Controller- Screen Rotated(onSavedInstance() on controller A is called ) 应用程序流A-> B-> C BaseController第一个屏幕A-Controller-屏幕旋转(称为控制器A上的onSavedInstance())

Second Screen (Navigate from A) B- Contoller- Screen Rotated(onSavedInstance() on controller A and B is called ) 第二个屏幕(从A导航)B- Contoller-旋转的屏幕(称为控制器A和B上的onSavedInstance())

Third Screen (Navigate from B) C- Controller - Screen Rotated(onSavedInstance() on controller A, B and C is called ) 第三屏幕(从B导航)C-控制器-屏幕旋转(称为控制器A,B和C上的onSavedInstance())

I have tried isAttached() to let only the shown controller save the instance but it is somehow only returning false 我尝试过isAttached()仅让显示的控制器保存实例,但是以某种方式仅返回false

    protected void onSaveInstanceState(@NonNull Bundle outState) {
        super.onSaveInstanceState(outState);
        if (isAttached()) {}

I found a solution to my problem. 我找到了解决问题的方法。 I do not know if it is the correct one but it might help. 我不知道它是否正确,但可能会有所帮助。 addChangeListner listen the change of controller change events. addChangeListner监听控制器更改事件的更改。 I created an abstract function in my BaseController which is called when addChange listener is called. 我在BaseController中创建了一个抽象函数,当调用addChange侦听器时会调用该抽象函数。 I have a boolean which is always true in every controller. 我有一个布尔值,在每个控制器中总是如此。 But when the controller is changed this abstract function I created is called and change the boolean value to false. 但是,当控制器更改时,将调用我创建的此抽象函数,并将布尔值更改为false。 Thus only the controller whose screen is attached saves the instance state. 因此,只有连接了屏幕的控制器才能保存实例状态。

`@Override
    protected void onSaveInstanceState(@NonNull Bundle outState) {
        super.onSaveInstanceState(outState);
        if (isAttached) {
            // State that I want to save 
        }
    }`

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

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