Skip to content Skip to sidebar Skip to footer

Call Activity's Methods From Fragment

INTRODUCTION I have a bluetooth app that consists on an Activity which has 3 buttons that do: Set visibility on and create a server connection Find devices and create a client con

Solution 1:

What you want is a callback interface for the communication from a Fragment to an Activity. The docs already provide a good example for this.

Define an Interface which your Activity implements:

publicinterfaceCallback {
    voiddoSomething();
}
publicclassYourActivityimplementsCallback {
    ...
    @OverridepublicvoiddoSomething() {
         // your implementation here
    }
}

In your Fragment use this interface if one of the Buttons are clicked.

publicclassServerFragmentextendsFragment { 

    Callback iCallback;
    ...
    @OverridepublicvoidonAttach(Activity activity) {
        super.onAttach(activity);
        try {
            iCallback = (Callback) activity;
        } catch (ClassCastException e) {
            thrownewClassCastException();
        }
    }

    @OverridepublicViewonCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.server_layout, container, false);

        btnServer = (Button) view.findViewById(R.id.buttonServer);   
        btnServer.setOnClickListener(newOnClickListener() {
            @OverridepublicvoidonClick(View v) {
                iCallback.doSomething();
            }
        });
    }
}

With this approach you can leave your logic in the Activity and handle the UI events in the Fragment. Through the events in the Fragment you can invoke the methods in your Activity.

Solution 2:

Making everything static is the wrong way to go on this. Do this:

  1. Define a new interface with the methods you need to call from your fragments
  2. Make your activity implement the interface
  3. In your fragments, cast getActivity() to your interface and call the methods. (Remember to check beforehand, i.e. if (getActivity() instanceof MyInterface) ...

Solution 3:

as Chris said, you have tu let your activity implement an interface, let's say ClickInterface:

publicclassBluetoothActivityextendsActivityimplementsClickInterface{
//...@overridepublicvoidonClickServer() {
    IntentdiscoverableIntent=newIntent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
    discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 120);
    startActivityForResult(discoverableIntent, REQUEST_DISCOVERABLE);
}

the interface would be like this:

publicinterfaceClickInterface {
    publicvoidonClickServer();
}

and this is your fragment:

publicclassServerFragmentextendsFragment { 
    privateClickInterface mCallback;

    @OverridepublicvoidonAttach(Activity activity) {
        super.onAttach(activity);
        mCallback = (ClickInterface) activity;
    }  
    @OverridepublicViewonCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.server_layout, container, false);

    btnServer = (Button) view.findViewById(R.id.buttonServer);   
    btnServer.setOnClickListener(newOnClickListener() {
        @OverridepublicvoidonClick(View v) {
            mCallback.onClickServer();
        }
    });
}

So, you're calling "onClickServer()" method on mCallback object, and this method would be called in the activity because it implements the interface

Solution 4:

Use interface to call method from Activity from any fragment page, for example:

publicinterfaceOnServerClickListener {
    publicvoidonUserClickServer();
}

And in Activity:

publicclassBluetoothActivityextendsActivityimplementsOnServerClickListener {
//...publicvoidonUserClickServer() {
  onClickServer();
}
}

And now in fragment you can use this:

publicclassServerFragmentextendsFragment { 
//...  @Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    Viewview= inflater.inflate(R.layout.server_layout, container, false);

    btnServer = (Button) view.findViewById(R.id.buttonServer);   
    btnServer.setOnClickListener(newOnClickListener() {
        @OverridepublicvoidonClick(View v) {
             try{
            ((OnServerClickListener)getActivity()).onUserClickServer();
             }
             catch(Exception e){Log.e("ServerFragment","Main Activity doesnt implement OnServerClickListener");
        }
    });

ENJOY!

Post a Comment for "Call Activity's Methods From Fragment"