我在活動A中,現在我導航到活動B.在完成活動B中的工作後,我切換回活動A(在按鈕的onclick()事件中調用finish();函數)。完成();函數通過恢復活動A將我帶回活動A.如何從活動B導航到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();
}
});
}