Okay, it's a really common question is what I'm seeing. I've picked Xamarin Android up a couple days ago, and I've been trying to figure out Intents and Bundles, after about 2 hours of searching around I still can't understand it, because 'getIntent()' is showing up everywhere I look, however, visual studio keeps complaining that 'getIntent()' doesn't exist in my current context. Plus I have to StartActivity(MyOtherActivity) and also StartActivity(MyIntent) (?) I've read a bunch of info and I think it merged into one massive flustercuck.
Most of the forums threads I find are from 2010-2015 and it's really confusing if I'm doing something wrong or if they've just changed how a bunch of the notation works (which is probably the case).
So in my CalculatorActivity - I've got an int array containing 'enemyStats' which I need to pass into another activity called 'CalcAttackActivity'.
protected override void OnCreate(Bundle savedInstanceState) { const int STRENGTH = 0; const int PERCEPTION = 1; const int ENDURANCE = 2; const int CHARISMA = 3; const int INTELLIGENCE = 4; const int AGILITY = 5; const int LUCK = 6; base.OnCreate(savedInstanceState); SetContentView(Resource.Layout.activity_calculator); int[] enemyStats = new int[7]; FindViewById<Button>(Resource.Id.btnContinue).Click += delegate { EditText editSpecial = (EditText)FindViewById(Resource.Id.edtEnemySpecial); string special = editSpecial.Text; string[] specialArr = special.Split(','); for (int i = 0; i < enemyStats.Length; i++) enemyStats[i] = int.Parse(specialArr[i]); Bundle b = new Bundle(); b.PutIntArray("myArr", enemyStats); Intent intent = new Intent(this, Class); intent.PutExtras(b); StartActivity(intent); StartActivity(typeof(CalcAttackActivity)); }; }
That's my CalculatorActivity, the bottom part is where I've attempted to do some weird stuff that's completely wrong.
Moving on into my CalcAttackActivity, I've got basically nothing because I can't figure out how to get data back from the Intent or bundle (anything really xD)
[Activity(Label = "CalcAttackActivity")]
public class CalcAttackActivity : Activity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.activity_calcAttack);
} }
If anyone knows a good (up-to-date) article or video or cares to explain how I can go about transferring this integer array I'd be very thankful!
Generally, we use Intent to pass data from one activity to another. Here is the code:
protected override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); Xamarin.Essentials.Platform.Init(this, savedInstanceState); // Set our view from the "main" layout resource SetContentView(Resource.Layout.activity_main); Button btn = FindViewById<Button>(Resource.Id.button); btn.Click += Btn_Click; } private void Btn_Click(object sender, System.EventArgs e) { Android.Content.Intent intent = new Android.Content.Intent(this, typeof(SecondActivity)); int[] intArray = new int[] { 1, 2, 3 }; intent.PutExtra("IntArray", intArray); this.StartActivity(intent); }
And we can access this extra via Intent property of the current activity. getIntent()
is a native Android API and we use Intent directly on Xamarin:
protected override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); // Create your application here SetContentView(Resource.Layout.activity_second); var intArray = this.Intent.GetIntArrayExtra("IntArray"); foreach (int i in intArray) { Log.Debug("tag", i.ToString()); } }
I also attached my sample here. You can refer to the attachment for more detailed code.
Answers
Generally, we use Intent to pass data from one activity to another. Here is the code:
And we can access this extra via Intent property of the current activity.
getIntent()
is a native Android API and we use Intent directly on Xamarin:I also attached my sample here. You can refer to the attachment for more detailed code.
Thank you so much, that helps clear it up!