7

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.

Daniel Nugent
  • 43,104
  • 15
  • 109
  • 137
neneo
  • 646
  • 1
  • 8
  • 21

5 Answers5

11

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
{
}
Sreeraj
  • 2,306
  • 2
  • 18
  • 31
7

You can use Finish method to close your current activity:

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

To close the app, simply use

System.exit(0);
Luis Beltran
  • 1,704
  • 12
  • 13
  • Thanks it works, i use this code before but on fragment and it is not detected – neneo Apr 27 '16 at 09:55
  • For Fragments you need to use FragmentTransaction and use its Remove method. Please have a read here and don't hesitate to ask if you have a question about it: https://developer.xamarin.com/guides/android/platform_features/fragments/part_2_-_managing_fragments/ – Luis Beltran Apr 27 '16 at 10:38
2

To remove an activity from navigation you can use finish keyword like that :

[Activity(Label = "MainActivity", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity: Activity
{
    protected override async void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);

        var intent = new Intent(this, typeof(SecondActivity));
        intent.SetFlags(ActivityFlags.NewTask);
        //Navigation to SecondActivity
        StartActivity(intent);
        //delete main activity from navigation
        Finish();
    }
}

For the Second question you can use :

System.exit(0);

You have a very good explanation about this feature in this post for android that you can use for xamarin android : Close Android Application

Community
  • 1
  • 1
OrcusZ
  • 3,555
  • 2
  • 31
  • 48
  • Thanks bro it works now, now i need to think how implement this to fragment. Do you know how to use it within fragment? – neneo Apr 27 '16 at 09:57
2
StartActivity(typeof(nameOfActivity));
// add this line
Finish();
userlond
  • 3,632
  • 2
  • 36
  • 53
  • 1
    Please explain more about your answer. Merely posting the code might not help others. – Pirate X Jul 21 '16 at 05:52
  • Welcome to Stack Overflow! While this code snippet may solve the question, [including an explanation](//meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. Please also try not to crowd your code with explanatory comments, as this reduces the readability of both the code and the explanations! – Blue Jul 21 '16 at 08:32
1

You can't close previous activity in current activity. It only can be closed by itself.

But you can return data to previous activity. And in event handler OnActivityResult of previous activity, you can do close action.

This sample will be helpful for you. https://code.msdn.microsoft.com/How-to-close-activity-d51941c8

code below shows how to close previous activity.

In previous activity:

Intent intent = new Intent(this, typeof(RegisterActivity)); 

//for get result, we should use method StartActivityForResult 
//the second param is the request code, it is the ID of this request, it should be >= 0 
StartActivityForResult(intent, 1);

In current activity:

Intent intent = new Intent(this, typeof(RegisterActivity)); 
intent.PutExtra("result", "Success"); 

SetResult(Result.Ok, intent); 

Finish(); 
//when back to login activity, the OnActivityResult event will be trigger.

And go back to previous activity:

protected override void OnActivityResult(int requestCode, [GeneratedEnum] Result resultCode, Intent data) 
{ 
    base.OnActivityResult(requestCode, resultCode, data); 

    //when regester activity retrun data, it will be execute 

    if (requestCode == 1 && resultCode == Result.Ok) 
    { 
        string result = data.GetStringExtra("result"); 
        if (result == "Success") 
        { 
            Finish(); 
        } 
    } 
}

For your second question:

Just use this:

System.exit(0);
Alex Zhang
  • 1,070
  • 11
  • 15