Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

onActivityResult RESULT_OK can not be resolved to a variable in android?

I am trying to launch camera in fragment but onActivityResult in fragment doesn't resolve RESULT_OK. What should i do?

I am launching camera using:

public static final int CAMERA_REQUEST_CODE = 1999;  Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); startActivityForResult(cameraIntent, CAMERA_REQUEST_CODE); 

get captured image using:

@Override public void onActivityResult(int requestCode, int resultCode, Intent data) {     super.onActivityResult(requestCode, resultCode, data);     if (requestCode == CAMERA_REQUEST_CODE && resultCode == RESULT_OK) {         Bitmap bitmap = (Bitmap) data.getExtras().get("data");         if (bitmap != null) {         }     } } 

and i want captured image in current fragment!

like image 876
Tulsiram Rathod Avatar asked Apr 15 '14 13:04

Tulsiram Rathod


People also ask

How does onActivityResult work on Android?

The android startActivityForResult method, requires a result from the second activity (activity to be invoked). In such case, we need to override the onActivityResult method that is invoked automatically when second activity returns result.

Can startActivityForResult still be used?

It has deprecated startActivityForResult in favour of registerForActivityResult . It was one of the first fundamentals that any Android developer has learned, and the backbone of Android's way of communicating between two components.

What can I use instead of Startactivity for results?

We use startActivityForResult() to send and receive data between activities, in almost of our android projects. But recently startActivityForResult() method is deprecated in AndroidX. Android came up with ActivityResultCallback (also called Activity Results API) as an alternative for it.

How do you manage startActivityForResult?

First you use startActivityForResult() with parameters in the first Activity and if you want to send data from the second Activity to first Activity then pass the value using Intent with the setResult() method and get that data inside the onActivityResult() method in the first Activity .


2 Answers

RESULT_OK is constant of Activity class. In Activity class you can access directly but in other classes you need to write class name (Activity) also.

Use Activity.RESULT_OK instead of RESULT_OK.


In your case it will be

if (requestCode == CAMERA_REQUEST_CODE && resultCode == Activity.RESULT_OK) { 
like image 63
Pankaj Kumar Avatar answered Sep 25 '22 19:09

Pankaj Kumar


In fragment we must use getActivity() method as prefix with RESULT_OK.

In your case it will be:-

if (requestCode == CAMERA_REQUEST_CODE && resultCode == getActivity().RESULT_OK) 
like image 30
Ekta Bhawsar Avatar answered Sep 21 '22 19:09

Ekta Bhawsar