Change object using intent and startActivityForResult - java

I have an object called Contact which contains an arraylist of type Transactions.
I pass the selected contact (from a listview in activity1) to activity2 using intent and startActivityForResult.
In the second activity I create a new Transaction object and add it to the passed Contact's arraylist of transactions, return it using setResult and retrieve it using onActivityResult
My problem is that the original Contact object doesn't get the new Transaction.
Sending intent:
Intent intent = new Intent(this, AddTransactionActivity.class);
intent.putExtra("contact", selectedContact);
startActivityForResult(intent, 1);
recieving intent:
Bundle b = getIntent().getExtras();
if(b != null) {
recievedContact = (Contact)b.getParcelable("contact");
}
Sending back result:
recievedContact.addTransaction(transaction);
Intent intent = new Intent(this, Contacts_activity.class);
intent.putExtra("contact", recievedContact);
setResult(Activity.RESULT_OK, intent);
finish();
startActivity(intent);
Retrieving result:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == 1) {
if(resultCode == Activity.RESULT_OK) {
Bundle b = data.getExtras();
if(b != null) {
selectedContact = (Contact)b.getParcelable("contact");
}
} else if (resultCode == 0) {
}
}
}
edit: I put a Log.v("result test", "success"); in onActivityResult(), it doesnt show in logcat so it seems my onActivityResult method is never called.

Related

How to pass Parceable in an intent?

I am having a problem receiving the extras that are coming in with my intent from a different activity. When I run the code and use break points at the line where the object is actually being created I can see it being created using the debugger. I can see the object being put in the putExtra() function.
Upon arrival at the resulting activity though using the function getParceableExtra() always returns null upon receiving the intent.
//In the receving activity
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
if(requestCode == 1){
if(resultCode == Activity.RESULT_OK){
Intent extras = getIntent();
if(extras != null){
Goals addToList = extras.getParcelableExtra(GOAL_PASSING);
if(addToList != null)
mGoal.add(addToList);
}
}
}
}
//In the sending activity
incrementMGoal = (findViewById(R.id.create_goal_button));
incrementMGoal.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent returnIntent = new Intent();
Goals newGoal = new Goals(GT,GD,GDueDate);
returnIntent.putExtra(MainActivity.GOAL_PASSING,newGoal);
setResult(Activity.RESULT_OK,returnIntent);
finish();
}
});
I expect when I create the intent to pass putExtra and then receive it to be put in a array. I am just receiving null however.
The Problem is that your code takes the wrong intent and not of the onActivityResult parameter data
Goals addToList = data.getParcelableExtra(GOAL_PASSING);
so instead you should do this
Goals addToList = data.getParcelableExtra(GOAL_PASSING);

Bundle null onActivityResult

I have tried a couple of different solutions already given but none works as everything seems to work fine in another activity that I am returning a result from, here is the code.
My Main activity where City activity is called:
//this method gets called on a button click and it works as other activity shows up
public void getCity(View v){
Intent intent = new Intent(getApplicationContext(), City.class);
startActivityForResult(intent,1);
}
//receiving the data the first data is ok but the second one is null although doing the same thing in both files
protected void onActivityResult(int requestCode, int resultCode, Intent data){
if(resultCode == RESULT_OK && data != null && requestCode==0){
initializeUI(data.getExtras());
}
if(resultCode == RESULT_OK && data != null && requestCode==1){
//data is null here
}
super.onActivityResult(requestCode, resultCode, data);
}
My City Activity
public void addInput(View v){
Bundle bundle = new Bundle();
EditText cityBox = (EditText) findViewById(R.id.cityInput);
String cityName = cityBox.getText().toString();
Intent resultIntent = new Intent();
try {
EditText longBox = (EditText) findViewById(R.id.longitudeInput);
String longitude = longBox.getText().toString();
double longi = Double.parseDouble(longitude);
bundle.putDouble("LONGITUDE", longi);
EditText latBox = (EditText) findViewById(R.id.latitudeInput);
String latitude = latBox.getText().toString();
double lati = Double.parseDouble(latitude);
bundle.putDouble("LATITUDE", lati);
} catch (NumberFormatException e){
}
bundle.putString("CITY_NAME", cityName);
resultIntent.putExtra("DATA",bundle);
setResult(RESULT_OK, resultIntent);
finish(); //calling finish just in case tried without finish aswell
onBackPressed(); //calling onBackPressed tried without it as well doesn't work
}
Any help would be appreciated.
try this:
Then, in B, I do:
Intent intent = getIntent();
intent.putExtra("Date",dateSelected);
setResult(RESULT_OK, intent);
finish();
And, in A:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if(resultCode==RESULT_OK && requestCode==1){
Bundle MBuddle = data.getExtras();
String MMessage = MBuddle .getString("Date");
}
}
the problem i have found in your code is use of wrong requestCode you are passing 1 as result code in startActivityForResult and in onActivityResult you are checking requestCode==0 means intializeUI etc. All you need to do is use this below code.
if(resultCode == RESULT_OK && data != null && requestCode==1){
//everything works fine
initializeUI(data.getExtras());
}
hope this will help you.
I recently faced same issue in my case. What i was doing wrong was that i was using startActivity() by mistake with startActivityForResult() then in child activity i was calling onBackPress() and finish() before setting the result. I can suggest you that you make sure you set the result before finish() the activity and make sure you're starting activity with startActivityForResult().
Here is snippet from my code.
setResult(RESULT_OK, getIntent().putExtra("country", "some result"));
And i made sure the above written code get excuted before onBackPressed() and onFinish() and it worked for me.
Hope this will help you.

Transfer data between two activities [duplicate]

This question already has answers here:
How do I pass data between Activities in Android application?
(53 answers)
How to manage startActivityForResult on Android
(14 answers)
Closed 5 years ago.
I am trying to send and receive data between two different activities. I have seen some other questions asked on this site but no question has dealt with preserving the state of the first class.
For example if I want to send an integer X to class B from class A then to do some operations on integer X and then send it back to class A, how does one go about doing this?
Is it as simple as the following code?
in Class A
Intent i = new Intent(this, ActivityB.class);
i.putExtra("Value1", 1);
startActivity(i);
and to receive the response from Class B:
Bundle extras = getIntent().getExtras();
int value1 = extras.getint("Value1",0);
in Class B
Bundle extras = getIntent().getExtras();
int value1 = extras.getint("Value1",0);
//Do some operations on value1 such as maybe adding or subtracting
Intent i = new Intent(this, ActivityA.class);
i.putExtra("Value1", 1);
startActivity(i);
This does not seem correct as I simply want to switch back to Activity A and receive the data from Activity B once the action is completed (perhaps a button in Activity B commences operations on the received data and then sends it back to Activity A?)
In First activity :
Intent i = new Intent(this, ActivityB.class);
i.putExtra("Value1", "1");
startActivityForResult(i, 100);
Receive data like :
Intent receivedIntent = getIntent();
if(receiveIntent!=null){
String value1 = receiveIntent.getStringExtra("Value1");
}
After some operations In second activity:
Intent returnIntent = new Intent();
returnIntent.putExtra("result",result);
setResult(Activity.RESULT_OK,returnIntent);
finish();
Handle result in FirstActivity :
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 100) {
if(resultCode == Activity.RESULT_OK){
String result=data.getStringExtra("result");
}
}
}
startActivityForResult() and onActivityResult() is your solution.
In your ActivityA. Use startActivityForResult() -
Intent i = new Intent(this, ActivityB.class);
i.putExtra("Value1", 1);
startActivityForResult(i, requestCodeForOperation);
And on your ActivityB, get your data sent from ActivityA. Like -
int value1 = getIntent().getExtras().getInt("Value1", 0);
Do your operation and use setResult() for adding you operation result and finish(). like-
Intent returnIntent = new Intent();
returnIntent.putExtra("result",result);
setResult(Activity.RESULT_OK,returnIntent);
finish();
And of course you need to implement onActivityResult() on ActivityA to get returned data from ActivityB. Like -
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == requestCodeForOperation) {
if(resultCode == Activity.RESULT_OK){
String result=data.getIntExtra("result", 0);
}
}
}

startActivityForResult() returns RESULT_CANCELED

I have 2 Activity classes and 1 Non-Activity class which calls startActivityForResult() from Context passed in constructor. This is how it looks: FirstActivity -> NonActivity -> SecondActivity -> FirstActivity. In SecondActivity there is ArrayList of custom objects that needs to be passed to FirstActivity as a result. There is a problem. When onActivityResult() is called resultCode is RESULT_CANCELED, but not RESULT_OK even if setResult(RESULT_OK, intent) is called. Here is my code:
NonActivity
public void showActivity() {
Intent intent = new Intent(request, ActivityKorak.class);
intent.putExtra("data", fields);
request.startActivityForResult(intent, 1);
}
SecondActivity
#Override
public void onBackPressed() {
super.onBackPressed();
Intent intent = new Intent();
intent.putExtra("data", fields);
setResult(Activity.RESULT_OK, intent);
finish();
}
FirstActivity
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent){
super.onActivityResult(requestCode, resultCode, intent);
if(resultCode != Activity.RESULT_CANCELED){
if(requestCode == 1) {
Bundle extras = intent.getExtras();
ArrayList<CustomInput> fields = (ArrayList<CustomInput>) extras.getSerializable("data");
}
}
}
You must simply remove
super.onBackPressed();
in the onBackPressed Method
What is happening is that "super.onBackPressed()" is setting the result code to "RESULT_CANCELED" and finishing your activity.
In my case, the problem was fixed by adding the SHA-1 and SHA-256 certificate fingerprints given in Android Studio (click on Gradle on the right side of the AS window, then run configurations and signingReport) to your Firebase project settings->General->SDK setup and configuration.

extra value not passed from activity using onActivityResult to fragment

Update
Activity A ==> host MyFragment and from here,user started ==> ActivityB from here I need to get some value from this activty for example value integer and some String to MyFragment that hosted in ActivityA
I've MyFragment that start the activityB like this :
Intent intent = new Intent(getActivity(),NewsCommentActivity.class);
intent.putExtra("MNewsFeed", new Gson().toJson(newsFeed));
intent.putExtra("idCommentBadge",idCommentBadge);
startActivityForResult(intent,addComment);
and then when user start the activty and finish it I'm trying to pass some value from that ActivityB to MyFragment like this :
#Override
public void onBackPressed() {
super.onBackPressed();
//set ok result before finish the activity
Intent returnIntent = new Intent();
returnIntent.putExtra("idBadgeComment", idBadgeComment);
returnIntent.putExtra("totalCommentInserted", totalCommentInserted);
setResult(RESULT_OK, returnIntent);
finish();
}
and I've implement onActivityResult in MyFragment :
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.i(TAG, "Result from News Comment ");
if(resultCode== Activity.RESULT_OK){
Log.i(TAG, "Result from News OK");
if(requestCode==addComment){
Log.i(TAG, "Result ReqCode Oke");
int idBadgeComment = data.getExtras().getInt("idBadgeComment");
int totalCommentInserted = data.getExtras().getInt("totalCommentInserted");
Log.i(TAG, "idComment: "+idBadgeComment);
Log.i(TAG, "Total Comment Inserted: "+totalCommentInserted);
}
}
}
also I've called onActivityResult in ActivityA that hosted MyFragment :
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
/**
* call below code to get the result on the fragment
*/
Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.viewpager);
fragment.onActivityResult(requestCode, resultCode, data);
}
when I try the code above I only get Log.i(TAG, "Result from News Comment "); and it didn't get the RESULT_OK value and the Intent data is null, can someone please pointed out where did I go wrong? and how to pass the value from activity to fragment ? or maybe a better way instead of using onActivityResult?
You can try putting super.onBackPressed(); as the last line since RESULT_CANCELED is returned on back press.
Just startActivity by activity.
Intent intent = new Intent(getActivity(),NewsCommentActivity.class);
intent.putExtra("MNewsFeed", new Gson().toJson(newsFeed));
intent.putExtra("idCommentBadge",idCommentBadge);
getActivity().startActivityForResult(intent,addComment);
Or put your onActivityResult to fragment.

Categories