[Android] Prevent NPE in browsers on device rotation

Geoffrey Métais geoffrey.metais at gmail.com
Tue Nov 14 11:37:35 CET 2017


These are 2 different case statements

First onCreate()
Then, onViewCreated() and onActivityCreated()

AFAIU.

But I don't mind refactoring and set adapter sooner for safety.

Le mar. 14 nov. 2017 à 11:15, Romain Vimont <rom at rom1v.com> a écrit :

>
>
> Le mardi 14 novembre 2017 à 10:04 +0000, Geoffrey Métais a écrit :
> >  onActivityCreated() is still called once the view are inflated, so it
> can
> > make a visual difference. That's the idea.
>
> The screen may not be refreshed between these two calls, they occur
> sequentially in the same event in the event loop:
>
>
> https://github.com/aosp-mirror/platform_frameworks_base/blob/oreo-release/core/java/android/app/FragmentManager.java#L1234
>
> https://github.com/aosp-mirror/platform_frameworks_base/blob/oreo-release/core/java/android/app/FragmentManager.java#L1296
>
> > But yes, creating adapters sooner is definitely safer.
> >
> > Le mar. 14 nov. 2017 à 10:56, Romain Vimont <rom at rom1v.com> a écrit :
> >
> > >
> > > Le mardi 14 novembre 2017 à  8:14 +0000, Geoffrey Métais a écrit :
> > > > The general idea was to offload onCreate(…) in order to have the
> > > > activities/fragments loaded faster.
> > >
> > > All these callbacks are called successively on the UI thread, so
> > > creating the adapter from one or another may not have any impact on
> > > "loading faster".
> > >
> > > The documentation of onActivityCreated() says:
> > >
> > > >  It can be used to do final initialization once these pieces are in
> > > >  place, such as retrieving views or restoring state.
> > >
> > > In my opinion, adapters should be created in onCreate(), so that they
> > > may not be null anywhere in your fragment/activity.
> > >
> > > What do you think?
> > >
> > > > I did not suspect onSaveInstanceState() could be called before
> > > > onActivityCreated(…)…
> > > >
> > > > Le lun. 13 nov. 2017 à 21:00, Romain Vimont <rom at rom1v.com> a écrit
> :
> > > >
> > > > >
> > > > >
> > > > > Le lundi 13 novembre 2017 à 18:45 +0100, Geoffrey Métais a écrit :
> > > > > > vlc-android | branch: 2.5.x | Geoffrey Métais <
> > > geoffrey.metais at gmail.com>
> > > > > | Mon Nov 13 18:40:44 2017 +0100|
> > > > > [758c200e10ddac5cbad406d4107b431efcdaa2e6] | committer: Geoffrey
> Métais
> > > > > >
> > > > > > Prevent NPE in browsers on device rotation
> > > > > >
> > > > > > (cherry picked from commit
> 45c45adbd5b2b8972fdc28a450333285fa878190)
> > > > > >
> > > > > > >
> > > > >
> > >
> https://code.videolan.org/videolan/vlc-android/commit/758c200e10ddac5cbad406d4107b431efcdaa2e6
> > > > > > ---
> > > > > >
> > > > > >
> > > vlc-android/src/org/videolan/vlc/gui/browser/BaseBrowserFragment.java |
> > > > > 3 ++-
> > > > > >  1 file changed, 2 insertions(+), 1 deletion(-)
> > > > > >
> > > > > > diff --git
> > > > >
> a/vlc-android/src/org/videolan/vlc/gui/browser/BaseBrowserFragment.java
> > > > >
> b/vlc-android/src/org/videolan/vlc/gui/browser/BaseBrowserFragment.java
> > > > > > index 2a1695b12..5aee19028 100644
> > > > > > ---
> > > > >
> a/vlc-android/src/org/videolan/vlc/gui/browser/BaseBrowserFragment.java
> > > > > > +++
> > > > >
> b/vlc-android/src/org/videolan/vlc/gui/browser/BaseBrowserFragment.java
> > > > > > @@ -245,7 +245,8 @@ public abstract class BaseBrowserFragment
> extends
> > > > > SortableFragment<BaseBrowserAd
> > > > > >      public void onSaveInstanceState(Bundle outState){
> > > > > >          outState.putString(KEY_MRL, mMrl);
> > > > > >          outState.putParcelable(KEY_MEDIA, mCurrentMedia);
> > > > > > -        VLCApplication.storeData(KEY_MEDIA_LIST+mMrl,
> > > > > mAdapter.getAll());
> > > > > > +        if (mAdapter != null)
> > > > >
> > > > > Why not just initializing mAdapter in onCreate(…)?
> > > > >
> > > > > > +            VLCApplication.storeData(KEY_MEDIA_LIST+mMrl,
> > > > > mAdapter.getAll());
> > > > > >          VLCApplication.storeData(KEY_CONTENT_LIST+mMrl,
> > > > > mFoldersContentLists);
> > > > > >          if (mRecyclerView != null)
> > > > > >              outState.putInt(KEY_POSITION,
> > > > > mLayoutManager.findFirstCompletelyVisibleItemPosition());
> > > > > >
> > > > > > _______________________________________________
> > > > > > Android mailing list
> > > > > > Android at videolan.org
> > > > > > https://mailman.videolan.org/listinfo/android
> > > > > _______________________________________________
> > > > > Android mailing list
> > > > > Android at videolan.org
> > > > > https://mailman.videolan.org/listinfo/android
> > > > >
> > >
> > > > _______________________________________________
> > > > Android mailing list
> > > > Android at videolan.org
> > > > https://mailman.videolan.org/listinfo/android
> > >
> > > _______________________________________________
> > > Android mailing list
> > > Android at videolan.org
> > > https://mailman.videolan.org/listinfo/android
> > >
>
> > _______________________________________________
> > Android mailing list
> > Android at videolan.org
> > https://mailman.videolan.org/listinfo/android
>
> _______________________________________________
> Android mailing list
> Android at videolan.org
> https://mailman.videolan.org/listinfo/android
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.videolan.org/pipermail/android/attachments/20171114/d8351d11/attachment.html>


More information about the Android mailing list