onActivityResult doesn't work? - android

I am facing with a problem related startActivityForResult()
To start SecondActivity from FirstActivity :
Intent intent = new Intent();
intent.setClass(FirstActivity.this, SecondActivity.class);
intent.putExtra("key1", "12345");
startActivityForResult(intent, 0);
And handles result :
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//TODO handle here.
}
To send the message to FirstActivity from SecondActivity :
in SecondActivity :
setResult(0);
I can't handle the result on onActivityResult in FirstActivity.
It never works for my application.
My OS is : 1.5
What is wrong here?

startActivityForResult is meant to be used for situations where you want to select a piece of data, or perform some sort of action that your Activity or application cannot do.
For example, you want to pick a contact, so you launch the contacts application, the user chooses the person they want, and you get sent the result. Or you want to take a photo, so you launch the camera application and ask it to send you the photo once it's done. This action is completely separate from your first activity that calls startActivityForResult.
The Activity you're launching will not send you the result until that Activity has completed, i.e. finish() has been called.
So in your case, you need to call this in SecondActivity:
setResult(...);
finish();
before FirstActivity will receive the result in its onActivityResult method. Of course, this means that SecondActivity is now gone and FirstActivity is top of the stack again.
It's not possible to send the result to FirstActivity then close it while keeping SecondActivity still active. In this case you should just handle whatever this 'result' is in SecondActivity, or send it off to a Service you define to do whatever processing it is you want.

I was stuck here for a while. Adding my problem here to make sure that you don't scratch your head as well.
The second parameter of this function has to be 0 or higher.
startActivityForResult(intent, 0); // <- this is OK
I was setting the second parameter to RESULT_OK, which is -1, and my onActivityResult callback was never getting called. So if you get stuck like me, you can also check if your second parameter is correct.
startActivityForResult(intent, RESULT_OK); // <- this is wrong
The above line will fail to call onActivityResult.

I was also stuck on the same problem - but due to a different reason as matangs. Apparently startActivityForResult only works if you have android:launchMode set to standard for main activity (in manifest). Hope it helps someone.

Your code seems ok, but do you stop your second activity ?
Try this in it :
setResult(0);
finish();

If you are doing actions on onPause (like unbinding a service) try to comment it and see if onActivityResult is called (I wasted few good hours on this..)

Thanks to #johndodo (that point to the manifiest) - I find my solution for the same problem.
removing android:noHistory=true at the manifiest" solved this problem for me.

Related

setResult works MOST of the time

I've worked with setResult(RESULT_OK, intent) for a while now, and am experiencing something strange.
MOST of the time, about 3/4 of the time that I enter an activity then exit from it using the following code which is ALWAYS executed:
Intent intent = new Intent();
intent.putExtra(KEY_ENVIRONMENT_SURVEY, esurvey);
setResult(RESULT_OK, intent);
finish();
In the activity being returned to, most of the time, RESULT_OK is returned, but sometimes RESULT_CANCELLED is returned, seemingly quite randomly. What could be causing this? Could finish() be called before the esurvey is attached (it's parcelable) and the default is RESULT_CANCELLED? If so how can I fix this? Use a handler and use postDelayed or check to see if RESULT_OK was set (I don't know how to do this, so please specify if that's the solution)?
Thanks!
Try to move this code into overridden finish() method. This will ensure it's called every time activity is finished.
#Override
public void finish() {
Intent intent = new Intent();
intent.putExtra(KEY_ENVIRONMENT_SURVEY, esurvey);
setResult(RESULT_OK, intent);
super.finish();
}
I'm revisiting this because I found the glitch in my application. The "back" button was very close to a button I had designated as moving to the next activity. I was allowing button presses to be registered after the first one, so sometimes the activity would seem to have completed by touching the "done" button, but was actually being navigated back to by the "back" button.
I fixed this using a boolean flag, checking to see if it was true upon a butotn press, and then setting it to true if it equaled false.

android launched activity exit callback function

I am launching the android setting activity, from an android service.
Intent LaunchIntent = getPackageManager().getLaunchIntentForPackage("com.android.settings");
startActivity(LaunchIntent);
I am searching, how I can detect if the setting activity is closed,As I need some callback method.
If there is a callback method to know the settings or any other app like browsers,if launched in this method to know if the launched activity is exit its own.
Since settings and browsers are general code we can't put broadcast code in these activities.
Use startActivityForResult to launch the settings activity like so:
Intent LaunchIntent =
getPackageManager().getLaunchIntentForPackage("com.android.settings");
startActivityForResult(LaunchIntent, 42);
Usually, you would use a specific request code as the second argument, but in this case, you have no control over what the settings Activity could return as a result, and you only want to know when it finishes, so you can essentially make up a request code. It must be greater than 0, however. The docs state this here:
requestCode If >= 0, this code will be returned in onActivityResult() when the activity exits.
Then, you can override the onActivityResult method to handle what happens when the settings activity closes:
#Override
protected void onActivityResult (int requestCode, int resultCode, Intent data){
// Do whatever you would like to do
}
If you had used a specific request code when you started the Activity, this is where you would check if the result code exists, but since we aren't expecting any real result, the result code will likely be equal to RESULT_CANCELLED, but that's okay since you at least know that the Activity was cancelled.

Android system stops application when launching Media Intent

I am launching a media intent to take a photo. After the user took a picture and gets back to the previous activity the whole application has restarted.
As this doesn't happen all the time, I think my application goes to the background and android kills it when the device has low memory.
Is there any way to keep my application from going to the background?
Thanks!
This is normal behavior in Android, all activities currently not visible on screen (after onStop) can be killed without notice (i.e. onDestory) if the system has low memory.
This usually happens to our app on one of our test devices which has memory issues regardless of our app.
You can usually recreate this behavior when you open the camera via the intent, and then rotate the device (portrait to landscape), this will also kill and re-create your app.
To solve this you need to extend onSaveInstanceState and use the savedInstanceState parameter in your onCreate to pass from your killed instance to your new instance some important information (like "we're in the middle of getting a pic from the camera").
I can think of two possibilities...
It's not killing the activity, but the intent launches a new activity. You can stop this by putting a tag in your manifest.xml as an attribute in the activity tag like this:
<activity
android:name=".nameOfActivity"
android:launchMode="singleTop" />
Make sure that the media intent is under the activity to handle the photo, and not a main/launcher activity.
IMO two options:
Implement onSaveInstanceState/onRestoreInstanceState
or
Make your activity a service.
I faced this issue and got a solution after R&D for it.
Set Target Android 4.0 and then add this line in AndroidManifest.xml of activity:
android:configChanges="screenLayout|uiMode|orientation|screenSize|smallestScreenSize"
It works for me.
When you start media intent use following method instead of startActivity(intent)
startActivityForResult(intent, REQUEST_CODE); //private int REQUEST_CODE = 232
When the started activity finishes, your calling activity will be started. You need to handle this using following function
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {
//Perform your task
}
}
The activity started need to override follwoing method
#Override
public void onBackPressed() {
Intent intent = new Intent();
setResult(REQUEST_CODE, intent);
super.onBackPressed();
}
startActivityForResult() is an special way for starting activity for a specific task and get the desired result. The called activity will send the data back.
You can use the method putExtra() & getExtra in intent to send and receive data between two activity.
This will solve your problem hopefully.
If you have doubts comment. And if possible share your code so it can be more clear.

Oncreate()is called after setResult instead of OnActivityForResult() in 2.2.1 LG-P500

In my application I use the startActivityForResult() and get the result using SetResult (request code,data). It is working fine, but in LG devices the setResult() is not getting called directly. First it is calling the onCreate() and then calling the onActivitForResult()
Below is my code
FirstActivity Button Click event
startActivityForResult(new Intent(TestActivity.this, Details.class),1);
call the onactivity result of first activity in second activity using below code
Intent data=new Intent();
data.putExtra("TEST", true);
setResult(2, data);
Details.this.finish();
Give me some suggestions for this.Thanks in advance
Please make sure u are not finishing the calling activity.......
it was crazy. I spend much for solving this issue and as result following fixed this issue:
turn off "Developer options" -> Apps -> "Don`t keep activities" option

How can I receive CameraActivities result in a DIFFERENT Activity (i.e. not in the launching one)?

I've got Activity A which fires up the Camera intent via:
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, REQUEST_CAMERA);
After the picture is taken I can easily grab the picture in:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
But I'd like to receive the result in Activity B in which the image can be edited.
Right now I'm receiving the result in Activity A and pass it over to Activity B which results in showing the GUI of Activity A for a short while:
Intent i = new Intent().setAction("DisplayJPEG");
i.setClass(this, EditImageActivity.class);
i.putExtra("IMAGE_URI", uri);
startActivityForResult(i, REQUEST_EDIT_IMAGE);
Of course, I will need the result from Activity B in Activity A after the image has been edited. But that should work with:
setResult(resultCode, data);
So there has to be a way to do what I need. Please point me into the right direction.
Have you tried launching ActivityB, and in ActivityB onCreate event launch the Camera Intent?
You technically can't do what you're asking. You'll need to find a way to continue passing it the way you are and hide the UI or do as Pentium says and do it the other way around.
Edit: Nevermind, I misread how this works. What actually happens is you can use Activity A to start Activity B for result, but then if Activity B needs to start Activity C to continue processing whatever Activity A wanted, you can use FLAG_ACTIVITY_FORWARD_RESULT to make Activity C return its result to Activity A not B.
I haven't looked into this more than a quick glance, but I noticed an Intent flag called FLAG_ACTIVITY_FORWARD_RESULT which according to the documentation:
If set and this intent is being used to launch a new activity from an existing one, then the reply target of the existing activity will be transfered to the new activity. This way the new activity can call setResult(int) and have that result sent back to the reply target of the original activity.
Like I said, I haven't experimented with this, but that seems to suggest that you could launch your camera intent from Activity A but have it forward its result to Activity B.

Categories

Resources