Use 活动结果.
这可能会帮助你理解活动结果.
通过使用startActivityForResult(Intent intent, int requestCode)
你可以start另一个活动,然后收到结果从该活动中onActivityResult()
方法.SoonActivityResult()
是从您开始另一个活动的地方。
onActivityResult(int requestCode, int resultCode, Intent data)
检查这里的参数。请求代码用于从您获得结果的位置进行过滤。这样您就可以使用它们的请求代码来识别不同的数据!
Example
public class MainActivity extends Activity {
// Use a unique request code for each use case
private static final int REQUEST_CODE_EXAMPLE = 0x9988;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Create an Intent to start AnotherActivity
final Intent intent = new Intent(this, AnotherActivity.class);
// Start AnotherActivity with the request code
startActivityForResult(intent, REQUEST_CODE_EXAMPLE);
}
//-------- When a result is returned from another Activity onActivityResult is called.--------- //
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// First we need to check if the requestCode matches the one we used.
if(requestCode == REQUEST_CODE_EXAMPLE) {
// The resultCode is set by the AnotherActivity
// By convention RESULT_OK means that what ever
// AnotherActivity did was successful
if(resultCode == Activity.RESULT_OK) {
// Get the result from the returned Intent
final String result = data.getStringExtra(AnotherActivity.EXTRA_DATA);
// Use the data - in this case, display it in a Toast.
Toast.makeText(this, "Result: " + result, Toast.LENGTH_LONG).show();
} else {
// AnotherActivity was not successful. No data to retrieve.
}
}
}
}
AnotherActivity
MainActivity
public class AnotherActivity extends Activity {
// Constant used to identify data sent between Activities.
public static final String EXTRA_DATA = "EXTRA_DATA";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_another);
final View button = findViewById(R.id.button);
// When this button is clicked we want to return a result
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Create a new Intent as container for the result
final Intent data = new Intent();
// Add the required data to be returned to the MainActivity
data.putExtra(EXTRA_DATA, "Some interesting data!");
// Set the resultCode to Activity.RESULT_OK to
// indicate a success and attach the Intent
// which contains our result data
setResult(Activity.RESULT_OK, data);
// With finish() we close the AnotherActivity to
// return to MainActivity
finish();
}
});
}
@Override
public void onBackPressed() {
// When the user hits the back button set the resultCode
// to Activity.RESULT_CANCELED to indicate a failure
setResult(Activity.RESULT_CANCELED);
super.onBackPressed();
}
}
Note: 现在签到MainActivity
you startActivityForResult
在那里你指定一个REQUEST_CODE
。假设您想调用三个不同的活动来获取结果..所以有三个startActivityForResult
调用三个不同的请求代码的。 REQUEST_CODE 只是您在活动中指定的唯一键,用于唯一标识您的startActivityForResult
calls.
一旦您从这些活动收到数据,您就可以检查 REQUEST_CODE 是什么,然后您就知道啊哈这个结果来自该活动。
这就像你给你的爱人发了一封带有彩色封面的邮件,并要求他们用同样的封面回复。然后,如果您收到他们的回信,您就知道是谁给您寄来的。哇哦;)