onactivityresult
Error: onActivityResult overrides nothing
Replace override fun onActivityResult(requestCode:Int, resultCode:Int, data:Intent) With below code, to make Intent object nullable. override fun onActivityResult(requestCode:Int, resultCode:Int, data:Intent?) As Intent is declared nullable in parent Activity class. Here is the sample code: protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data)
onActivityResult() not called
Option 1 : If you’re calling startActivityForResult() from the Fragment then you should call startActivityForResult() and not getActivity().startActivityForResult(), as it will result in Fragment‘s onActivityResult(). If you’re not sure where you’re calling on startActivityForResult() and how you will be calling methods. Option 2: Since Activity gets the result of onActivityResult(), you will need to override … Read more
OnActivityResult sometimes not called after ACTION_GET_CONTENT intent
You must add following code for taking images from devices choosebtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent i1=new Intent(); i1.setType(“image/*”); i1.setAction(Intent.ACTION_GET_CONTENT); startActivityForResult(i1,1); } }); @Override protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { super.onActivityResult(requestCode, resultCode, data); if(requestCode == 1 && resultCode == RESULT_OK && data != null && data.getData() != … Read more
How to replace startActivityForResult with Activity Result APIs?
You can call as many Activities for result as you wish and have separate callback for each: val startForResult = registerForActivityResult( ActivityResultContracts.StartActivityForResult() ) { result: ActivityResult -> if (result.resultCode == Activity.RESULT_OK) { // you will get result here in result.data } } startForResult.launch(Intent(activity, CameraCaptureActivity::class.java)) You just need to specify Activity class – CameraCaptureActivity::class.java Update: The … Read more
Android: how to make an activity return results to the activity which calls it?
In order to start an activity which should return result to the calling activity, you should do something like below. You should pass the requestcode as shown below in order to identify that you got the result from the activity you started. startActivityForResult(new Intent(“YourFullyQualifiedClassName”),requestCode); In the activity you can make use of setData() to return … Read more
Wrong requestCode in onActivityResult
You are calling startActivityForResult() from your Fragment. When you do this, the requestCode is changed by the Activity that owns the Fragment. If you want to get the correct resultCode in your activity try this: Change: startActivityForResult(intent, 1); To: getActivity().startActivityForResult(intent, 1);
OnActivityResult method is deprecated, what is the alternative?
A basic training is available at developer.android.com. Here is an example on how to convert the existing code with the new one: The old way: public void openSomeActivityForResult() { Intent intent = new Intent(this, SomeActivity.class); startActivityForResult(intent, 123); } @Override protected void onActivityResult (int requestCode, int resultCode, Intent data) { if (resultCode == Activity.RESULT_OK && requestCode … Read more