質問

I have a problem in a custom view. Its state is not saved after I turn off the screen and turn it back on.

onSaveInstanceState is called correctly when I turn the screen off, but when I turn the screen on, onRestoreInstanceState is not called. I understand that it's because the activity is not recreated, but I don't know what else to use to restore the state of the view.

I think I would need to do something at onStart() or onResume(), but I don't want to do this in the activity, but in the view, because the view is used in multiple activities and it would be very inconvenient.

I've tried several other methods, like onWindowsVisibilityChanged, but no luck.

What other method could I use?

Thank you.

EDIT: Details: what I want to keep is only a boolean to let me know if the button is toggled on or off, so I can change it's appearance. I know it should keep its appearance without my intervention, but the implementation is a little buggy.

役に立ちましたか?

解決

After some more trying, I managed to get the event I needed in onWindowFocusChanged(Boolean hasWindowFocus). As far as I'm concerned, it gives me the same opportunity as onResume(), because it's called just as the window becomes visible and is brought to front.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top