OnActivityResult fragment method to perform onActivityResult call activity

In my snippet, I started the startActivityforresult goal to capture photos. I overriden the callbackActivityResult method in the fragment class. I applied the onActivityResult callback in the main activity for some other purposes. My problem is the onActivityResult snippet after execution invokes the onActivityResult actions and returns a null pointer exception. Fragment onactivityresment method

@Override public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (resultCode == Activity.RESULT_OK) { prof_bitmap = null; if (requestCode == 0) { Log.e("" ,"entered activity Result Code 0"); Uri photoUri = data.getData(); if (photoUri != null) { String[] filePathColumn = { MediaStore.Images.Media.DATA }; Cursor cursor = getActivity().getContentResolver().query( photoUri, filePathColumn, null, null, null); cursor.moveToFirst(); int columnIndex = cursor.getColumnIndex(filePathColumn[0]); String filePath = cursor.getString(columnIndex); cursor.close(); Log.e("" ,"File Path" +filePath); prof_bitmap = setImage(filePath); } } if (requestCode == 1) { Log.e("" ,"entered activity Result Code 1"); Bitmap bitmap = (Bitmap) data.getExtras().get("data"); prof_bitmap = bitmap; Log.e("" ,"entered activity Result Code 1"+bitmap); profile_pic.setImageBitmap(bitmap); } } } 

OnActivityResult action

  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); Log.e("" ,"called onActivityResult in main"); Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data); } 

How to call only the onactivityresult method for a fragment?

MY Logcat

ResultInfo result execution error {who = null, request = 1, result = -1, data = Intent {act = inline-data dat = content: // media / external / images / media / 222 (has additional functions)}} in action {com.mobiotics.tvbuddydemo / com.mobiotics.tvbuddydemo.TVBuddyMainActivity}: java.lang.NullPointerException

+6
source share
5 answers

If you have an onActivityResult defined in your activity, you cannot skip it and go directly to the fragment. However, you can redirect it to the Fragment if the Activity does not know how to handle it. Use unique query codes to distinguish between those who process the result.

 public void onActivityResult(int requestCode, int resultCode, Intent data) { boolean processed = true; if (resultCode == Activity.RESULT_OK) { if (requestCode == 0) { // Something } else if (requestCode == 1) { // Something } else { processed = false; } } else { // Error if (requestCode == 0) { // Handle error } else { processed = false; } } if (!processed) { fragment1.onActivityResult(requestCode, resultCode, data); fragment2.onActivityResult(requestCode, resultCode, data); ... super.onActivityResult(requestCode, resultCode, data); } } 

Note. Be sure to call getActivity (). startActivityForResult () from your fragment, not just using this.startActivityForResult ()

 public void something(Intent intent) { getActivity().startActivityForResult(intent); // or if you are using SherlockActionBar/Support package getSupportActivity().startActivityForResult(intent); } 

Hope this helps.

+9
source

Try to delete the line super.onActivityResult(requestCode, resultCode, data); in the onActivityResult() fragment.

+1
source

public void onActivityResult is called when the Activity you started with startActivityForResult ends with the finish() call

+1
source

The onActivityResult in the Activity will be called first, you should not delete the result processing in the Activity, but do what you want by checking the requestCode.

0
source

I am also facing this problem.

Invoking activity from a fragment

((Activity)). StartActivityForResult (photoPickerIntent, 100);

here is the "context" of the FragmentActivity. Now the result comes in the FargmentActivity onActivityResult method. to switch this result you must declare a static variable of the fragment class in the fragmetn activity class.

 static ActivityUploadPhotos activityUploadPhotos; ... ActivityUploadPhotos f = new ActivityUploadPhotos(); ActivityHome.activityUploadPhotos=f; ...... if(activityUploadPhotos!=null) { activityUploadPhotos.onActivityResult(requestCode, resultCode, data); } 
0
source

Source: https://habr.com/ru/post/944500/


All Articles