admin管理员组

文章数量:1123509

I am familiar with onSaveInstanceState/onRestoreInstanceState in fragments and it works perfectly for saving data when activity has been destroyed.

I have UI process, where user should pick up the phone and he would hear security code, which should enter in android application.

But, on some devices (very rarely) my app is destroyed during a telephone conversation. After restore, of course, fragment has been restored, but my okhttp session has been cleared (mainly my backend session id). So after restore I can't finish my confirmation process. Can I save this data in bundle saved in onSaveInstanceState? Or is it any better idea for deal with this issue?

I am familiar with onSaveInstanceState/onRestoreInstanceState in fragments and it works perfectly for saving data when activity has been destroyed.

I have UI process, where user should pick up the phone and he would hear security code, which should enter in android application.

But, on some devices (very rarely) my app is destroyed during a telephone conversation. After restore, of course, fragment has been restored, but my okhttp session has been cleared (mainly my backend session id). So after restore I can't finish my confirmation process. Can I save this data in bundle saved in onSaveInstanceState? Or is it any better idea for deal with this issue?

Share Improve this question asked 19 hours ago AdamNAdamN 5861 gold badge5 silver badges13 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Yes, you can save your session data in the Bundle passed to onSaveInstanceState. However, it's not an ideal solution for data like backend session IDs, which may be more sensitive or have specific lifetimes independent of the Android app's lifecycle. Here's a detailed breakdown of potential solutions:

Using onSaveInstanceState

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putString("SESSION_ID", sessionId); 
}

@Override
public void onViewStateRestored(Bundle savedInstanceState) {
    super.onViewStateRestored(savedInstanceState);
    if (savedInstanceState != null) {
        sessionId = savedInstanceState.getString("SESSION_ID");
    }
}

本文标签: android okHttponRestoreInstanceStateStack Overflow