在活动之间传递数据-Android SDK
我已经阅读了之前发布的问题和答案两天了,我尝试了所有建议的变体,并在清单中将我的launchMode属性设置为“standard” 按下按钮后,我试图将数据从第二个活动传回第一个活动。按下按钮后,启动第一个活动,但它不会返回到onActivityResult()方法。我不明白为什么会这样 以下是我在活动2中的代码:在活动之间传递数据-Android SDK,android,android-intent,android-activity,Android,Android Intent,Android Activity,我已经阅读了之前发布的问题和答案两天了,我尝试了所有建议的变体,并在清单中将我的launchMode属性设置为“standard” 按下按钮后,我试图将数据从第二个活动传回第一个活动。按下按钮后,启动第一个活动,但它不会返回到onActivityResult()方法。我不明白为什么会这样 以下是我在活动2中的代码: Button btnAdd = (Button) findViewById(R.id.btnAdd); btnAdd.setOnClickListener(new Vi
Button btnAdd = (Button) findViewById(R.id.btnAdd);
btnAdd.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//Check that message is printed out to LogCat
System.out.println("hello test1 Activity2");
EditText band = (EditText) findViewById(R.id.txtBand);
band.setFilters(new InputFilter[] {
new InputFilter.LengthFilter(9)
});
EditText album = (EditText) findViewById(R.id.txtAlbum);
album.setFilters(new InputFilter[] {
new InputFilter.LengthFilter(9)
});
final Spinner genre = (Spinner) findViewById(R.id.spin_genre);
TextView selection = (TextView)genre.getSelectedView();
CharSequence strBand = band.getText();
CharSequence strAlbum = album.getText();
CharSequence strGenre = selection.getText();
//Check that we got input values
System.out.println("hello test Activity2- " +
strBand + " - " + strAlbum + " - " + strGenre);
//**********Intent Declaration************
Intent i = new Intent(getApplicationContext(), Activity1.class);
i.putExtra("strBand", strBand);
i.putExtra("strAlbum", strAlbum);
i.putExtra("strGenre", strGenre);
startActivityForResult(i, 0);
setResult(RESULT_OK, i);
finish();
}
});
以下是活动1:
public class Activity1 extends Activity {
/** Called when the activity is first created. */
@SuppressWarnings("deprecation")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button addAlbum = (Button) findViewById(R.id.btnMain);
addAlbum.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent i = new Intent();
i.setClassName("jorge.jorge.jorge",
"jorge.jorge.jorge.Activity2");
startActivity(i);
}
});
}// end of onCreate()
//******************Callback Method****************************
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
//Checks if we got to ActivityResult
System.out.println("hello test 2: We got to Activity1");
if (resultCode == RESULT_OK)
{
Bundle returndata = data.getExtras();
String strAlbum = returndata.getString("strAlbum");
String strBand = returndata.getString("strBand");
String strGenre = returndata.getString("strGenre");
// check to see if we got the variable values from activity2
System.out.println("hello test 2: We got to Activity1 with variables - "
+ strBand + " - " + strAlbum + " - " + strGenre);
//Create table layout to contains views with variable values
TableLayout table = new TableLayout(this);
table.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
//creates row with parameters
TableRow row = new TableRow(this);
row.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
//text views to contain variable values
TextView tv1 = new TextView(this);
tv1.setText(strBand);
row.addView(tv1);
TextView tv2 = new TextView(this);
tv2.setText(strAlbum);
row.addView(tv2);
TextView tv3 = new TextView(this);
tv3.setText(strGenre);
row.addView(tv3);
//adds the table row to the table layout
table.addView(row);
}
}// end onActivityResult()
}
我不确定我的活动回调是否没有正确地放置在代码中,或者我没有正确地触发意图,或者我没有使用正确的方法设置回调。我知道这个话题已经讨论过了,但我没有主意了。谢谢。你刚刚把它倒过来了。如果Activity1应启动Activity2,Activity2应将结果发送回Activity1,则您需要这样做: 在活动1中:
Intent i = new Intent();
i.setClassName("jorge.jorge.jorge", "jorge.jorge.jorge.Activity2");
startActivityForResult(i); // This starts Activity2 and waits for the result
Intent i = new Intent(getApplicationContext(), Activity1.class);
i.putExtra("strBand", strBand);
i.putExtra("strAlbum", strAlbum);
i.putExtra("strGenre", strGenre);
setResult(RESULT_OK, i);
finish(); // This closes Activity2 and generates the callback to Activity.onActivityResult()
在活动2中:
Intent i = new Intent();
i.setClassName("jorge.jorge.jorge", "jorge.jorge.jorge.Activity2");
startActivityForResult(i); // This starts Activity2 and waits for the result
Intent i = new Intent(getApplicationContext(), Activity1.class);
i.putExtra("strBand", strBand);
i.putExtra("strAlbum", strAlbum);
i.putExtra("strGenre", strGenre);
setResult(RESULT_OK, i);
finish(); // This closes Activity2 and generates the callback to Activity.onActivityResult()
你刚刚把它倒过来了。如果Activity1应启动Activity2,Activity2应将结果发送回Activity1,则您需要这样做: 在活动1中:
Intent i = new Intent();
i.setClassName("jorge.jorge.jorge", "jorge.jorge.jorge.Activity2");
startActivityForResult(i); // This starts Activity2 and waits for the result
Intent i = new Intent(getApplicationContext(), Activity1.class);
i.putExtra("strBand", strBand);
i.putExtra("strAlbum", strAlbum);
i.putExtra("strGenre", strGenre);
setResult(RESULT_OK, i);
finish(); // This closes Activity2 and generates the callback to Activity.onActivityResult()
在活动2中:
Intent i = new Intent();
i.setClassName("jorge.jorge.jorge", "jorge.jorge.jorge.Activity2");
startActivityForResult(i); // This starts Activity2 and waits for the result
Intent i = new Intent(getApplicationContext(), Activity1.class);
i.putExtra("strBand", strBand);
i.putExtra("strAlbum", strAlbum);
i.putExtra("strGenre", strGenre);
setResult(RESULT_OK, i);
finish(); // This closes Activity2 and generates the callback to Activity.onActivityResult()
哦,这是有道理的。这有点疯狂,我可以找到一个合适的例子来指出这一点。我查阅了一本教科书和多篇帖子,都有很好的答案,但缺乏这方面的内容。非常感谢,我很感激。哦,这很有道理。这有点疯狂,我可以找到一个合适的例子来指出这一点。我查阅了一本教科书和多篇帖子,都有很好的答案,但缺乏这方面的内容。非常感谢,我很感激。