1

I want to receive data in previous Activity from next activity like (1 <--- 2 ). I tried but data is not received from second to first activity .

This is First Cativity

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

This is second Activity

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

This is Third Activity

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();
p. ld
  • 585
  • 3
  • 10
  • 23
  • 1
    but for this your first activity must start your second activity via startActivityForResult make sure you are doing this. Are you doing this ? – Sandy Dec 08 '15 at 09:55
  • No Actually i use only like this Intent i = new Intent(First.this, Second.class); i.putExtra("HashTag", strHashTag); startActivity(i); – p. ld Dec 08 '15 at 09:57
  • you must use `startActivityForResult(..)`in FirstActivity then use existing code in secondActivity. – Devendra Singh Dec 08 '15 at 09:59
  • Strange that such questions get +1s – Sufian Dec 08 '15 at 10:38
  • Possible duplicate of [onActivityResult() not being called in activity](http://stackoverflow.com/questions/26239761/onactivityresult-not-being-called-in-activity) – Sufian Dec 08 '15 at 10:41

4 Answers4

2

do this when you are calling the second activity

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

Now you need to set the result what you want on CustomActionActivity

e.g.

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

Now you will get this data to the your first activity

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

let me know in case of any issue

Sandy
  • 985
  • 5
  • 13
0

Use SharedPreferences Store the data in a SharedPreference and access it in from the SharedPreference in the other activity. Add the data to the SharedPreference in the onStop for the 2nd Activity and access it in the onCreate of the other Activity

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

}

in onCreate() of the other Activity:

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

Hope this helps. Cheers.

Anuraag Baishya
  • 874
  • 2
  • 11
  • 26
0

I am doing this way its works for me: Activity 2: public void onBackPressed() { // TODO Auto-generated method stub

    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);
MurugananthamS
  • 2,395
  • 4
  • 20
  • 49
0

I would do something like this:

In Activity A:

private static final int REQUEST_CODE = 9001;

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

In Activity B:

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

And finally, in the Activity A, receive result:

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

Good luck!

Jose Angel Maneiro
  • 1,226
  • 9
  • 20