Calling of `Bridge.restoreInstanceState(this, savedInstanceState)` without checking `savedInstanceState != null` could cause accidental removal of saved sharedPreferences
See original GitHub issueThe bridgeDelegate has the below functions
void restoreInstanceState(@NonNull Object target, @Nullable Bundle state) {
boolean isFirstRestoreCall = mIsFirstRestoreCall;
mIsFirstRestoreCall = false;
if (state == null) {
if (isFirstRestoreCall) {
mSharedPreferences.edit()
.clear()
.apply();
}
return;
}
// ... other codes
}
With this, we are suppose to call the restoreInstanceState
without checking the savedInstanceState
to help clear the sharedPreference, as we assume this is the state where it doesn’t have state restoration needed.
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
Bridge.restoreInstanceState(this, savedInstanceState)
// .... other codes
}
However, there could be chance where isFirstRestoreCall
is true, and savedInstanceState
is also null, but it is actually in a restored state. This would cause accidental removal of all saved and needed sharedPreference
This issue is further describe in https://medium.com/@elye.project/handling-transactiontoolargeexception-with-livefront-bridge-a846459420bb, the Uses restoreInstanceState to help clear the data is called without StateRestoration
section
Issue Analytics
- State:
- Created 5 years ago
- Comments:6 (5 by maintainers)
Top Results From Across the Web
Handling TransactionTooLargeException with LiveFront:Bridge
This looks good, so we could just always called restoreInstanceState without need to check savedInstanceState != null i.e.
Read more >Android : savedInstanceState null when launching application
And this way i can close my application and relaunch it without losing the saved data. It's obvious when we know the correct...
Read more >savedInstanceState always remains null after restart #10
savedInstanceState will always remain null after the app is killed. It is not passed on to the bundle. public void onCreate(Bundle savedInstanceState) {...
Read more >Diff - platform/development - Google Git
public void onPostCreate(Bundle savedInstanceState) { - } - - /** - * Action bar helper code to be run in {@link Activity#onCreateOptionsMenu(android.view.
Read more >The Android Developer's Cookbook: Building Applications with the ...
Each can lead to different and unpredictable behavior, but test- ... public void onRestoreInstanceState(Bundle savedInstanceState) {.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@elye OK yeah I can understand the scenario you are talking about now. Thanks for clarifying that. As mentioned above, I wasn’t really expecting people to “selectively” use
Bridge
in just a fewActivity
/Fragment
; the thought was that all screens would useBridge
by just placing the calls in a base class. Because there is no requirement to do so, though, I can see how this issue can pop up for some people unexpectedly and is something I should fix.Fortunately there is a pretty easy fix for this : rather than doing the
isFirstRestoreCall
check insiderestoreInstanceState
(which might not be called for everyActivity
/Fragment
in the scenario you’ve described) I can just do the check inside theActivityLifecycleCallbacks.onActivityCreated
callback, which will be triggered for everyActivity
(not just those that useBridge
). So I think this is a fix I can incorporate soon.Thanks for helping find this issue!
@elye I’m going to close this issue now. If you still seem to have problems, feel free to re-open it.