sockeqwe / mosby

A Model-View-Presenter / Model-View-Intent library for modern Android apps
http://hannesdorfmann.com/mosby/
Apache License 2.0
5.49k stars 841 forks source link

Fragment MVI presenter attaches in onViewCreated(). Isn't better in onActivityCreated() ? #264

Closed sevar83 closed 6 years ago

sevar83 commented 7 years ago

Hi, I'm using Mosby 3.0.3 with MVI. Looking at the sample's SearchFragment

@Nullable @Override
  public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
      @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_search, container, false);
    unbinder = ButterKnife.bind(this, view);

    adapter = new SearchAdapter(inflater, this);
    recyclerView.setAdapter(adapter);
    recyclerView.setLayoutManager(new GridLayoutManager( ------->   getActivity()    <------ , spanCount));
    recyclerView.addItemDecoration(new GridSpacingItemDecoration(spanCount,
        getResources().getDimensionPixelSize(R.dimen.grid_spacing), true));

    return view;
  }

The problem is that getActivity() is not guaranteed to be non-null in all cases. I had some cases in the past when getActivity() was giving null in onCreateView() or onViewCreated() so I started to use onActivityCreated() to be 100% sure I've got a valid Activity instance. What if MviFragmentDelegateImpl attachment code is just moved to onActivityCreated(). Otherwise a render() call takes place even before Fragment.onActivityCreated(). Does not make sense. It's too early.

sockeqwe commented 7 years ago

MviFragments Lifecycle will be changed to onStart() and onStop() in next version. See #262

However, it sounds pretty strange that getActivity() is null. That should not be the case since onAttach() is called before onCreateView()

sockeqwe commented 7 years ago

Yes, 3.0.5-SNAPSHOT uses onStart() - onStop() but has an error with fragmente on the backstack. Its not reay for production yet.

Svetlozar Kostadinov notifications@github.com schrieb am So., 16. Juli 2017, 19:31:

Maybe I don't remember correctly. Probably I've encountered not a null getActivity() but a non-initialized instance. In the past I thought that onCreateView() is called after the activity is created. Is the fragment lifecycle change already implemented to try it out in the SNAPSHOT?

— You are receiving this because you commented.

Reply to this email directly, view it on GitHub https://github.com/sockeqwe/mosby/issues/264#issuecomment-315624041, or mute the thread https://github.com/notifications/unsubscribe-auth/AAjnrkcoZKuk4ogdOjc29D_29beoXlOXks5sOkkIgaJpZM4OZCjk .