views:

78

answers:

1

I have a MainActivity class with an "Add Item" button and a listview and a AddItemActivity class with a textbox and a "save" button. The user should click on the "Add Item", go to the AddItemActivity screen, enter an item, click 'save' and then go back to the updated MainActivity screen.

(BTW, I'm using MonoDroid, but I don't think this is necessary a MonoDroid specific problem).

My "Add item" click event (on main activity):

    private void addItemButton_Click(object sender, EventArgs e)
    {
        var intent = new Intent();
        intent.SetClassName(this, "monoApp.AddItemClassName");
        StartActivityForResult(intent, 0);
    }

My "Save" click event (on add item activity):

    private void saveButton_Click(object sender, EventArgs e)
    {
        var itemname = FindViewById<EditText>(Resource.id.itemName);

        _repo.SaveItem(new Item() {Name = itemname.Text.ToString()});

        Toast.MakeText(this, "You saved: " + itemname.Text, ToastLength.Short).Show();

        var intent = new Intent();
        SetResult(Result.Ok, intent);
        Finish();
    }

And then back in main activity:

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

        RefreshTheList();
    }

This works like a charm, on the first time. The screen slides over to 'add item', and then slides back to main when it's done. The second time, it slides over, but then it slides back to itself, if that makes any sense. And then if I hit save again, it goes back to working fine.

If video would help, I've posted a clip of what I'm seeing to TwitVid: http://www.twitvid.com/W7XZC

Basically, I don't want it to "slide back to itself" ever, so what am I doing wrong?

+2  A: 

I don't see anything wrong in your code here...I am taking a guess here.

Your Second activity might be getting launched multiple times in the second attempt somehow from your remaining portions of the code.

Print a log inside your addItemButton_Click and check. If the log is getting printed twice then it is calling the method 2 times somehow.

Rahul
Never underestimate the power of a second set of eyes :) The logging did it--the click event method was being fired twice because I was binding it twice by mistake. Thanks!
mgroves