gpt4 book ai didi

android - 如何与数据一起从一项 Activity 导航到另一项 Activity ?

转载 作者:搜寻专家 更新时间:2023-11-01 08:05:13 24 4
gpt4 key购买 nike

我在 Activity A 中,现在导航到 Activity B。在 Activity B 中完成我的工作后,我切换回 Activity A(在按钮的 onclick() 事件中调用 finish(); 函数)。这个完成();函数通过恢复 Activity A 将我带回 Activity A。我如何从 Activity B 导航到 A 以及数据。

这是代码:- Activity A:-

新主程序.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);

}
});
}

}

Activity 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();




}
});

}

最佳答案

开始您的 B Activity ,例如,

Intent intent = new Intent(ActivityA.this, ActivityB.class);
startActivityForResult(intent, 1);

在完成 B Activity setResult() 之前,

Intent i =new Intent();
i.putExtra("yourdata", "abc");
setResult(2, i);
finish();

关于你的 A Activity

@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");
}
}

这样您就可以将数据从子 Activity 传递到父 Activity 。

关于android - 如何与数据一起从一项 Activity 导航到另一项 Activity ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15269404/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com