1

我的 Android 应用程序中有 2 个活动,Activity1并且Activity2.

当从 Activity2 按下返回时,我想显示从 String in 检索到的数据,Activity2并且在 Activity 1 textview 中设置此字符串值设置文本。

请任何人提供此问题的解决方案,在此先感谢。

活动 2:这里我将字符串值传递给活动 1,当我回按活动 2 时,这将被检索到活动 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) {
    }
}
}
4

1 回答 1

1


您应该在活动 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); 
        }   
  }  

}

在活动 2

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

希望这有帮助

于 2015-09-30T13:43:23.160 回答