-1

I'm trying to call the source fragment onActivityResult() from the target fragment when hardware back button pressed and struck with the below code when used inside the target fragment class. Getting NPE in the code mentioned below.

Please share some suggestion.

// target fragment
public void backButtonWasPressed() {
    Intent intent = new Intent();
    // NPE occurs in the below line 
    getTargetFragment().onActivityResult(getTargetRequestCode(), Activity.RESULT_OK, intent);
    getActivity().getSupportFragmentManager().popBackStack();
}

// main activity
@Override
public void onBackPressed() {       
    if (sourceFragment != null) {
            TargetFragment fragment = new TargetFragment();
            fragment.backButtonWasPressed();
    } else {
            super.onBackPressed();
            return;
    }        
}
Durga
  • 73
  • 12
  • 1
    Why this method is static? How getTargetFragment() will work if you call this method without creating a object. – Rahul Mar 29 '17 at 13:58
  • If the method backButtonWasPressed() is made non-static, I need to instatntiate the fragment class to call this method from main activity. And, when I do that I'm getting NPE inside the calling method. – Durga Mar 29 '17 at 14:29
  • I got it working. I changed my code to have a back button(imageview) in toolbar and when hardware back button is pressed i triggered the backbutton.performclick(). Added the buttonOnClickListener() and added the code in the buttonclicklistener block `Intent intent = new Intent(); getTargetFragment().onActivityResult(getTargetRequestCode(), Activity.RESULT_OK, intent); getActivity().getSupportFragmentManager().popBackStack();` – Durga Mar 30 '17 at 04:39

1 Answers1

0

getTargetFragment() is a non-static method of Fragment. See here. You need to get a reference to an instantiation of the fragment from which you are returning. The easiest way may be to pass the fragment as an argument, i.e.,

public static void backButtonWasPressed(Fragment frag)

then call

frag.getTargetFragment().onActivityResult(getTargetRequestCode(), Activity.RESULT_OK, intent);
Cheticamp
  • 61,413
  • 10
  • 78
  • 131
  • I am getting this error. java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v4.app.Fragment.onActivityResult(int, int, android.content.Intent)' on a null object reference – Durga Mar 29 '17 at 14:42
  • `getTargetFragment()` is probably returning null. This is a different, although related, issue. See [What is a NullPointerException, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it). – Cheticamp Mar 29 '17 at 16:11
  • I got it working. I changed my code to have a back button(imageview) in toolbar and when hardware back button is pressed i triggered the backbutton.performclick(). Added the buttonOnClickListener() and added the code in the buttonclicklistener block `Intent intent = new Intent(); getTargetFragment().onActivityResult(getTargetRequestCode(), Activity.RESULT_OK, intent); getActivity().getSupportFragmentManager().popBackStack();` – Durga Mar 30 '17 at 04:38