我想知道,是否可以在调用finish()后将信息发送给我返回的活动?
例如,我有一个活动SendMessageActivity.class
,允许用户在其Feed中发布消息。将该消息保存到服务器后,我呼叫finish()
。我应该用新的意图来启动我的MainActivity.class
吗?或者,只有完成SendMessageActivity.class
才能更好地进行生命周期开发?
我没有看到开始新活动的重点,因为关闭当前活动将始终将您带回MainActivity.class
。如何在完成当前活动后发送一个额外的字符串?
答案 0 :(得分:4)
使用 onActivityResult 。
这可以帮助您理解 onActivityResult 。
使用startActivityForResult(Intent intent, int requestCode)
,您可以开始另一个活动,然后在onActivityResult()
方法中从该活动接收结果。{{1来自你开始另一个Activity的地方。
onActivityResult()
检查这里的参数。请求代码可以从您获得结果的位置进行过滤。所以你可以使用他们的requestCodes识别不同的数据!
示例强>
onActivityResult(int requestCode, int resultCode, Intent data)
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();
}
}
您MainActivity
指定了startActivityForResult
。假设你想调用三个不同的活动来获得结果..所以有三个REQUEST_CODE
调用三个不同的 REQUEST_CODE 。 REQUEST_CODE只是您在活动中指定的唯一键,用于唯一标识您的startActivityForResult
来电。
从这些活动中收到数据后,您可以查看REQUEST_CODE是什么,那么您知道啊,这个结果来自此活动。
就像你用彩色的封面给你的情人发送邮件,并要求他们用相同的封面回复。然后,如果你收到他们的回信,你知道是谁送给你的。 awww;)
答案 1 :(得分:3)
您可以set result一项活动,它允许您将数据转换为initent。
在您的第一个活动中,使用startActivityForResult()调用新活动,并在方法onActivityResult中检索数据。一切都在文档中。
答案 2 :(得分:1)
如果您调用finish()以避免用户返回SendMessageActivity.class,您可以将此标志设置为您的意图:
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
这将打开MainActivity并从活动堆栈中删除SendMessageActivity。
答案 3 :(得分:1)
试试这个:
第一个活动中的:
Intent first = new Intent(ActivityA,this, ActivityB.class);
startActivityForResult(first, 1);
现在进入第二项活动:在finish
()
Intent intent = new Intent();
intent.putExtra("result",result); //pass intent extra here
setResult(RESULT_OK,intent);
finish();
第一项活动收集结果;
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
// check if the request code is same as what is passed here it is 1
if(requestCode==1)
{
String message=data.getStringExtra("result");
//get the result
}
}