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

Called the createViewState always on nested fragment. #74

Closed allsoft777 closed 9 years ago

allsoft777 commented 9 years ago

On fragment, the createViewState method is always called on nested fragments. So, it doesn't restore the view state and data when rotating the device. Could you check it out? As i see it, it could be called once when created the fragment.

sockeqwe commented 9 years ago

Do you have concrete source code or a demo project a can have a look at to reproduce this issue ?

allsoft777 commented 9 years ago

Hi~ Hannes.

I've attached the demo project.

If you have any time, please take a look at the project.

The issue is occuring on below path.

MonthlyReportFragmentItem.java

@Override public LceViewState<List, MonthlyReportPagerView<List>> createViewState() { return new MonthlyViewStateImpl<>(); }

​​Thanks!

​ ViewStateTest.zip https://drive.google.com/file/d/0BwbnfAfPxdhkUy1hYzJfRTZJdjg/view?usp=drive_web

2015-08-02 22:34 GMT+09:00 Hannes Dorfmann notifications@github.com:

Do you have a concrete source code or a demo project a can have a look at?

— Reply to this email directly or view it on GitHub https://github.com/sockeqwe/mosby/issues/74#issuecomment-127024441.

sockeqwe commented 9 years ago

thanks, I will have a look at it tomorrow

sockeqwe commented 9 years ago

I'm not 100% sure, but I don't think that your problem is Mosby related (I may be wrong).

I have added a demo to the official mosby samples to demonstrate that nested fragments are working properly.

Something strange happens in your app with the saved instance state bundle. The savedInstanceState Bundle of all of your fragments are always null after screen orientation changes which has two causes:

  1. Your Fragments don't have an ID or TAG
  2. You always create a new Fragment and replace the old one.

I think 2. is the case.

Your code is a little bit confusing at this point but I guess the problem is that you are replacing the old fragment with a new / or the same fragment retrieved from FragmentManager.findFragmentByTag() previously.

Have a look at MainActivity.onNavigationDrawerItemSelected():

FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.container, fragment, tag);
ft.addToBackStack(null);
ft.commitAllowingStateLoss();

Doesn't it replace the MonthlyReportFragment?

allsoft777 commented 9 years ago

Yes, you are right. The root cause was on NavigationDrawerFragment class. Whenever rotating the device, switching the main fragment is called always. I've fixed by modifying the framgment structure. Thanks.