本文介绍了未能传递结果 ResultInfo的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有很多人在stackoverflow上遇到过同样的错误,但是我在那些帖子中找不到任何相关的解决方案.我的 MainActivity 正在使用 startActivityForResult() 开始一个新活动 (SecondActivity);SecondActivity 然后用 onBackPressed() 返回一些数据;和 putExtra();
There are many people who have encountered the same error on stackoverflow, but I haven't been able to find any relevant resolution in those posts. My MainActivity is starting a new activity (SecondActivity) with startActivityForResult(); SecondActivity then returns some data with onBackPressed(); and putExtra();
从 MainActivity.java 中提取:
Extract from MainActivity.java:
public void addNewNote(View v){
Intent intent = new Intent(this, SecondActivity.class);
intent.putExtra("Source", "NEW");
startActivityForResult(intent, 1); //1 is the result code
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.v("TAG", data.getStringExtra("Note"));
if (requestCode == 1) {
if(resultCode == RESULT_OK){
listItems.add(data.getStringExtra("Note"));
Log.v("TAG", data.getStringExtra("Note"));
adapter.notifyDataSetChanged();
listView.invalidateViews();
}
if (resultCode == RESULT_CANCELED) {
}
}
}
从 SecondActivity.java 中提取:
Extract from SecondActivity.java:
@Override
public void onBackPressed() {
super.onBackPressed();
if (mainTextField.getText() != null){
Intent returnIntent = new Intent();
returnIntent.putExtra("Note",mainTextField.getText());
setResult(RESULT_OK, returnIntent);
finish();
} else {
Intent returnIntent = new Intent();
setResult(RESULT_CANCELED, returnIntent);
finish();
}
}
Logcat:
推荐答案
试试这个-
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == 1 && data != null)
{
Log.v("TAG", data.getStringExtra("Note"));
if(resultCode == RESULT_OK)
{
listItems.add(data.getStringExtra("Note"));
Log.v("TAG", data.getStringExtra("Note"));
adapter.notifyDataSetChanged();
listView.invalidateViews();
}
if (resultCode == RESULT_CANCELED)
{
}
}
}
这篇关于未能传递结果 ResultInfo的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!