Skip to content Skip to sidebar Skip to footer

How To Receive Data In Previous Activity In Android

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

Solution 1:

do this when you are calling the second activity

Intent i = newIntent(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=newIntent();  
intent.putExtra("MESSAGE",message);  
setResult(REQUEST_CODE,intent);  
finish();

Now you will get this data to the your first activity

e.g.

@OverrideprotectedvoidonActivityResult(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

Solution 2:

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

@OverridepublicvoidonStop(){//Where you wish to insert dataSharedPreferences 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.

Solution 3:

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

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

        finish();

    }  else {

            super.onBackPressed();
        }
    }

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

        StringsSuName= data.getStringExtra("MESSAGE");

        //txtfavouratecount.setText(sSuName);

}


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

Solution 4:

I would do something like this:

In Activity A:

privatestaticfinalint REQUEST_CODE = 9001;

Intent intent = newIntent(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:

@OverridepublicvoidonActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_CODE) {
        if (resultCode == CommonStatusCodes.SUCCESS) {
            if (data != null) {
                Stringresult= data.getStringExtra("key");
            } 
        } else {
            //Error to receive data
        }
    }
    else {
        super.onActivityResult(requestCode, resultCode, data);
    }
}

Good luck!

Post a Comment for "How To Receive Data In Previous Activity In Android"