Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ShowNestedFragment throws exception when host fragment is not visible #2442

Closed
1 of 7 tasks
dpnolte opened this issue Dec 6, 2017 · 1 comment
Closed
1 of 7 tasks
Labels
p/android-support Android Support Packages platform s/wont-fix Won't fix status

Comments

@dpnolte
Copy link

dpnolte commented Dec 6, 2017

In the method 'ShowNestedFragment' in the 'MvxAppCompatViewPresenter', an exception is thrown when the host fragment is not visible (lines 242-243):
if (!fragmentHost.IsVisible) throw new InvalidOperationException($"Fragment host is not visible when trying to show View {view.Name} as Nested Fragment");

Steps to reproduce 📜

  1. Create an activity A, create two fragments (B & C). The activity A hosts fragment B, and fragment B hosts fragment C.

  2. Activity has AppCompActivty as base, and uses the default MvxAppCompatViewPresenter. Fragment B is declared with [MvxFragmentPresentation] attribute and has Activity A as the ActivityHostViewModelType. Fragment C is declared with [MvxFragmentPresentation] attribute with Fragment B as the fragmentHostViewType .

  3. In the view models, by using the navigation service:
    -Navigate fragment B in ViewAppeared override in Activity A's viewmodel.

  • Navigate to fragment C in ViewAppeared override in Fragment B's viewmodel.
    ViewAppeared are mapped to the activity's/fragment's onResume.

My reason for setting it up this way is that I can have as much control of the navigation in the view models for code reusability.

Expected behavior 🤔

I expected Fragment C to be displayed.

Actual behavior 🐛

Instead, Fragment C was not shown. There is an exception thrown in ShowNestedFragment, because the host fragment B was not visible.
My understanding is that a host fragment will not yet be visible (on the foreground), when initializing fragment B & C together. The OnResume event occurs before the window has focus.
Google also states:

onWindowFocusChanged
void onWindowFocusChanged (boolean hasFocus)
Called when the current Window of the activity gains or loses focus. This is the best indicator of whether this activity is visible to the user. The default implementation clears the key tracking state, so should always be called.
Note that this provides information about global focus state, which is managed independently of activity lifecycles. As such, while focus changes will generally have some relation to lifecycle changes (an activity that is stopped will not generally get window focus), you should not rely on any particular order between the callbacks here and those in the other lifecycle methods such as onResume().

It took me a while to figure this out. I have got it working now by creating a custom view presenter. In this presenter, I override the the ShowNestedFragment method and do not preform the IsVisisbility check.

Is there a reason that you are checking against IsVisibility?

Configuration 🔧

Version: 5.2.*

Platform:

  • 📱 iOS
  • 🤖 Android
  • 🏁 WPF
  • 🌎 UWP
  • 🍎 MacOS
  • 📺 tvOS
  • 🐒 Xamarin.Forms
@nmilcoff nmilcoff added the p/android-support Android Support Packages platform label Dec 8, 2017
@nmilcoff
Copy link
Contributor

nmilcoff commented Dec 8, 2017

It could be the case that you have a Fragment with two different MvxFragmentPresentationAttributes, both for nested presentation. Then we use IsVisible to decide which one is the more appropiate to use.

We can't do much here as this is more of an Android-related problem.

I guess your options are:

  1. Set a Task.Delay and wait until the view appears
  2. Perform the nested navigation from a different place
  3. Override the ViewPresenter method and remove that condition if that works for your app.

@nmilcoff nmilcoff closed this as completed Dec 8, 2017
@nmilcoff nmilcoff added the s/wont-fix Won't fix status label Dec 8, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
p/android-support Android Support Packages platform s/wont-fix Won't fix status
Development

No branches or pull requests

2 participants