简体   繁体   中英

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

I am working with Xmarine c# anroid app. 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). 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. Now if we click back button from (Activity1) the app does not close and every time it repoens the landing activity.

Where as I need to close the app at the time of back button press of (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.

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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