简体   繁体   English

如何在重新打开同一活动时按后退按钮关闭应用程序?

[英]How to close app on back button press as it reopens the same activity?

I am working with Xmarine c# anroid app. 我正在使用Xmarine c#anroid应用程序。 Here I am unable to close the app on back button pressed. 在这里,我无法关闭按下后退按钮的应用程序。 In my app on the landing activity (Activity1) we will get 3 buttons, each button will open a new activity (inner1,inner2,inner3). 在我关于登陆活动(Activity1)的应用程序中,我们将获得3个按钮,每个按钮将打开一个新的活动(inner1,inner2,inner3)。 Now if we click back button from any of these 3 inner activity then it will come to the landing activity (Activity1).It is working fine. 现在,如果我们从这3个内部活动中的任何一个单击后退按钮,它将进入登陆活动(Activity1)。它工作正常。 Now if we click back button from (Activity1) the app does not close and every time it repoens the landing activity. 现在,如果我们单击(Activity1)中的“后退”按钮,则该应用程序不会关闭,并且每次都会重新提示着陆活动。

Where as I need to close the app at the time of back button press of (Activity1). 在按下(Activity1)的后退按钮时需要关闭应用程序的位置。 How to close app on back button press. 如何在按下后退按钮时关闭应用程序。

Here I have used the below mentioned code for performing closing activity from Activity1 back pressed. 在这里,我使用下面提到的代码从回按下的Activity1执行关闭活动。

Process.KillProcess(Process.MyPid());

Also I have used the below mentioned code for redirecting the Landing activity from all inner1, inner2, inner3 activity back press. 另外,我还使用了下面提到的代码来从所有的inner1,inner2,inner3活动返回键中重定向Landing活动。

public override bool OnKeyDown(Keycode keyCode, KeyEvent e)
        {
            if (e.KeyCode == Keycode.Back)
            {                
                // Transition your Fragments here
                var Activity = new Intent(this, typeof(Activity1));
                StartActivity(Activity);
                Finish();
            }

            return base.OnKeyDown(keyCode, e);
        }

You can use the below codes to exit from the app 您可以使用以下代码退出应用

this.Finish();
Android.OS.Process.KillProcess(Android.OS.Process.MyPid());
System.Environment.

如果您在第一个活动中使用意图,则不想定义后按方法,如果您单击后退按钮,则它会自动重定向到着陆活动

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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