-1

I have not found a clear solution anywhere on stack for this.

Here's my basic set up

public class Activity1 extends AppCompatActivity
{
    private OnAttributesUpdatedListener onAttributesUpdatedListener;

    public interface OnAttributesUpdatedListener
    {
        public void onAttributesUpdated();
    }

    public void setTargetFragment(Fragment fragment)
    {
        this.onAttributesUpdatedListener = (OnAttributesUpdatedListener) fragment;
    }

    private void whenFinishedSomethingCallback()
    {
        onAttributesUpdatedListener.onAttributesUpdated();
    }
}


public class Fragment1 extends Fragment implements Activity1.OnAttributesUpdatedListener
{
     button.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view) {
                if(rivalButtonClick == 0)
                {
                    Activity1 activity1 = new Activity1();
                    activity1.setTargetFragment(Fragment1.this);
                    startActivity(new Intent(getActivity(), activity1.getClass()));
                }
            }
        });
}

I get a null pointer exception and crashes on : onAttributesUpdatedListener.onAttributesUpdated(); because for some reason my listener never gets set properly. What's the proper way to do this?

TheQ
  • 1,949
  • 10
  • 38
  • 63

2 Answers2

2

You need to set the listener at start of the fragment onCreatView() or in onActivityCreated() only if the Desired Activity is a parent Activity of that particular fragment. Below is an example .

 public class Activity1 extends AppCompatActivity {
    private OnAttributesUpdatedListener onAttributesUpdatedListener;
    public interface OnAttributesUpdatedListener {
        public void onAttributesUpdated();
    }

    public void setListener(OnAttributesUpdatedListener onAttributesUpdatedListener) {
        this.onAttributesUpdatedListener = onAttributesUpdatedListener;
    }

    private void whenFinishedSomethingCallback() {
        if(onAttributesUpdatedListener!=null)
        onAttributesUpdatedListener.onAttributesUpdated();
    }
}


 public class Fragment1 extends Fragment implements Activity1.OnAttributesUpdatedListener
{
    @Override
    public void onAttributesUpdated() {
        // Do your stuff here
    }
    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        ((Activity1)getActivity()).setListener(this);
    }
}

Read about fragment Life cycle to make use of getActivity(). also remove the listener when fragment is destroyed .

Use LocalBroadcastManager for communicating between in case the Fragment exists in other Activity.

ADM
  • 20,406
  • 11
  • 52
  • 83
  • my app crashes when I override onActivityCreated. I was a bit unclear, Activity1 is not the parent of the Fragment, Activity1 is completely separate from the fragment – TheQ Feb 18 '18 at 06:05
  • notice in my code, i use an intent to start Activity1 – TheQ Feb 18 '18 at 06:06
  • Thats what i mentioned in first place . You can only access the parent activity from fragment . So accept it your question is misleading . Anyway you can use `LocalBroadcast` for this purpose . – ADM Feb 18 '18 at 06:08
  • You doing great, I will trying keep up to your example. – newbie Oct 06 '21 at 15:18
0

At first create an Interface like this:

public interface Listener{ void doSomething() }

Then implement this interface in your activity.

And also add

Listener listener

In your fragment And in onAttach method in fragment use this

listener=(Listener)activity

Then call listener whenever you need .

f4rz4m1995
  • 41
  • 7