Xamarin Android - Update UI control of one activity from another activity

吃可爱长大的小学妹 提交于 2019-12-23 23:41:03

问题


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:


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
  }


来源:https://stackoverflow.com/questions/46577529/xamarin-android-update-ui-control-of-one-activity-from-another-activity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!