how to receive data in previous activity in Android - 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 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();

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

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.

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

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!

Related

onActivityResult - resultCode is always 0

I have problem with onActivityResult, whatever I'm doing I can't get resultCode right.
I know that there are similar questions but at the end they didn't help me and I couldn't fix it
MainActivity: method which will open new Activity Popup.class
public void openShopView(){
Intent intent = new Intent(this, Popup.class);
Bundle b = new Bundle();
b.putString("which", "ShopMain");
intent.putExtras(b);
startActivityForResult(intent, 1);
}
Second Activity: method which will open yet another Activity Popup.class just with different layout
shop_c1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(getIntent());
Bundle b = new Bundle();
b.putString("which", "ShopBuildings");
intent.putExtras(b);
startActivity(intent);
finish();
}
});
Third Activity: and there is method which should setResult and close Activity
building2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Log.i("LOG_NEW: ", "" + getCurrentBuildingTable(1) + ", " + checkSlotTable(1));
if(getCurrentBuildingTable(1) && checkSlotTable(1) == -1) {
Intent returnIntent = getIntent();
returnIntent.putExtra("result", 1);
setResult(RESULT_OK, returnIntent);
finish();
}else if (checkSlotTable(1) == -1){
Log.i("LOG_NEW: ", "Building already exist");
}
else{
Log.i("LOG_NEW: ", "Not enough resources");
}
}
});
At the end there is onActivityResult() from MainActivity
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.i("LOG_RES: ", "Checking.. " + requestCode + ", " + resultCode);
if (requestCode == 1) {
if(resultCode == RESULT_OK){
String result = data.getStringExtra("result");
Log.i("LOG_RES: ", result);
}
}
}
Whatever I'm doing I can't start if(resultCode == RESULT_OK) loop and resultCode is always 0..
Thanks for help
setResult must be called in Second Activity, since intent of second activity was passed in startActivityForResult.
However, you can delegate the result code of Third Activity to Second Activity, then to third.
Change your Second Activity to something like this:
shop_c1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(getIntent());
Bundle b = new Bundle();
b.putString("which", "ShopBuildings");
intent.putExtras(b);
startActivityForResult(intent,1);
//Remove finish from here
}
});
then also add this in Second Activity
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==1){
setResult(resultCode,data);
}
finish();
}

FragmentActivity onActivityResult data is null

I know there are several questions about this, but I don't found a solution for my problem.
I have ActivityA which extends AppCompatActivity. It starts an ActivityB
Activity A
Intent intent = new Intent(this, ActivityB.class);
intent.putExtra("data", data);
startActivityForResult(intent, 1);
....
#Override
protected void onActivityResult(int requestCode, int result, Intent intent) {
super.onActivityResult(requestCode, result, intent);
if (requestCode != 1) { // check code
return;
}
if (intent == null) { // HERE INTENT IS NULL
return;
}
}
Activity B
// code called when an asynctask is done
Intent i = new Intent();
i.putExtra("dataone", "test");
i.putExtra("datatwo", objet);
setResult(RESULT_OK, i);
finish();
I don't understand why intent is null in onActivityResult() method.
Two things. I would refactor your code like the following:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == 1) {
if (resultCode == RESULT_OK) {
// example
// String myString = intent.getString("myStringFrom2ndActivity")
}
}
}
and also make sure that you are calling the right RESULT_OK. It should be something like Activity.RESULT_OK.

Get value from returnActivity when backpressed android

I have 2 activities in my Android application, Activity1 and Activity2.
I want to display the data retrieved from a String in Activity2 and this string value set text in Activity 1 textview when back is pressed from Activity2.
Please anyone give the solution for this problem,thanks in advance.
Activity 2: here I pass the string value to Activity 1 when I back press the Activity 2 this will be retrieved to 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: here i retreive the string from Activity 2 and set the string value to my 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) {
}
}
}
You should follow this structure
In 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);
}
}
}
In 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();
}
Hope this help

Using StartActiviytForResult

I have Three Activity A,B,C.
From A activity I send it to B using startActivityforResult and then from activity B I Send it to Activity C using startActivityforResult but when I came back to activity A from all activities with RESULTOK than i got no data on activity A and every thing refresh on bluestack but its working fine on Samsung and Motorola as I had tested on that.
So I want it to be working on all devices.
I had set my activity orientation to Portait statically in all activities.
Code:
Acitivity A:
Intent i = new Intent(v.getContext(),B.class);
startActivityForResult(i, 1);
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if (requestCode == 1) {
String result = data.getStringExtra("imagesreturn");
}
}
}
Activity B:
ImageButton btn = (ImageButton)findViewById(R.id.button_capture);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(CameraUiActivity.this, CameraImageTakenActivity.class);
i.putExtra("image", f.getAbsolutePath());
startActivityForResult(i, 1);
// dialog.dismiss();
}
});
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if(resultCode == RESULT_OK){
String result = data.getStringExtra("images");
Intent intent = new Intent();
intent.putExtra("imagesreturn",result);
setResult(RESULT_OK, intent);
finish();
}
if (resultCode == RESULT_CANCELED) {
//Write your code if there's no result
finish();
}
}
}
Activity C:
final String myimage = b.getString("image");
btnsave.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.putExtra("images",myimage);
setResult(RESULT_OK, intent);
finish();
}
});

NullPointerException on Display Data(from an intent) in a TextView

I am going to build app which shows the bus connection.
My problem is that I get a nullpointer exception when I am putting a string into an intent and want to display this String in a TextView.
Here is my MainActivity:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
HaltestelleName = intent.getStringExtra("HaltestelleName");
final String Zeit = intent.getStringExtra("Zeit");
if(resultCode == Activity.RESULT_OK && requestCode == requestCode)
{
if(rgStatus.getCheckedRadioButtonId() == R.id.rdAbfahrt)
{
txtHaltestelleStart.setText("HTL Neufelden");
txtHaltestelleZiel.setText(HaltestelleName);
}
else
{
txtHaltestelleStart.setText(HaltestelleName);
txtHaltestelleZiel.setText("HTL Neufelden");
}
}
txtZeit.setText(Zeit);
super.onActivityResult(requestCode, resultCode, intent);
}
Here is my Subactivity Code:
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Intent haltestelle = new Intent(getApplicationContext(),
PlanActivity.class);
String item = ((TextView)view).getText().toString();
haltestelle.putExtra("HaltestelleName", item);
setResult(RESULT_OK,haltestelle);
finish();
}
hi please add the line
haltestelle.putExtra("Zeit", "yours passed value");
just below
haltestelle.putExtra("HaltestelleName", item);
Hi You didn't pass any value in your subactivity in "Zeit" name just pass some value from your sub activity to avoid null pointer exception
#Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
HaltestelleName = intent.getStringExtra("HaltestelleName");
final String Zeit;
if(resultCode == Activity.RESULT_OK && requestCode == requestCode)
{
if(rgStatus.getCheckedRadioButtonId() == R.id.rdAbfahrt)
{
txtHaltestelleStart.setText("HTL Neufelden");
txtHaltestelleZiel.setText(HaltestelleName);
}
else
{
txtHaltestelleStart.setText(HaltestelleName);
txtHaltestelleZiel.setText("HTL Neufelden");
}
}
if (!getIntent().hasExtra("HaltestelleName")) {
Zeit = intent.getStringExtra("Zeit");
txtZeit.setText(Zeit);
}
super.onActivityResult(requestCode, resultCode, intent);
}
The simple logic behind sending the data across two activities:
Activity A:
Intent intent = new Intent(ActivityA.this);
intent.putExtra("key", "value");
startActivity(intent);
Activity B:
Intent intent = getIntent();
String value = intent.getStringExtra("key");

Categories

Resources