setResult(<resultCode> , <Intent>) does not work - android

I'm trying to pass byteArray and just a String from a current activity to a previous fragment.
public void uploadClick (View view) {
//Send results back to previous screen
switch (activity.from) {
case "review" :
if (activity.mCurrentPhotoPath != null && activity.bytes != null) {
Intent reviewReturnIntent = new Intent();
reviewReturnIntent.putExtra(ReviewsFragment.REVIEW_PHOTO_BITMAP, activity.bytes);
reviewReturnIntent.putExtra(ReviewsFragment.REVIEW_PHOTO_PATH, activity.mCurrentPhotoPath);
activity.setResult(Activity.RESULT_OK, reviewReturnIntent);
activity.finish();
}
break;
case "reply" :
break;
case "merchant" :
break;
}
}
Fragment onActivityResult :
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REVIEW_REQUEST) {
if (resultCode == getActivity().RESULT_OK) {
if (data != null) {
returnedBitmap = data.getByteArrayExtra(REVIEW_PHOTO_BITMAP);
returnedPhotoPath = data.getStringExtra(REVIEW_PHOTO_PATH);
Bitmap bmp = BitmapFactory.decodeByteArray(returnedBitmap, 0, returnedBitmap.length);
binding.reviewIV.setImageBitmap(bmp);
binding.returnedImageLayout.setVisibility(View.VISIBLE);
}
}
}
}
startActivityForResult call:
public void uploadClick (View view) {
Intent uploadPhotoIntent = new Intent(fragment.getContext(), UploadPhotoActivity.class);
uploadPhotoIntent.putExtra(UploadPhotoActivity.FROM, "review");
fragment.startActivityForResult(uploadPhotoIntent, ReviewsFragment.REVIEW_REQUEST);
}
Every time I try to run the activity.setResult with an intent parameter. The compiler runs that code, but does not finish the activity (therefore I could not return to the previous fragment / activity).
I've added breakpoints and it runs the "setResult" and the "activity.finish()" methods. But it just doesn't seem to destroy the current activity.
Although, after taking out the intent parameter from the "setResult" method, everything works fine, but on the previous fragment I'm getting a null returned from "data".
Example :
activity.setResult(Activity.RESULT_OK);
Been trying to look for answers around the internet, still no luck.
Would really appreciate it , and thanks for reading. :)

Related

How to make an intent and launch an activity in onActivityResult()?

I am trying to launch an activity after a user has selected a photo. I was trying to do this:
uploadImageButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent selectImageIntent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
selectImageIntent.setType("image/*");
startActivityForResult(selectImageIntent, 1);
Intent goToActivityIntent = new Intent(view.getContext(), SendPhotoChangeActivity.class);
goToActivityIntent.putExtra("email", email);
goToActivityIntent.putExtra("donorEmail", donorEmail);
goToActivityIntent.putExtra("orderId", orderId);
goToActivityIntent.putExtra("uriString", uriString);
view.getContext().startActivity(goToActivityIntent);
}
});
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK && data != null) {
uriString = data.getData().toString();
}
}
But I realised that with this code, the code for launching the activity (SendPhotoChangeActivity) executes before the user selects the image, crashing the app because the uriString variable is null.
I tried simply copy/pasting the code into onActivityResult(), but the view variable (in view.getContext()) was, of course, not recognized in onActivityResult().
I am thinking of simply replacing view.getContext() by getApplicationContext() in onActivityResult(). Is this the right thing to do? If not, please tell me how I can start an activity in onActivityResult().
If you are in Activity then you can just use this as Context
Intent goToActivityIntent = new Intent(this, SendPhotoChangeActivity.class);
If you are in a Fragment then you can obtain Context by calling getContext()
Intent goToActivityIntent = new Intent(getContext(), SendPhotoChangeActivity.class);
And use that code inside onActivityResult() as you were trying to.
set an integer code for the act of selecting a picture like REQUEST_CODE_TAKE_PICTURE so you know that has happened, this works ok in Kotlin, I assume that works as well with java:
if (requestCode == REQUEST_CODE_TAKE_PICTURE && resultCode == Activity.RESULT_OK) {
Intent goToActivityIntent = new Intent(view.getContext(),SendPhotoChangeActivity.class);
goToActivityIntent.putExtra("email", email);
goToActivityIntent.putExtra("donorEmail", donorEmail);
goToActivityIntent.putExtra("orderId", orderId);
goToActivityIntent.putExtra("uriString", uriString);
view.getContext().startActivity(goToActivityIntent);
if (data == null) {
//Display an error
println("error accuered at onActivityResult ")
return
}
Have you tried this simpler one:
uploadImageButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent selectImageIntent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
selectImageIntent.setType("image/*");
startActivityForResult(selectImageIntent, 1);
}
});
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK && data != null) {
uriString = data.getData().toString();
Intent goToActivityIntent = new Intent(view.getContext(), SendPhotoChangeActivity.class);
goToActivityIntent.putExtra("email", email);
goToActivityIntent.putExtra("donorEmail", donorEmail);
goToActivityIntent.putExtra("orderId", orderId);
goToActivityIntent.putExtra("uriString", uriString);
startActivity(goToActivityIntent);
}
}
Just call
startActivity(goToActivityIntent);
to call the activity.
This assumes you are calling it from your activity or fragment. If this doesn't meet your requirements, let me know. There are other ways to implement this.

How to handle getResults from multiple activities?

I am trying to get results back from intents in android studio.
In my main I start an activity and use startActivityForResult(intent, 1)
I then use get results in mainActivity from activity 2's setResults()
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1) {
if(resultCode == RESULT_OK){
Bundle extras = data.getExtras();
if (extras != null) {
String name = extras.getString("FIRSTNAME");
String Lname = extras.getString("LASTNAME");
int ID = extras.getInt("ID");
//TODO: Get the list fragment to newinstance with out new arraylist
Person p = new Person(name, Lname, ID);
people.add(p);
getFragmentManager().beginTransaction().replace(R.id.content_main, FullList.newInstance(people)).commit();
}
In my fragment in activity 1 I am calling a new startActivityForResult(i, 2)
How do i get my main activity to grab the setResults()
from Activity 3?
Activity 3 is doing this:
Intent deleteIntent = new Intent();
deleteIntent.putExtra("FNAME", first);
deleteIntent.putExtra("LNAME", last);
deleteIntent.putExtra("ID", num);
setResult(RESULT_OK, deleteIntent);
finish();
I am trying to have my main activity call if (requestCode == 2)
But it works to no avail.
Here is the all the onActivityResult for reference:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1) {
if(resultCode == RESULT_OK){
Bundle extras = data.getExtras();
if (extras != null) {
String name = extras.getString("FIRSTNAME");
String Lname = extras.getString("LASTNAME");
int ID = extras.getInt("ID");
//TODO: Get the list fragment to newinstance with out new arraylist
Person p = new Person(name, Lname, ID);
people.add(p);
getFragmentManager().beginTransaction().replace(R.id.content_main, FullList.newInstance(people)).commit();
}
// NOW SEEING IF THE DETAILS SCREEN PASSED BACK RESULTS
} else if (requestCode == 2) {
if (resultCode == RESULT_OK) {
Bundle extras = data.getExtras();
if (extras != null) {
String name = extras.getString("FNAME");
String Lname = extras.getString("LNAME");
int ID = extras.getInt("ID");
Person p = new Person(name, Lname, ID);
// Delete happens here //
if (people.contains(p)) {
people.remove(p);
// If empty show blank frag, if not, update list //
if (people.isEmpty()) {
getFragmentManager().beginTransaction().replace(R.id.content_main, BlankList.newInstance());
} else {
getFragmentManager().beginTransaction().replace(R.id.content_main, FullList.newInstance(people)).commit();
}
} else {
Toast.makeText(this, "DIDNT RECEIVE SAME INFO", Toast.LENGTH_SHORT).show();
}
}
}
}
// END ELSE CHECK
}
}
Here is the code that is calling the startActivityForResult() in the Fragment on activity 1.
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
//super.onListItemClick(l, v, position, id);
ArrayList<Person> people = (ArrayList<Person>) getArguments().getSerializable(ARG_People);
if (people != null && position != -1) {
Person listPerson = people.get(position);
Intent i = new Intent("OPENDETAILS");
i.putExtra("NAME", listPerson.name);
i.putExtra("LASTNAME", listPerson.lName);
i.putExtra("ID", listPerson.ID);
startActivityForResult(i, 2);
} else {
Toast.makeText(getActivity(), "EMPTY LIST ERROR", Toast.LENGTH_SHORT).show();
}
}
It's a little unclear what you're trying to do, but it sounds like:
Activity1 starts Activity2 for result
Activity2 starts Activity3 for result
Activity3 returns a result
Activity1 is expected receive Activity3's result.
If I got that right then the key element that seems to be missing here is that you are expecting Activity1 to get a result from Activity3 even though it was Activity2 that started it for result. In this case you should implement onActivityResult in Activity2, handle the results coming back from Activity3 and set them as Activity2's results to pass back to Activity1 and then finish; An activity will only receive results from activities it directly starts via startActivityForResult.
Use different code to launch different activities,
such as
startActivtityForResult(new Intent(this,Person1.class),1);
startActivtityForResult(new Intent(this,Person2.class),2);
startActivtityForResult(new Intent(this,Person3.class),3);
then on activityresult
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (resultCode) {
case 1:
//implement your code here
break;
case 2:
//implement your code here
break;
case 3:
//implement your code here
break;
}
}
then set Retrun result in these classes
Person1.class
return_intent.putExtra("result", 1);
setResult(1, return_intent);
Person2.class
Person1.class
return_intent.putExtra("result", 2);
setResult(2, return_intent);
Person3.class
Person1.class
return_intent.putExtra("result", 3);
setResult(3, return_intent);

startActivityForResult resultcode always 0

I'm trying to get edited list back from activity 2 to activity 1.
Here is my code:
public void listDataSms(ArrayList<MySmsLog> stringList) {
Bundle bundle = new Bundle();
bundle.putParcelableArrayList(NUMBER_LIST, stringList);
Intent i = new Intent(this, MyCommonListActivity.class);
i.putExtra(WHO_INT, SMS_LOG);
i.putExtras(bundle);
startActivityForResult(i, SMS_LOG);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == SMS_LOG) {
if (resultCode == RESULT_OK) {
ArrayList<MySmsLog> mySmsLogs = (data.getParcelableArrayListExtra(PICK_SMS_LOG));
mainLog.setSmsLog(mySmsLogs);
}
if (resultCode == RESULT_CANCELED) {
// do something if there is no result
}
}
}
#Override
public void onBackPressed() {
super.onBackPressed();
Intent intent = new Intent();
Bundle result = new Bundle();
switch (who) {
case SMS_LOG:
result.putParcelableArrayList(MainActivity.PICK_SMS_LOG, mySmsLogList);
break;
}
setResult(RESULT_OK, intent);
intent.putExtras(result);
}
But I never get setSmsLog because resultCode is always 0.
I tried Android onActivityResult is always 0 this but with no result.
Even if I change my condition to if (resultCode == RESULT_CANCELED) {do smth} program ends with NullPointerException.
Assuming that onBackPressed() from your code shown above is from MyCommonListActivity, that is too late to call setResult(). At best, your code might work if you call super.onBackPressed() as the last thing, not the first. The typical solution is to call setResult() and finish() as soon as the user taps on something in a ListView or otherwise chooses the particular item to work with, rather than wait until the user presses BACK.
Try to put super.onBackPressed(); after intent.putExtras(result);

Why does onActivityResult return, but I am getting the correct requestCode?

I'm building an application where the first Activity starts another one by startActivityByResult. After some setting is done, I need to send the setting result back.
So I override the second Activity's onPause() method, I get the intent, putExra, and send it back through setResult().
Back to the first Activity. onActivityResult has definitely been called. I got the resultCode set before, but the intent data is null. I can't figure out what's wrong.
Here's my code:
The first Activity
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.e("page1", requestCode + "_");
Log.e("page1", resultCode + "_");
Log.e("page1", (data == null) + "_");
// if ((requestCode == 1) && (data != null)) {
if (data != null) {
String size = data.getStringExtra("size");
Log.e("switch", size + "_");
switch (size.charAt(0)) {
case '1': {
text.setTextSize(10);
}
break;
case '2': {
text.setTextSize(20);
}
break;
case '3': {
text.setTextSize(30);
}
break;
}
}
}
My second Activity
#Override
protected void onPause() {
Intent intent = getIntent();
Log.e("prefs", (intent == null) + "_");
intent.putExtra("size", size);
setResult(3, intent);
super.onPause();
}
I've tested in LogCat. In the second Activity. The intent about to be sent is definitely not null. But when it goes to the first Activity. Null just returned. This is driving me really crazy.
Problem with your code is in String size = data.getStringExtra("size"); this line.
You should change it with String size = data.getExtras.getString("size");
In your first activity:
Intent intent = new Intent(context,SecondActivity.class);
startActivityForResult(intent, REQUEST_CODE);
And in your second activity make a button called SAVE and in its listener;
Intent result = new Intent();
Bundle bundle = new Bundle();
bundle.putString("keyName", "KeyValue");
result.putExtras(bundle);
setResult(RESULT_OK,result);
finish();
And again in your first activity's onActivityresult method:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(resultCode==RESULT_OK){
if(requestCode == REQUEST_CODE){
Log.d("Activity Result", data.getExtras.getString("keyName");
}
}
}
Can you try this.
you want to finish your activity in all the cases in pause manually and in back press automatically so it will be better to save your intent data in onDestroy
#Override
protected void onPause() {
super.onPause();
finish();
}
#Override
public void onDestroy() {
Intent intent = getIntent();
intent.putExtra("size", size);
setResult(3, intent);
super.onDestroy();
}

Android Built-in Camera Interface wont return image and crashes on cancel

I'd like to ask for your help concerning Android's built in Camera Interface
When i click capture, the screen captures(meaning it freezes)
But when i click ok, it hangs. It's suppose to go back to the main interface but it doesn't.
When i press cancel, it would force close.
the code is pretty long but this is my listener:
#Override
protected void onActivityResult(int requestCode, int
resultCode, Intent data)
{
super.onActivityResult(requestCode,
resultCode, data);
Double x = null;
Toast.makeText(mContext, x.toString(),Toast.LENGTH_LONG);
if (resultCode == Activity.RESULT_OK) {
switch (requestCode) {
case CAMERA_PIC_REQUEST:
image.setImageURI(uri);
break;
}
}
}
thanks..
This is the intent to call the camera interface.
Intent intentCamera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intentCamera, CALL_CAMERA);
`protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent){
if(requestCode == CALL_CAMERA && resultCode == RESULT_OK)
{
if(imageReturnedIntent.getAction() != null)
{
// display image received on the view
Bundle newBundle = imageReturnedIntent.getExtras();
newBitmap = (Bitmap) newBundle.get("data");
if(newBitmap != null)
{
ImageView imageViewProfilePicture = (ImageView) this.findViewById(R.id.imageViewProfilePicture);
imageViewProfilePicture.setImageBitmap(newBitmap);
imageViewProfilePicture.invalidate();
}
}
}}`
Solved!
The startActivityForResult() sub needs to be within the class. I placed mine inside my onclick which was inside my onCreate sub.
Thanks guys:D

Categories

Resources