我如何为结果启动活动并从我开始的活动中获得结果?

时间:2011-12-26 07:35:59

标签: android android-layout

任何机构都可以解释我如何开始结果活动并从我开始的活动中获得结果? 谢谢和问候 RizN81

4 个答案:

答案 0 :(得分:3)

使用它 在活动中

Intent i = new Intent(this, NextActivity.class);
startActivityForResult(i, SELECT_IMAGE ); //SELECT_IMAGE is an static int value.

此结果代码

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
     if (resultCode == RESULT_OK) {
         switch(requestCode) {
         case SELECT_IMAGE:
                 ///// write code here for your requirement
         }
     }
  }

答案 1 :(得分:0)

让您想从ActivityA调用ActivityB 请按照以下步骤进行操作

activityB中的

Step-1 设置结果

ActivityB 中的

使用setResult()设置结果,请参阅ActivityB的示例代码

Intent intent = new Intent();
            intent.putExtra("hh", hour);
            intent.putExtra("mm", min);
            intent.putExtra("ss", sec);
            intent.putExtra("am", am);
            setResult(2, intent);

step-2 从ActivityA调用activityB ActivityA 中的使用以下代码

从activityA调用activityB
startActivityForResult(activityBIntent,1);

步骤3 在activityA中获取activityB的结果后编写您想要执行的逻辑

当ActivityB完成控制时将调用onActivityResult()方法调用acticity(ActivityA)

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);
         if (requestCode == 1) {

            if (resultCode == 2) {
                int hour = data.getIntExtra("hh", 0);
                int min = data.getIntExtra("mm", 0);
                int sec = data.getIntExtra("ss", 0);
                int am = data.getIntExtra("am", 0);

            }
}
}

答案 2 :(得分:0)

尝试使用startActivityForResult()方法开始您的活动,并使用onActivityResult()方法检查您的活动是否已完成。

答案 3 :(得分:0)

试试这个

IN Activity_A

// Activity Callback Variable
private static final int FROM_ACTIVITY_B = 2;

// Now Start the Activity B from Activity A
startActivityForResult(new Intent(Activity_A.this, Activity_B.class), FROM_ACTIVITY_B);

IN Activity_B

// Now Place the following code when you want to pass the result to caller Activity which in our case is Activity_A
Intent data = new Intent();
// Put some data in the intent if you want those in the Activity_A
setResult(Activity.RESULT_OK, data);
Activity_B.this.finish();

IN Activity_A

现在要在Activity_A中获取结果,您需要覆盖Activity_A中的onActivityResult

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (resultCode == Activity.RESULT_OK) {

        if (requestCode == FROM_ACTIVITY_B) {
            // Intent data is the one you passed from the Activity_B
            // Do whatever you want here...
        }
    }
}