Xamarin Android - Update UI control of one activit

2019-07-19 01:02发布

I have one Fragment "AddNewIncomeFragment " with a TextView("@+id/lblAccountHead"). On click on textview it starts a new activity "AccountHeadListActivit" which shows a list of existing Account Head. On selection of account head from "AccountHeadListActivity" i want to update "lblAccountHead" of first activity with selected account name, other values need to be intact.

Earlier i did it using "messaging center" for xamarin form. Now i trying to do the same in xamarin native(android).

*****UPDATED WITH FIRST SOLUTION APPLIED****

Click event in AddNewIncomeFragment which start account head activity:

  public void onAccounyHeadClick(object sender, EventArgs e)
        {
               var intend = new Intent(this.Activity, typeof(AccountHeadListActivity));
        //this.StartActivity(intend);
        this.StartActivityForResult(intend, 1000);
        }

ListView Selection event of Account head activity

void OnSelection (object sender, SelectedItemChangedEventArgs e)
        {

                     var result = new Intent();
        result.PutExtra("name", "Salary Account");
        result.PutExtra("id", 2);
        SetResult(Result.Ok, result);
       Finish();
        }

On closing this activity i want to update textview of previous activity with selected account head name/id. Please share what option we have to do this in xamarin andriod, should use StartActivityForResult,Local Notifications or any best approach.


I have implemented above solution and it is working fine. But the issue is- 2nd activity "AccountHeadList" contains an add new account link which start a new activity "AddNewAccount"- Now if user create a new account and save it then this activity need to be closed and 1st activity need to updated with newly created account name. So basically "StartActivityForResult" failed when it involves three activity and need to updated 1st activity from 3rd activity- Please suggest.


Thanks,

@Paul

1条回答
神经病院院长
2楼-- · 2019-07-19 01:38

To receive a result, call startActivityForResult() instead of startActivity()

then in your second activity, call the setResult(result) method to set the result before you finish the activity finish() after that in your original activity you can override the method

  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent     data) {
      // set the text view with the data from the result
  }
查看更多
登录 后发表回答