question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Calling of `Bridge.restoreInstanceState(this, savedInstanceState)` without checking `savedInstanceState != null` could cause accidental removal of saved sharedPreferences

See original GitHub issue

The 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:closed
  • Created 5 years ago
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
byenchocommented, Oct 1, 2018

@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 few Activity / Fragment; the thought was that all screens would use Bridge 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 inside restoreInstanceState (which might not be called for every Activity / Fragment in the scenario you’ve described) I can just do the check inside the ActivityLifecycleCallbacks.onActivityCreated callback, which will be triggered for every Activity (not just those that use Bridge). So I think this is a fix I can incorporate soon.

Thanks for helping find this issue!

0reactions
byenchocommented, Oct 11, 2018

@elye I’m going to close this issue now. If you still seem to have problems, feel free to re-open it.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found