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

Fix crash when switching back to the app after Permission change #3032

Conversation

vatsalyagoel
Copy link
Contributor

@vatsalyagoel vatsalyagoel commented Jul 25, 2018

when changing permission and going back to the application, ViewAssemblies is null thus causes the app to crash during setup

✨ What kind of change does this PR introduce? (Bug fix, feature, docs update...)

Bugfix

⤵️ What is the current behavior?

When Android forms application is running and you change app permissions when going into Android settings, once you switch back to the app, the app crashes as ViewAssemblies is null. s

🆕 What is the new behavior (if this is a feature change)?

I saw that we have a get view assemblies method which we probably should use instead of referenceing ViewAsemblies directly

💥 Does this PR introduce a breaking change?

No

🐛 Recommendations for testing

Launch App
Go to app Info without closing the app
Enable all permissions available
Go back to app in task switcher
Switch back to permissions
Disable a permission
Switch back to app
App Crashes with error

System.Linq.Enumerable.TryGetFirst[TSource] (System.Collections.Generic.IEnumerable`1[T] source, System.Boolean& found) [0x0000d] in <908c8d3ddc3a406497c942eef2cea13b>:0 
at System.Linq.Enumerable.FirstOrDefault[TSource] (System.Collections.Generic.IEnumerable`1[T] source) [0x00000] in <908c8d3ddc3a406497c942eef2cea13b>:0 
at MvvmCross.Forms.Platforms.Android.Core.MvxFormsAndroidSetup.get_FormsApplication () [0x00036] in <f9ca34d669a54607bed0e9bd0795a0d4>:0 
at MvvmCross.Forms.Platforms.Android.Core.MvxFormsAndroidSetup.CreateViewPresenter () [0x00006] in <f9ca34d669a54607bed0e9bd0795a0d4>:0 
at MvvmCross.Platforms.Android.Core.MvxAndroidSetup.get_Presenter () [0x00000] in <17df0d0bdae848b7a8a12b58d710f763>:0 
at MvvmCross.Platforms.Android.Core.MvxAndroidSetup.RegisterPresenter () [0x00000] in <17df0d0bdae848b7a8a12b58d710f763>:0 
at MvvmCross.Platforms.Android.Core.MvxAndroidSetup.InitializePlatformServices () [0x0000c] in <17df0d0bdae848b7a8a12b58d710f763>:0 at MvvmCross.Core.MvxSetup.InitializePrimary () [0x00052] in <17df0d0bdae848b7a8a12b58d710f763>:0 
at MvvmCross.Core.MvxSetupSingleton.StartSetupInitialization () [0x0000a] in <17df0d0bdae848b7a8a12b58d710f763>:0 at MvvmCross.Core.MvxSetupSingleton.EnsureInitialized () [0x00017] in <17df0d0bdae848b7a8a12b58d710f763>:0
 at MvvmCross.Forms.Platforms.Android.Views.MvxFormsAppCompatActivity.OnCreate (Android.OS.Bundle bundle) [0x0000c] in <f9ca34d669a54607bed0e9bd0795a0d4>:0

📝 Links to relevant issues/docs

#2969
Refer https://stackoverflow.com/questions/33488589/android-marshmallow-dynamic-permission-change-kills-all-application-processe
This also fixes the issue when launching the app from MvxIntentService

🤔 Checklist before submitting

when changing permission and going back to the application, ViewAssemblies is null thus causes the app to crash during setup
@nickrandolph
Copy link
Contributor

Please can we address the issue of why ViewAssemblies is null - this is there as an optimisation so that that GetViewAssemblies (which could be an expensive call) doesn't have to be called repeatedly.

@vatsalyagoel
Copy link
Contributor Author

When we have MvxFormsSplashScreenActivity<Setup, Core.App, FormsApp> and MvxFormsAppCompatActivity and RegisterSetupType<TMvxSetup> is only provided in Splash Screen, when launching the app directly form MainActivity where the MvxSetupType is not provided, ViewAssemblies end up being null

protected MvxFormsAppCompatActivity()
{
    RegisterSetup();
    BindingContext = new MvxAndroidBindingContext(this, this);
    this.AddEventListeners();
}

        protected virtual void RegisterSetup()
        {
        }

RegisterSetupType<TMvxSetup>(params Assembly[] assemblies) where TMvxSetup : MvxSetup, new() is never called which leads to ViewAssemblies being null. I'll update the PR so that we still have the performance

@nickrandolph
Copy link
Contributor

I know this code was already there but the fallback to GetType().Assembly concerns me - it's less likely that this will resolve to the correct assembly now. It's no longer required to override Setup, which means that GetType().Assembly will resolve to the MVX assembly, rather than the application assembly

@vatsalyagoel
Copy link
Contributor Author

vatsalyagoel commented Jul 26, 2018

I do have another suggestion which may require a bit of architecture change. How about making the splash screen just a splash screen with no setup needed and moving the setup to AppCompatActivity for which the code already exists.

This way the whole starting the app from a different screen would make sense. However MvxIntentService issue won't be solved as setup.ensureinitialized still won't have the right assemblies

@nickrandolph
Copy link
Contributor

No architecture change required. If you want you can use a vanilla splash screen today with mvx and just load mvx when you reach the first activity. But that defeats the whole point - which is to have splash shown whilst you setup mvx

@nickrandolph
Copy link
Contributor

@vatsalyagoel do the repro steps in the description work with the Playground sample app? I'm concerned that ViewAssemblies is null as this shouldn't really happen

@vatsalyagoel
Copy link
Contributor Author

Hi @nickrandolph I tested the issue in playground and it does happen there as well

@nickrandolph nickrandolph added this to the 6.2.0 milestone Aug 7, 2018
@nickrandolph nickrandolph changed the title Fix app crash [WIP] Fix app crash Aug 7, 2018
@nickrandolph nickrandolph added t/bug Bug type p/android Android platform p/forms Xamarin.Forms platform labels Aug 7, 2018
@martijn00
Copy link
Contributor

I tested this in one of my own apps, and can confirm that i both see the same issue, and that this fixes it. I'm not 100% sure this should be the longer term fix, but for now it works.

@vatsalyagoel vatsalyagoel changed the title [WIP] Fix app crash Fix app crash Aug 7, 2018
@Cheesebaron Cheesebaron changed the title Fix app crash Fix when switching back to the app after Permission change Aug 7, 2018
@Cheesebaron Cheesebaron changed the title Fix when switching back to the app after Permission change Fix crash when switching back to the app after Permission change Aug 7, 2018
@JelleDamen
Copy link
Contributor

I've seen this in a project of mine, but in a different scenario (direct on startup)
I've worked around it by implementing the 'ExecutableAssembly' property as follows:

public override Assembly ExecutableAssembly => ViewAssemblies?.FirstOrDefault() ?? GetType().Assembly;

instead of

public override Assembly ExecutableAssembly => ViewAssemblies.FirstOrDefault() ?? GetType().Assembly;

because during setup the ViewAssemblies are not loaded yet.

@vatsalyagoel
Copy link
Contributor Author

@JelleDamen That's what we ended up doing in this PR, However I agree with @martijn00 and @nickrandolph This is not a good long term fix as we don't exactly know what gettype().Assembly will give us. It may give us the Mvx or even Mono Droid assembly.

@JelleDamen
Copy link
Contributor

@vatsalyagoel Ah I see. Any clue why the ViewAssemblies are not loaded, I've not looking into it yet

@vatsalyagoel
Copy link
Contributor Author

vatsalyagoel commented Aug 7, 2018

@JelleDamen Please have a read through this thread. It explains how viewassemblies are setup and loaded and why in particular scenarios it is not actually loaded properly hence ending up being null

@nickrandolph nickrandolph modified the milestones: 6.2.0, 6.3.0 Aug 8, 2018
@martijn00 martijn00 merged commit 79f2f08 into MvvmCross:develop Aug 14, 2018
@martijn00 martijn00 modified the milestones: 6.3.0, 6.2.0 Aug 14, 2018
@martijn00
Copy link
Contributor

Let's merge this, and look further into the problem for 6.3.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
p/android Android platform p/forms Xamarin.Forms platform t/bug Bug type
Development

Successfully merging this pull request may close these issues.

4 participants