Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to close activity android using visual studio and xamarin

i have login, register, and home page on my project. I use StartActivity(typeof(Register));to open register page. When user already insert data and click register button, i use StartActivity(typeof(MainActivity)); to go back to login page again. When i click back button on my phone it back to register page>login page>then exit. I want my activity that already created is closed after i open a new page.

And my second question, i have exit button, how to close my app using the exit button?

I'm using Visual Studio 2015 and Xamarin for developing android app.

like image 943
neneo Avatar asked Apr 27 '16 08:04

neneo


People also ask

Which method closes the activity in Xamarin Android?

Calling Finish will close and kill the Activity and it will work as expected.

What is an activity in Xamarin?

Advertisements. When a user navigates through an Android App, a series of events occurs. For example, when a user launches an app, e.g., the Facebook App, it starts and becomes visible on the foreground to the user, onCreate() → onStart() → onResume().


2 Answers

Calling Finish will close and kill the Activity and it will work as expected. Better way to remove an Avtivity so that it won't appear when Back button is pressed will be to set the NoHistory of that Activity as true.

If you have a LoginActivity and a DashboardActivity and you don't want the LoginActivity to show while pressing back-button after logging in, you can set NoHistory of LoginActivity as true, like below.

[Activity (NoHistory = true)]
public class LoginActivity : Activity
{
}
like image 152
Sreeraj Avatar answered Sep 21 '22 06:09

Sreeraj


You can use Finish method to close your current activity:

StartActivity(typeof(MainActivity));
Finish();

To close the app, simply use

System.exit(0);
like image 35
Luis Beltran Avatar answered Sep 20 '22 06:09

Luis Beltran