如何在Android中的先前活动中接收数据

时间:2015-12-08 09:53:49

标签: android

我希望从下一个活动中接收上一个活动中的数据,例如(1< --- 2)。我尝试过,但是从第二次到第一次活动都没有收到数据。

这是第一次活动

 Intent i = new Intent(CustomActionActivity.this, Edit_Post.class);
                    i.putExtra("ActivityId", getItemActivity);
                    i.putExtra("Vessel", strVesselName);
                    i.putExtra("HashTag", strHashTag);
                    i.putExtra("RemarkTitle", strRemark);
                    i.putExtra("ShortRGN", strShortTypeRGN);
                    i.putExtra("VessId", strvesselid);
                    startActivity(i);

这是第二个活动

  Intent intent = getIntent();
    strActivityId = intent.getStringExtra("ActivityId");
    strVesselName = intent.getStringExtra("Vessel");
    strHashTag = intent.getStringExtra("HashTag");
    strRemark = intent.getStringExtra("RemarkTitle");
    strShortRGN = intent.getStringExtra("ShortRGN");
    strVessId = intent.getStringExtra("VessId");


img_AddPostAudio.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(Edit_Post.this, EditRecord.class);
                i.putExtra("EditVesselId", strVessId);
                i.putExtra("EditActivityId" , strActivityId);
                startActivity(i);

            }
        });


protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode==REQUEST_CODE)
            {
                String audioString=data.getStringExtra("AUDIO_STRING");
                Log.e(" audioString "," = "+audioString);
            }
       }

这是第三项活动

Intent intent = getIntent();
        vesselId = intent.getStringExtra("EditVesselId");
        strActivityId = intent.getStringExtra("EditActivityId");

Intent intent=new Intent(EditRecord.this, Edit_Post.class);
                intent.putExtra("AUDIO_STRING",newAudioFile);
                setResult(REQUEST_CODE, intent);
                finish();

4 个答案:

答案 0 :(得分:2)

在拨打第二个活动时执行此操作

Intent i = new Intent(CustomActionActivity.this, Edit_Post.class);
        i.putExtra("HashTag", strHashTag);
        startActivityForResult(i, REQUEST_CODE);

现在您需要在CustomActionActivity上设置您想要的结果

e.g。

Intent intent=new Intent();  
intent.putExtra("MESSAGE",message);  
setResult(REQUEST_CODE,intent);  
finish();

现在,您将获得此数据到您的第一个活动

e.g。

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

               if(requestCode==REQUEST_CODE)  
                     {  
                        String message=data.getStringExtra("MESSAGE");   
                        textView1.setText(message);  
                     }  
 }  

如果有任何问题,请告诉我

答案 1 :(得分:1)

我这样做是为了我的作品:     活动2:     public void onBackPressed(){         // TODO自动生成的方法存根

    Intent intent = new Intent();
    intent.putExtra("MESSAGE", strtext + "");
    setResult(2, intent);
    if (isclose) {

        finish();

    }  else {

            super.onBackPressed();
        }
    }

}
 Activity1:
 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

        String sSuName = data.getStringExtra("MESSAGE");

        //txtfavouratecount.setText(sSuName);

}


in Your onclick listener
    Intent itemintent = new Intent(context,your target Activity.class);
            Bundle b = new Bundle();
            b.putStringArray("iarray", Qtag);
            b.putInt("mflag", 0);
            itemintent.putExtra("android.intent.extra.INTENT", b);
            startActivityForResult(itemintent,2);

答案 2 :(得分:0)

使用SharedPreferences 将数据存储在SharedPreference中,并从其他活动的SharedPreference中访问它。 将数据添加到第二个活动的onStop中的SharedPreference,并在另一个活动的onCreate中访问它

@Override
public void onStop(){//Where you wish to insert data
    SharedPreferences data=getSharedPreferences(PREFS_FILE,0);
    SharedPreferences.Editor editor= count.edit();
    editor.put("data","DATA");
    editor.apply();
    super.onStop();

}

在另一个Activity的onCreate()中:

SharedPreferences data = getApplicationContext().getSharedPreferences(PREFS_FILE, 0);
String dataString=data.get("Data",0);

希望这会有所帮助。欢呼声。

答案 3 :(得分:0)

我会做这样的事情:

在活动A中:

private static final int REQUEST_CODE = 9001;

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

在活动B中:

Intent data = new Intent();
data.putExtra("key", parameter);
setResult(CommonStatusCodes.SUCCESS, data);
finish();

最后,在活动A中,收到结果:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_CODE) {
        if (resultCode == CommonStatusCodes.SUCCESS) {
            if (data != null) {
                String result = data.getStringExtra("key");
            } 
        } else {
            //Error to receive data
        }
    }
    else {
        super.onActivityResult(requestCode, resultCode, data);
    }
}
祝你好运!