微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

点击通知以打开新的临时活动页面,然后返回至主要活动Xamarin Android

如何解决点击通知以打开新的临时活动页面,然后返回至主要活动Xamarin Android

此问题与my previous question有关。现在,添加方法SendNofitication我有两个活动:

[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,grantResults);
    }
}

和:

[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();
        };
    }
}

在这里,我遇到类似的问题:单击button显示一个Android通知。单击通知会打开Activity2页面的新实例,并隐藏MainActivity页面的现有实例。 button_close关闭Activity2,但它也完全关闭该应用程序!但是,我可以通过在最小化的应用程序列表中找到并单击它来重新激活Android Emulator中的应用程序。

通过单击close_button,我想返回到MainActivity页面的上一个实例。我怎样才能做到这一点?还是我的SendNotification代码中导致应用程序关闭的问题是什么?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。