Now I move my c# {name}Activity.cs files which are in essence our code behinds into their own folder with their own namespace, by default they're are located in the root of the project which doesn't sit well with me.
Activities in Android are not started directly, it's something the ART does for you. to switch to an activity you must:
- package your activity request inside of an intent
- Using the context you send the intent to the ART
- ART looks inside the intent and decides which intent to start for you
To start an explicit activity
private void
StatsBUTTON_Click(object sender, EventArgs e)
{
var intent = new
Intent(packageContext: this, type: typeof(StatsActivity));
base.StartActivity(intent);
}
once you navigate to you target activity, make sure is to set the content view inside your activity to the xml markup of your view that's that axml file
using Android.App;
using Android.OS;
using Android.Widget;
namespace pav.TicTacToe
{
[Activity(Label = "StatsActivity")]
public class StatsActivity : Activity
{
Button CancelBUTTON;
protected override void
OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Set the view
from the resource.layout folder
SetContentView(Resource.Layout.activity_stats);
CancelBUTTON = FindViewById<Button>(Resource.Id.CancelBUTTON);
CancelBUTTON.Click +=(s,a)=> this.Finish();
}
}
}
notice that in my OnCreate(Bundle savedInstanceState) method after i call the base OnCreate method i set the content view to that of my axml file that contains the markup for my view. without this you'll just get a blank page.
now in many situations when navigating between activities we're going to want to pass arguments from one to the next and maybe even back, the arguments that are passed between activities must be serializable types .
one way to pass data between activities is to leverage the putExtra method inside the intent, this is the same as creating a bundle and adding value key pairs to it, but a little less verbose
void ItemClicked(object sender, EventArgs e)
{
var intent = new Intent(this, typeof(DetailsActivity));
intent.PutExtra("Id", 45);
base.StartActivity(intent);
base.StartActivity(intent);
}
protected override void
OnCreate(Bundle bundle)
{
int position = base.Intent.GetIntExtra(name:"id",defaultValue: -1);
}
For example let's say that we have a guess the number game, now in this game we define the number to guess on the source Activity(View), but to guess the number we go to a target Activity (View), then return our guess from the target back to the source to check if it was correct.
In our source activity we would override or just call the StartActivityForResult(intent, requestCode, bundle) method to launch our target activity. this is where we'd set the data to send via the intent or bundle parameters, and the request code that would then identify the target returning results.
then in our Target activity we would extract our extras like we normally would wire up event receivers, etc; basically go about our business as usual.
the difference in our target would be that before we call the base finish method we'd have to call the SetResult method, here we'd set our result code [Canceled, Ok, FirstUser].
finally back in our source we have to override the OnActivityResult(requestCode, result, intent) method , here we can correlate our request and response using the request code, then identify the result using the result enum and finally extract any data from our target using the extras on the intent.