gpt4 book ai didi

android - 当返回按下android时从返回 Activity 中获取值

转载 作者:行者123 更新时间:2023-11-30 01:52:07 26 4
gpt4 key购买 nike

我的 Android 应用程序中有 2 个 Activity ,Activity1Activity2

我想显示从 Activity2 中的字符串检索的数据,并且当从 Activity2 按下后退时,此字符串值在 Activity 1 TextView 中设置文本。

请任何人给出这个问题的解决方案,在此先感谢。

Activity 2:这里我将字符串值传递给 Activity 1,当我向后按 Activity 2 时,它将被检索到 Activity 1。

  public void onBackPressed() {
// TODO Auto-generated method stub
NoolDataBaseHelper db = new NoolDataBaseHelper(NoolDashboardDetailPage.this);
int strtext = db.getProfilesCount();
db.close();
Intent intent = new Intent();
intent.putExtra("Obj", strtext+"");
setResult(Activity.RESULT_OK, intent);
if (isclose) {

finish();

}
else
{
if (!isplays) {
inflateLoginlayout.setVisibility(View.GONE);
topview.setVisibility(View.VISIBLE);
isplays = true;
//edtnames.getText().clear();

}
else
{
super.onBackPressed();
}
}

}

Activity1:这里我从 Activity 2 中检索字符串并将字符串值设置到我的 textview

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1) {
if (resultCode == Activity.RESULT_OK) {
String sSuName = data.getStringExtra("Obj");
txtfavouratecount.setText(sSuName);
} else if (resultCode == 0) {
}
}
}

最佳答案

你应该遵循这个结构
在 Activity 1 中

Intent intent=new Intent(Activity1.this,Activity2.class);  
startActivityForResult(intent, 2);// Activity is started with requestCode 2

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// check the request code here is 2
if(requestCode==2){
if(resultCode == 3){ // check the result code
String message=data.getStringExtra("MESSAGE");
// set text for your textview
textView1.setText(message);
}
}

}

在 Activity 2 中

public void onBackPressed() {
String message = "abc";
Intent intent=new Intent();
intent.putExtra("MESSAGE",message);
setResult(3,intent); // 3 is result code
super.onBackPressed();
}

希望对你有帮助

关于android - 当返回按下android时从返回 Activity 中获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32867417/

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