Click a notification to open a new temporary activity page and then return to the main activity (Xamarin Android)

家住魔仙堡 提交于 2021-01-29 09:11:14

问题


This question is related to my previous question. Now, the method SendNofitication is added. I have two activities:

[Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = true)]
public class MainActivity : AppCompatActivity
{
    DateTime time = DateTime.Now;
    void SendNofitication(string title, string text)
    {
        TaskStackBuilder stack_builder = TaskStackBuilder.Create(this);
        stack_builder.AddParentStack(Java.Lang.Class.FromType(typeof(Activity2)));
          Intent intent = new Intent(this, typeof(Activity2));
        stack_builder.AddNextIntent(intent);

        PendingIntent pending_intent = stack_builder.GetPendingIntent(0, PendingIntentFlags.UpdateCurrent);
        Android.Support.V4.App.NotificationCompat.Builder builder = new Android.Support.V4.App.NotificationCompat.Builder(this);
        builder.SetAutoCancel(true);
        builder.SetContentIntent(pending_intent);
        builder.SetContentTitle(title);
        builder.SetSmallIcon(Resource.Drawable.icon1);
        builder.SetContentText(text);
        NotificationManager notification_manager = (NotificationManager)GetSystemService(Context.NotificationService);
        notification_manager.Notify(1000, builder.Build());
    }
    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        Xamarin.Essentials.Platform.Init(this, savedInstanceState);
        SetContentView(Resource.Layout.activity_main);

        var button = FindViewById<Button>(Resource.Id.button);
        button.Text = "Button created at " + time.ToString();
        button.Click += (s, e) =>
        {
             /*Intent intent = new Intent(this, typeof(Activity2));
             StartActivity(intent);*/
            SendNofitication("title", "message");
        };
    }
    public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
    {
        Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);
        base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
    }
}

and:

[Activity(Label = "Activity2")]
public class Activity2 : Activity
{
    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        SetContentView(Resource.Layout.activity2);

        Button button_close = FindViewById<Button>(Resource.Id.button_close);

        button_close.Click += (s, e) =>
        {
            Finish();
        };
    }
}

Here, I have a similar problem: Clicking on button shows an android notification. Clicking on the notification opens a new instance of Activity2 page and hides the existing instance of MainActivity page. button_close closes Activity2 and but it also closes the app entirely! However, I can reactivate the app in the Android Emulator by finding and clicking it in minimized apps list.

By clicking on close_button I want to return to the previous instance of MainActivity page. How can I do that? Or what's wrong in my SendNotification code that causes the app to close?

来源:https://stackoverflow.com/questions/64784717/click-a-notification-to-open-a-new-temporary-activity-page-and-then-return-to-th

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!