2

I have a callback interface.

public interface RowCallback {

    void callOnRowAction(String action);
}

My fragment FragmentA is attached to ActivityA in which FragmentA is defined as follows:

class FragmentA extends Fragment implements RowCallback {
  @Override
  void callOnRowAction(String action) {
        . . . Custom implementation . . . 
  }

  ... Other methods . . . 
  @Override
  public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_crime_list, container, false);
     mButton = view.findViewById(R.id.my_button);
     mButton.setOnClickListener((view) -> {
        // I want to pass the callback instance (which is FragmentA) to activity B.
        Intent intent = new Intent(getActivity(), ActivityB.class);
        . . .  Some more intent initialisation. . . 
        startActivity(intent);
     });
      return view;
  }


}

ActivityB source Code

public class ActivityB extends AppCompatActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fragment);

        FragmentManager fm = getSupportFragmentManager();
        Fragment fragment = fm.findFragmentById(R.id.fragment_container);
        if (fragment == null) {
            fragment = createFragment(getIntent());
            fm.beginTransaction().add(R.id.fragment_container, fragment).commit();
        }
    }

   private Fragment createFragment(Intent intent) {
      FragmentB fragment = new FragmentB();
      Bundle bundle = new Bundle();
      . . . . initialise bundle . . . 
      // I want to pass the callback instance from the intent to the callback function.
     // In this example, the callback instance would be an instance of fragment A.
      fragment.setArguments(bundle);
     return fragment;
   }
}

In an instance of FragmentB, I want to invoke a call back method should a logical condition be satisfied.I want to return to ActivityA/FragmentA after the callback function has been invoked by calling getActivity().finish();

How can I achieve something like this?

manoflogan
  • 31
  • 8

3 Answers3

3

Implement function in within fragment which you want get call back after execute

    public void testCallBack(Callback callback) {

    callback.callOnRowAction("callback");

}

Implement this in Activity class

   public void getCallBack() {

    FragmentA.newInstance().testCallBack(new FragmentA.Callback() {
        @Override
        public void callOnRowAction(String action) {
            //you can do something
        }
    });
}

if you want to excute testCallBack method when the function start you can call this inside onCreate method

madu_dev
  • 87
  • 5
  • How do I load ActivityB from a click event of a button that has been initliased in FragmentA of ActivityA? I want to go back ActivityA after the callback has been invoked. So I will be calling getActivity().finish() after the callback function has been called in FragmentB's method. – manoflogan Nov 24 '18 at 12:27
2

I will split my answer:

  1. If you want a callback for one time:

    • Fragment A OnclickListener will call to his Activity (lets say A).
    • Activity A will do StartActivityForResult(intent) to Activity B.
    • Activity B will attach Fragment B to himself.
    • Fragment B OnclickListener will update his Activity (B).
    • Activity B will return IntentResult and Activity A will catch it on onActivityResult.
  2. If you want a callback for multiple times:

motis10
  • 2,484
  • 1
  • 22
  • 46
  • Actually FragmentA's (which is created by ActivityA) button's on click listener will call Activity B from within the fragment. All the logic is encapsulated within the fragment. Activity does not do anything other than instantiating the fragment. I want the callback to be invoked every time a logical condition inside FragmentB is satisfied. For example, if I had a reference to the callback instance in Fragment B, I would simply call `callbackInstance.callOnRowAction("delete");` within FragmentB's methods. – manoflogan Nov 24 '18 at 11:59
  • I also want to return back to ActivityA after the task is done. – manoflogan Nov 24 '18 at 12:21
0

You could inside the fragment add

if(getParentFragment() instanceof Activity1)
((Activity1) getParentFragment()).setRowCallback(this);
else if(getParentFragment() instanceof Activity2)
((Activity2) getParentFragment()).setRowCallback(this);

the fragment will automatically call the callback of his parent activity

Master Fathi
  • 349
  • 1
  • 9
  • 19