0

I get the following Exception:

Java.Lang.IllegalStateException: The specified child already has a parent. You must call removeVeiw() on the child's parent fist.

when running the following code blocks. It all starts in my Forms Page with a button press that uses the alarm manager to generate a scheduled notification. The problem is that if the notification is clicked on, the above exception gets thrown if the app is still active.

If I switch applications on the phone, then click the notification, it brings the application back up as intended without the crash.

Below are the code snippets in question for this workflow:

from xamarin page:

 private void BtnStartJob_Clicked(object sender, EventArgs e)
    {
        lblStatus.Text = "";
        if (btnStartJob.Text == "Start Job")
        {
            tmrToggle.StartCommand.Execute(null);
            App.Manager.StartJob(App.Manager.currentTimesheet.ProjectID);
            btnStartJob.Text = "Start Break";
        }
        else if (btnStartJob.Text == "End Break")
        {
            tmrAlert.PauseCommand.Execute(null);
            tmrToggle.StartCommand.Execute(null);
            App.Manager.EndBreak(App.Manager.currentTimesheet.TimesheetID);
            btnStartJob.Text = "Start Break";
            var notificationService = DependencyService.Get<INotificationService>();
            notificationService.CancelNotification();
        }
        else if (btnStartJob.Text == "Start Break")
        {
            tmrAlert.StartCommand.Execute(null);
            tmrToggle.PauseCommand.Execute(null);
            App.Manager.StartBreak(App.Manager.currentTimesheet.TimesheetID);
            btnStartJob.Text = "End Break";

            // schedule the notification here.
            var notificationService = DependencyService.Get<INotificationService>();
            notificationService.CreateNotification("Take Action", "Your break started 1 second ago, please take action.", TimeSpan.FromSeconds(15).Ticks);
        }
    }

NotificationService

    public class NotificationService : INotificationService
{

    public void CancelNotification()
    {
        var alarmIntent = new Intent(Android.App.Application.Context, typeof(AlarmReceiver));            
        var pending = PendingIntent.GetBroadcast(Android.App.Application.Context, 0, alarmIntent, PendingIntentFlags.UpdateCurrent);

        var alarmManager = Android.App.Application.Context.GetSystemService("alarm").JavaCast<AlarmManager>();
        alarmManager.Cancel(pending);            
    }

    public void CreateNotification(string title, string message, long durationInTicks)
    {
        var duration = TimeSpan.FromTicks(durationInTicks);

        var alarmIntent = new Intent(Forms.Context, typeof(AlarmReceiver));
        alarmIntent.PutExtra("title", title);
        alarmIntent.PutExtra("message", message);

        var pending = PendingIntent.GetBroadcast(Android.App.Application.Context, 0, alarmIntent, PendingIntentFlags.UpdateCurrent);

        var alarmManager = Android.App.Application.Context.GetSystemService("alarm").JavaCast<AlarmManager>();
        alarmManager.Set(AlarmType.ElapsedRealtime, duration.Milliseconds, pending);
    }
}    

Alarm Receiver:

    [BroadcastReceiver]
class AlarmReceiver : BroadcastReceiver
{
    public override void OnReceive(Context context, Intent intent)
    {
        var message = intent.GetStringExtra("message");
        var title = intent.GetStringExtra("title");

        var resultIntent = new Intent(context, typeof(MainActivity));
        resultIntent.SetFlags(ActivityFlags.NewTask | ActivityFlags.ClearTask);

        var pending = PendingIntent.GetActivity(context, 0,
            resultIntent,
            PendingIntentFlags.CancelCurrent);

        var builder =
            new Notification.Builder(context)
                .SetContentTitle(title)
                .SetContentText(message)                    
                .SetSmallIcon(Resource.Drawable.WESSUClogo)
                .SetDefaults(NotificationDefaults.All);

        builder.SetContentIntent(pending);

        var notification = builder.Build();

        var manager = NotificationManager.FromContext(context);
        manager.Notify(1337, notification);
    }
}

MainActivity: global::Xamarin.Forms.Platform.Android.FormsApplicationActivity {

    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);

        global::Xamarin.Forms.Forms.Init(this, bundle);
        LoadApplication(new App());                        
    }
}

Any suggestions on how to resolve? For me the biggest question is what object do I need to call removeView() on? This exception is thrown on the LoadApplication(newApp()); line in my MainActivity.

Since a full stack trace is really long, click here for the pastebin.

Nebri
  • 773
  • 2
  • 8
  • 21
  • Any chance you could provide a full stack trace? – pushasha Apr 08 '16 at 16:44
  • you did not your AlarmReceiver - you posted the NotificationService twice. And it would be helpful if you could tell us which specific line is causing the exception – Jason Apr 08 '16 at 16:48
  • @Jason your quite right, corrected the alarm code block in the question. If you read the post more carefully you will clearly see that I state the exception is thrown on the LoadApplication(newApp()); call in my MainActivity.cs – Nebri Apr 08 '16 at 16:52

1 Answers1

0

Discovered the answer myself.

inside the AlarmReceiver all I need to do was this:

resultIntent.SetFlags(ActivityFlags.Singletop);

Nebri
  • 773
  • 2
  • 8
  • 21