onActivityResult doesn't get called within Dialog Fragment - java

So, I've read some threads about this, but I can't get this to work.
Basically I have this Dialog in which the user chooses to take a new pic or select a pic from their gallery. Here is the code:
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setCancelable(true)
.setItems(R.array.galeria_camera_array, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int userChoice) {
if (userChoice == 1) {
// take photo
}
if (userChoice == 0) {
Intent galleryIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(galleryIntent, 1);
}
}
});
return builder.create();
}
And then, the onActivityResult:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//choose file from gallery
}
Can anyone help? Before I have to re-read 10 pages worth of theory again... I'm quite new to this kind of things (onResult). Thank you.

Try implementing something like this in the activity where the dialog fragment is fired from:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.myFragmentId);
fragment.onActivityResult(requestCode, resultCode, data);
The underlining activity is what will receive onActivityResult, so u should be able to 'point' it towards onActivityResult of the dialog fragment.

The easy way to fix this is to move your fragment's onActivityResult into your activity, or you can use your activity's onActivityResult to call a method in your fragment.
I looked at my code and apparently I managed to get around this by calling this.getActivityForResult from my fragment, and then the result returned to my fragment, but I'll be honest with you: I don't remember writing this.

Ok, these two things made it work:
getActivity().startActivityForResult(galleryIntent, 1); You have to add getActivity()
As you guys said, you have to declare the onActivityResult() in the Activity itself.
Thanks guys!

Related

startActivity for result does not called on fragment

I'am try to use start Activity for result to pick photo from gallery but it does not called on fragment
I checked most solutions but no one work for me
please help me
newsImg.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
intent=new Intent(Intent.ACTION_PICK);
intent.setType("image*//*");
getActivity().startActivityForResult(intent,GALLERY_INTENT);
}
});
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==GALLERY_INTENT && resultCode== Activity.RESULT_OK){
Uri uri=data.getData();
StorageReference filePath=mStorage.child("Photos").child(uri.getLastPathSegment());
filePath.putFile(uri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
#Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
downloadUri=taskSnapshot.getDownloadUrl();
Picasso.with(getActivity().getApplicationContext()).load(downloadUri).fit().centerCrop().into(newsImg);
Toast.makeText(getActivity().getApplicationContext(),"success",Toast.LENGTH_SHORT).show();
publishBtn.setEnabled(true);
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
Toast.makeText(getActivity().getApplicationContext(),"fail",Toast.LENGTH_SHORT).show();
}
});
}
Try using this and then find out, before giving my answer I would suggest you to be please do some research or search the exact thing in short form like you want to use the use the gallery intent, just use the image picker in android or gallery intent in android. For going from the fragment we use the getContext() rather than using ActivityIntent like MainActivity.this.
For Calling the startActivity for result do like this :
private final int REQUEST_CODE = 1
class ImagePicker extends Fragment{
/*here we are calling the start activity for result
supposing you know how to use the gallery intent so just giving you
the answer what you want */
Intent intent = new Intent(getContext(), YourClass.class);
startActivityForResult(intent, REQUEST_CODE);
}
And in onActivityResult() you get the result in the same Fragment or Activity after finishing this.
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {
//get your gallery image uri data here
}
}
Hope it helps and please refer to this link to read more about the how to get the data in the same Activity.
About startAcitivityForResult in android
Hope that helps! Thanks
EDITS
Since your code seems fine for gallery intent but I think there is problem with your gallery intent. Try this also and see if it works.
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, RESULT_CODE;
Here specifically I have used the inten.setType("image/*") and one more thing, make this simple, do not use getActivity.startActivityForResult() just make it simple, startAcitivityForResult() will also work fine and use getContext() despite of doing getActivity().getApplicationContext(). Try amending this thins things and tell me whether it works or not!

App Crashing After Selecting Picture From Gallery

So whenever I select a picture from the gallery in my app, it crashes. Here is the code for the button to the gallery and selected picture to the imageview.
pickImageButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
openGallery();
}
});
}
private void openGallery() { //opens the gallery
Intent gallery =
new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
startActivityForResult(gallery, PICK_IMAGE);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK && requestCode == PICK_IMAGE) {
Uri imageUri = data.getData();
imageView2.setImageURI(imageUri);
}
}
Please comment if you need more information, I desperately need help, as this is a very major roadblock for me.
Without the Logs, I can recommend on the following:
Make sure that pickImageButton isn't null.
Make sure that imageView2 isn't null.
Make sure that in manifest your Activity is in:
android:launchMode="singleTop"

startActivityForResult out an activity

How to use startActivityForResult outside an activity context say in a regular java class
EX:
In the main activity, Im calling method of SampleClass. In this method i have to open the device camera and obtain the image and process it.
The problem is the control is not going to the onActivityResult callback method inside the SampleClass but instead it is going to onActivityResult callback method inside the MainActivity. What am i doing wrong?
I need to be able to handle the image inside Sample Class only. How can i achieve this
Code Snippet
MAIN ACTIVITY:
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
...
...
SampleClass sample = new Sample(this);
sample.openCamera();
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == 0){
if(resultCode == RESULT_OK){
syso("INSIDE MAIN ACTIVITY");
}
if(resultCode == RESULT_CANCELED){
...
}
}
}
}
SAMPLE CLASS:
public class SampleClass extends Activity{
private Context context;
public SampleClass(Context context){
this.context = context;
}
public void openCamera(){
Intent photoIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
((Activity) context).startActivityForResult(photoIntent, 0);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == 0){
if(resultCode == RESULT_OK){
//get the image
// process it.
syso("INSIDE SAMPLE CLASS");
}
if(resultCode == RESULT_CANCELED){
}
}
}
}
If i run this program, The output on the console is INSIDE MAIN ACTIVITY. What i want is to be able to get INSIDE SAMPLE CLASS as the output.
Thanks and Regards
Instead of using
SampleClass sample = new Sample(this);
sample.openCamera();
just use
Intent intent = new Intent(this, SampleClass .class);
and write SampleClass onCreate call openCamera().
Hope this will help.
the problem is that you open the camera using MainActivity Context!!
sure will triggered onActivityResult() of MainACtivity.
((Activity) context).startActivityForResult(photoIntent, 0);
see context object = MainActivity check you SimpleClass constructor.
public SampleClass(Context context){
// when you call from mainActivity and send **this** it means context = MainActivty
this.context = context;
}
if you want SampleClass onActivityResult() triggered there is no way else extends Activity
then call it like normall way ....
Intent photoIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
context.startActivityForResult(photoIntent, 0); // context must = SampleClass (); no other way
but if you still want to handel image in SimpleClass then follow this code remove extends Activity
in SimpleClass
public static void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == 0){
if(resultCode == RESULT_OK){
//get the image
// process it.
syso("INSIDE SAMPLE CLASS");
}
if(resultCode == RESULT_CANCELED){
}
}
}
and in you MainActivity class
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
SimpleClass.onActivityResult(requestCode,resultCode,data);
}
never ever create new object from activity using new key word Adnroid OS will do that for you when you say
Itnent foo = new(context,foo.class);
and if you did maybe this means hack Code and android OS.
i hope you understand and hope this helps
It is impossible. onActivityResuly will be called on that activity, which have ran another activity.
So if you took activity1 instance to run another activity, another activity will call activity1.onActivityResult()

onActivityResult not called

There are many question here from people having the exact same problem as I, and I've looked though a million, tried different things for 2-3 hours now, and I still can't get it working.
Child Activity:
Intent resultIntent = new Intent(myColorPicker.this, WidgetConfig.class);
resultIntent.putExtra("key", appwidget_notecolor);
setResult(RESULT_OK, resultIntent);
finish();
Parent Activity:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Toast does not show
Toast.makeText(getApplicationContext(), "onActivityResult fired", Toast.LENGTH_SHORT).show();
if (resultCode == RESULT_OK ) {
// Toast does not show
Toast.makeText(getApplicationContext(), "Result Recieved", Toast.LENGTH_SHORT).show();
}
}
I launch child activity from parent activity like this:
Intent myColorPickerIntent = new Intent(WidgetConfig.this, myColorPicker.class);
myColorPickerIntent.putExtra("appwidget_notecolor", appwidget_notecolor);
WidgetConfig.this.startActivity(myColorPickerIntent);
Of course you won't get the result, you're calling startActivity() instead of startActivityForResult().
You don't seem to be calling startActivityForResult() after creating the Intent.
Are you pass the intent to startActivityForResult() method?

Refresh text EditText after finish()

I created a 'New File' activity using
startActivityForResult(new Intent(MainActivity.this, NewFile.class),1);
The NewFile activity lets users set certain options for their text file then after clicking a button a string is saved to a static variable in my StringBuilder class and finish(); is called. How can I load this new string into the MainActivity's EditText? onCreate() is only called when the activity is first created right?
Do it on onResume or onActivityResult. It would be ideal though onActivityResult since you've used startActivityForResult, before finishing the other activity you set the setResult(int resultCode, Intent data) if you have intent to sent back or if none setResult(int resultCode). I think it is better to put the string which will be used to update your EditText as extra in the intent, then set the text using that string in onActivityResult method.
#Override
protected void onResume() {
super.onResume();
et.setText(DocumentBuilder.docText);
}
in your class NewFile.java :
String strName = "toto";
Intent intent = new Intent();
intent.putExtra("name", "toto");
setResult(1, intent);
finish();
in your MainActivity.java :
#Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == 1) {
if (resultCode == RESULT_OK) {
// Handle successful result
String name = intent.getStringExtra("name");
editText.setText(name);
}
}
}
refer this tutorial for more explanations

Categories