我在活动A中,现在我导航到活动B.在活动B中完成工作后,我切换回活动A(在按钮的onclick()事件中调用finish();函数)。这个完成();函数通过恢复活动A将我带回活动A.如何从活动B导航到A以及数据。
这是代码: - 活动A: -
NewMain.java
public void onRestoreInstanceState(Bundle savedInstanceState) {
// Always call the superclass so it can restore the view hierarchy
super.onRestoreInstanceState(savedInstanceState);
// Restore state members from saved instance
alarmname1 = savedInstanceState.getString("EXTRA_MESSAGE");
range1 = savedInstanceState.getInt("EXTRA_MESSAGE_RANGE", 100);
//Toast.makeText(NewMain.this, longi1, 2000).show();
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1) {
if(resultCode == RESULT_OK){
/*Bundle bu_lati = getIntent().getExtras();
lati1= Double.parseDouble( bu_lati.getString("EXTRA_MESSAGE_LATITUDE"));
Bundle bu_longi = getIntent().getExtras();
longi1=Double.parseDouble( bu_longi.getString("EXTRA_MESSAGE_LONGITUDE"));
*/
Bundle lati1=data.getBundleExtra("EXTRA_MESSAGE_LATITUDE");
Bundle longi1=data.getBundleExtra("EXTRA_MESSAGE_LONGITUDE");
}
if (resultCode == RESULT_CANCELED) {
//Write your code on no result return
}
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.newgui1);
alarmname=(EditText) findViewById(R.id.editText1);
range=(EditText) findViewById(R.id.editText2);
Button button = (Button) findViewById(R.id.button2);
im1=(ImageButton) findViewById(R.id.imageButton1);
Button lnl = (Button) findViewById(R.id.button3);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//alarmname
alarmname1=alarmname.getText().toString();
//range
range1=Integer.parseInt(range.getText().toString());
Intent myIntent = new Intent(NewMain.this, NewMain2.class);
myIntent.putExtra("EXTRA_MESSAGE", alarmname1);
myIntent.putExtra("EXTRA_MESSAGE_RANGE", range1);
myIntent.putExtra("EXTRA_MESSAGE_LATITUDE1", lati1);
myIntent.putExtra("EXTRA_MESSAGE_LONGITUDE1", longi1);
NewMain.this.startActivity(myIntent);
}
});
lnl.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Toast.makeText(NewMain.this, longi1, 2000).show();
}
});
im1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent myIntent = new Intent(NewMain.this, MapViewDemo.class);
NewMain.this.startActivity(myIntent);
}
});
}
}
活动B: -
MapViewDemo.java
public boolean onTouchEvent(MotionEvent event, MapView mapView)
{
//---when user lifts his finger---
if (event.getAction() == 1) {
GeoPoint p = mapView.getProjection().fromPixels((int) event.getX(),(int) event.getY());
lati=p.getLatitudeE6() / 1E6;
longi=p.getLongitudeE6() /1E6;
Toast.makeText(getBaseContext(), lati+ "," +longi,
Toast.LENGTH_SHORT).show();
}
return false;
}
}
Button buttondone = (Button) findViewById(R.id.btndone);
buttondone.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent myIntent1 = new Intent();
myIntent1.putExtra("EXTRA_MESSAGE_LATITUDE", lati);
myIntent1.putExtra("EXTRA_MESSAGE_LONGITUDE", longi);
setResult(NewMain.RESULT_OK, myIntent1);
finish();
}
});
}
答案 0 :(得分:3)
开始您的B活动,
Intent intent = new Intent(ActivityA.this, ActivityB.class);
startActivityForResult(intent, 1);
在完成B活动之前setResult()喜欢,
Intent i =new Intent();
i.putExtra("yourdata", "abc");
setResult(2, i);
finish();
在您的A活动
上@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if(resultCode==2){
String s = data.getStringExtra("yourdata");
}
}
这样您就可以将数据从子活动传递到父活动。
答案 1 :(得分:1)
最好的方法是使用带有Intent和请求代码作为参数的模式startActivityForResult。
与startActivity相同,但您可以在活动B完成后等待结果。
在调用活动B之前,你必须在活动B上使用setResult然后完成()。 setResult有2个参数:一个intent(你要传回A的数据)和一个resultCode(ok,错误代码)。
在活动A上,您可以等待并获取结果覆盖方法onActivityResult
这是一个很好的教程:Getting a Result from an Activity
答案 2 :(得分:0)
从您的活动newMain传递请求代码并使用startActivityForResult
而不是NewMain.this.startActivity(myIntent)
:
startActivityForResult(intent, 10);
现在让你的(requestCode == 10)
处于你的if状态。
希望它能运作
答案 3 :(得分:0)
使用此代码。
开始第二项活动:
Intent myIntent = new Intent(NewMain.this, MapViewDemo.class);
startActivityForResult(launchNewIntent, 2);
在活动A中
int result1 ;
int result2 ;
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 2) {
if (resultCode == RESULT_OK) {
result1=data.getExtras().getInt("EXTRA_MESSAGE_LATITUDE");
result2 =data.getExtras().getInt("EXTRA_MESSAGE_LONGITUDE");
}
}
}
在活动B中
Intent myIntent1 = new Intent();
myIntent1.putExtra("EXTRA_MESSAGE_LATITUDE", lati);
myIntent1.putExtra("EXTRA_MESSAGE_LONGITUDE", longi);
setResult(Activity.RESULT_OK, resultData);
finish();
答案 4 :(得分:0)
参考link,您将了解整个概念..