Um Cara Qualquer
Um Cara Qualquer

Reputation: 61

On activity finished

I'm developing an notepad app, and i need to reload the notes on the view when the Note Editor is closed (i'm using Finish() to close the Editor), but I am not sure how to wait until the Activity closes and does something.

I'm starting the Editor Activity with this:

public async void noteClicked(object sender, EventArgs e)
        {
            var obj = (RelativeLayout)sender;
            var id = obj.Id;
            var note = Util.db.Query<Note>(string.Format("SELECT * FROM Notes WHERE ID={0}", id));

            var intent = new Intent(this, typeof(EditorAcitivity));
            intent.PutExtra("Note", JsonConvert.SerializeObject(note));
            intent.PutExtra("Mode", "Edit");
            StartActivity(intent);
        }

On the EditorActivity.cs:

protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            SetContentView(Resource.Layout.editor_activity);

            Util.context = this;

            mode = Intent.GetStringExtra("Mode");
            txtTitle = FindViewById<EditText>(Resource.Id.editorTitle);
            txtText = FindViewById<EditText>(Resource.Id.editorText);

            if (mode == "Edit")
            {
                note = JsonConvert.DeserializeObject<Note>(Intent.GetStringExtra("Note"));
                txtTitle.Text = note.title;
                txtText.Text = note.text;
            }

            FindViewById<ImageButton>(Resource.Id.editorDelete).Click += editorDelete_Clicked;
            FindViewById<ImageButton>(Resource.Id.editorSave).Click += editorSave_Clicked;
        }
private void editorSave_Clicked(object sender, EventArgs e)
        {
            if (txtText.Text.Length == 0 || string.IsNullOrWhiteSpace(txtText.Text))
            {
                Util.makeShortText("Null text");
            }
            else
            {
                var note = new Note();
                note.date = Util.now();
                note.text = txtText.Text;
                if (string.IsNullOrWhiteSpace(txtTitle.Text))
                {
                    note.title = " ";
                }
                else {
                    note.title = txtTitle.Text;
                }
                Util.db.Insert(note);
                this.Finish();
            }
        }

I want to do something like loadNotes() when the Activity is finished (this.Finish())

Edit: I don't wanna return some data, just wait the activity.

Upvotes: 0

Views: 80

Answers (1)

I would suggest you to use StartActivityForResult

var intent = new Intent(this, typeof(EditorAcitivity));
intent.PutExtra("Note", JsonConvert.SerializeObject(note));
intent.PutExtra("Mode", "Edit");
StartActivityForResult(intent,REQUEST_CODE_EDITOR_ACITIVTY);

In that case REQUEST_CODE_EDITOR_ACITIVTY is a integer constant.

And then when your about to finish your activity call to the SetResult method as below

Util.db.Insert(note);            
SetResult(Result.Ok);
this.Finish();

And finally override the OnActivityResult method in the same activity you're starting the EditorAcitivity as below

protected override void OnActivityResult(int requestCode, [GeneratedEnum] Result resultCode, Intent data)
{
    base.OnActivityResult(requestCode, resultCode, data);
    if(requestCode == REQUEST_CODE_EDITOR_ACITIVTY) {
       if(resultCode == Result.Ok) {
        //Here your activity received the callback and you can call the load notes method
        loadNotes();
       }
    }
}

Take a look at this tutorial if you want to learn more about Activity for result

https://subscription.packtpub.com/book/application_development/9781784398576/8/ch08lvl1sec84/obtaining-data-from-activities

Upvotes: 1

Related Questions