简体   繁体   中英

Xamarin.Android app resumed by shortcut intent - Master and Detail must be set before adding MasterDetailPage to a container

I have Xamarin.Android todo list mobile app using Prism.

The problem is:

  • In android system, I can create shortcut to open specific list in app.
  • When I open app, and press home button, it remains on background (thats ok)
  • When I then run app from desktop shortcut, it opens android activity and when I create new PrismApplication ( LoadApplication(new App()); ) everything is running OK, but after creating viewmodel for view, app is still using old viewmodel from before.

I made this workaroud and I use same instance of PrismApplication:

static App xamApp;

protected override void OnCreate(Bundle bundle)
{
    if (xamApp == null)
    {
        Forms.Init(this, bundle);
        xamApp = new App();
    }

    LoadApplication(xamApp);
    xamApp.Redirect(Intent.GetStringExtra("ListID"));
}

Now, problem is redirecting. This code:

public void Redirect(string listId)
{
    NavigationService.NavigateAsync($"MainPage/MainNavigationPage/TodoList?id={listId}", animated: false);
}

leads to the error:

System.InvalidOperationException: Master and Detail must be set before adding MasterDetailPage to a container.

Prism should take care of Binding of Detail in MasterDetailPage by the "TodoList" from NavigateAsync uri.

Does enyone know what can be the problem here?

So I finally got it working.

  • First I used LaunchMode = LaunchMode.SingleTask in my ActivityAttribute of MainActivity

    [Activity(Label = "..", LaunchMode = LaunchMode.SingleTask , Icon = "@drawable/icon", Theme = "@style/MainTheme", MainLauncher = true]

    public class MainActivity : FormsAppCompatActivity

  • Then I used OnNewIntent method of FormsAppCompatActivity so after app is on backgroud, only this event is launched :

protected override void OnNewIntent(Intent intent)
{
    var listId = intent.GetStringExtra("ListID");
    ((App)App.Current).Redirect(listId);
}
  • Now even $"MainNavigationPage/TodoList?id={listId}" works

Based on the info you provided, I am assuming that when the app is launched again, it is already running, your previous MasterDetail page is already on the stack. IN your reset method, you want to reset your navigation stack to the new uri passing in the parameter. IN this case, you should use an absolute uri. This means try adding a "/" prefix to your uri. So something like this:

public void Redirect(string listId)
{
    NavigationService.NavigateAsync($"/MainPage/MainNavigationPage/TodoList?id={listId}", animated: false);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM