Java 我可以在finish()上传递一个额外的意图吗?
我想知道,调用finish()后是否可以将信息发送到返回的活动 例如,我有一个ActivityJava 我可以在finish()上传递一个额外的意图吗?,java,android,android-intent,Java,Android,Android Intent,我想知道,调用finish()后是否可以将信息发送到返回的活动 例如,我有一个ActivitySendMessageActivity.class,它允许用户将消息发布到他们的提要。将该消息保存到服务器后,我调用finish()。我是否应该以新的意图开始我的main活动.class?或者生命周期开发最好只完成SendMessageActivity.class 我不认为启动新活动有什么意义,因为关闭当前活动总是会让您返回到MainActivity.class。如何在完成当前活动后发送一个额外的字符串
SendMessageActivity.class
,它允许用户将消息发布到他们的提要。将该消息保存到服务器后,我调用finish()
。我是否应该以新的意图开始我的main活动.class
?或者生命周期开发最好只完成SendMessageActivity.class
我不认为启动新活动有什么意义,因为关闭当前活动总是会让您返回到MainActivity.class
。如何在完成当前活动后发送一个额外的字符串?您可以选择一个活动的名称,它允许您将数据输入到initent中
在第一个活动中,使用方法onActivityResult调用新活动并检索数据。所有内容都在文档中。如果调用finish()以避免用户返回SendMessageActivity.class,则可以将此标志设置为您的意图:
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
这将打开MainActivity并从activities堆栈中删除SendMessageActivity。尝试以下操作:
在第一项活动中:
Intent first = new Intent(ActivityA,this, ActivityB.class);
startActivityForResult(first, 1);
现在进入第二个活动:在完成过程中设置结果()
第一个活动捕捉结果
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
}
}
在ActivityResult上使用
这可能有助于您了解活动结果
通过使用startActivityForResult(Intent-Intent,int-requestCode)
您可以启动另一个活动,然后在onActivityResult()
方法中从该活动接收结果。因此onActivityResult()
是从您启动另一个活动的地方开始的
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
是否有与子活动等效的onActivityResult()函数?我假设它的调用类似于onActivityRequest(),并且它允许我根据启动活动所调用的请求代码执行某些操作。
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.
}
}
}
}
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();
}
}