Forum Xamarin.Forms

How to clear intent after use

DomKapDomKap USMember ✭✭

My app showing notification on specific time. Notification has got pending intent with note id. When user tapped on notification, this note id is use to launch app and load note with id that come from notification.

Basically it works fine but problem starts when user leave app using "back button". On next application start that note id is still in memory and app takes user to that note again.

How can I clear that intent from memory after first use.

MainActivity.cs

public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
    {
        public static Context AndroidContext { get; private set; }
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            //noteId is a id of Note thet notification was dispaly and tapped by user
            //Id is pass to xamarin PCL and proper note basic on this id is displayed

            int noteId = Intent.GetIntExtra("noteId", 0);

            TabLayoutResource = Resource.Layout.Tabbar;
            ToolbarResource = Resource.Layout.Toolbar;   

            CalligraphyConfig.InitDefault(new CalligraphyConfig.Builder()
                   .SetDefaultFontPath("Dosis-Regular.ttf")
                   .SetFontAttrId(Resource.Attribute.fontPath)            
                   .Build()
                    );
            AndroidContext = this;
            global::Xamarin.Forms.Forms.Init(this, bundle);
            Xamarin.FormsGoogleMaps.Init(this, bundle);           

            LoadApplication(new App(noteId));    
        }

        protected override void AttachBaseContext(Context context)
        {
            base.AttachBaseContext(CalligraphyContextWrapper.Wrap(context));
        }
        public override void OnRequestPermissionsResult(int requestCode, string[] permissions, Permission[] grantResults)
        {
            PermissionsImplementation.Current.OnRequestPermissionsResult(requestCode, permissions, grantResults);
        }

    }

BroadcastReciver

    class AlamReciver : BroadcastReceiver
        {
            public override void OnReceive(Context context, Intent intent)
            {
                int noteId = intent.GetIntExtra("id",0);
                var noteTitle = intent.GetStringExtra("title");
                var noteContent = intent.GetStringExtra("content");

                var resultIntent = new Intent(context, typeof(MainActivity));      
                resultIntent.PutExtra("noteId", noteId);           

                resultIntent.SetFlags(ActivityFlags.ClearTop | ActivityFlags.ClearTask);

                var PI = PendingIntent.GetActivity(context, noteId, resultIntent, PendingIntentFlags.OneShot);           

                var builder = new Notification.Builder(context)
                    .SetContentTitle(noteTitle)
                    .SetContentText(noteContent)                
                    .SetSmallIcon(Resource.Drawable.ic_gnote_logo)
                    .SetPriority(1)               
                    .SetAutoCancel(true)
                    .SetDefaults(NotificationDefaults.All)
                    .SetContentIntent(PI);
                var notification = builder.Build();

                var manager = NotificationManager.FromContext(context);

                manager.Notify(noteId, notification); 
        }
    }

Thank you.

Sign In or Register to comment.