Experience in using android startActivity() and startActivityForResult()

The final effect:

1. List Activity—>Details Activity—>Edit Activity3. After editing the data, return to the details interface and refresh the data. The details interface returns to the list interface and refresh the data.
2. List Activity—>Details Activity—>Edit Activity3. After editing the data, return directly to the list and refresh the data.

Specific steps:

The first way to achieve the effect:
  public static void startAction(Activity context, String id) {
        Intent intent = new Intent(列表Activity, 详情Activity);
        context.startActivityForResult(intent,10000);
    }
  public static void startAction(Activity context, String id) {
        Intent intent = new Intent(详情Activity, 编辑Activity);
        context.startActivityForResult(intent,10000);
    }

Edit Activity completes data and returns details

Intent intent = new Intent();
setResult(Activity.RESULT_OK, intent);
finish();

Details Activity handles refresh data

  @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
             if(requestCode == 10000){
                //再次调用接口请求数据
            }
        }
    }

Details Activity returns to the list interface and refreshes the data

 @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode != RESULT_OK) {
           //再次调用接口请求数据
        }
    }
The second way to achieve the effect:
  public static void startAction(Activity context, String id) {
        Intent intent = new Intent(列表Activity, 详情Activity);
        context.startActivityForResult(intent,10000);
    }
  public static void startAction(Activity context, String id) {
        Intent intent = new Intent(详情Activity, 编辑Activity);
        context.startActivityForResult(intent,10000);
    }

Details Activity jumps to editing Activity

  public static void startAction(Activity context, String id) {
        Intent intent = new Intent(详情Activity, 编辑Activity);
        context.startActivityForResult(intent,10000);
    }
    finish();//跳转时直接关闭详情Activity

Edit Activity. After editing the data, return directly to the list Activity.

 @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode != RESULT_OK) {
           //再次调用接口请求数据
        }
    }

Remark:


A programming novice’s opinion. If there are any mistakes, please point them out and correct them below to avoid misleading others.

The effect of this article has been personally tested. I am just writing this article as a note to myself. Don’t spray, don’t spray, don’t spray.


Guess you like

Origin blog.csdn.net/u010689434/article/details/111993500