Call Activity's methods from fragment

masmic

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 connection
  • Send something

I have the following classes:

  • Main activity: where I have all the methods, and the button listeners
  • 3 different classes with the Threads: ClientConnection, ServerConnection and ConnectedThread

Now I have to change the desing a little bit and instead of having one activity with all this, using Tabs on the ActionBar I need to create 2 fragments: Server and client.

So, basically, I have to define the Server and send buttons in the Server fragment, and the client button in the Client fragment, and set their listeners.

QUESTION

From each fragment, when using one button, I should call its corresponding method ubicated in the main Activity. I would like to do it this way, instead of copying all the methods and interfaces in each fragment, just to have it simplier.

I tried setting the methods as static, but there are some definitions inside the methods that can't be called with static references. Example:

public class ServerFragment extends Fragment { 
    //...  
    @Override
    public View onCreateView(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(new OnClickListener() {
            @Override
            public void onClick(View v) {
                onClickServer();
            }
        });
        //...

_

public class BluetoothActivity extends Activity {
    //...
    public void onClickServer() {
        Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
        discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 120);
        startActivityForResult(discoverableIntent, REQUEST_DISCOVERABLE);
    }

If I set onClickServer() as static, I have to set REQUEST_DISCOVERABLE too as static, and the startActivityForResult() doesn't let me use a static variable there.

How could I do this?

UPDATE -- Throwing NullpointerException

I have created new class called ClickInterface as suggestions, and there i have defined the 2 interfaces, one for the server and the other for the client.

No, from the fragment, I'm trying to call the method in the onClick method, but is throwing an NPE just in the line where i do the call to the callback's method:

public class ServerFragment extends Fragment implements OnClickListener {

    public View onCreateView(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(this);

        return view;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.buttonServer:
                serverFragCallback.onServer(); //HERE I RECEIVE THE NPE
                break;
        }
    }

This is the logcat's output:

03-20 16:33:57.581: E/AndroidRuntime(24884): FATAL EXCEPTION: main
03-20 16:33:57.581: E/AndroidRuntime(24884): Process: com.uax.bluetoothconnection, PID: 24884
03-20 16:33:57.581: E/AndroidRuntime(24884): java.lang.NullPointerException
03-20 16:33:57.581: E/AndroidRuntime(24884):    at com.uax.bluetoothconnection.ServerFragment.onClick(ServerFragment.java:43)
03-20 16:33:57.581: E/AndroidRuntime(24884):    at android.view.View.performClick(View.java:4633)
03-20 16:33:57.581: E/AndroidRuntime(24884):    at android.view.View$PerformClick.run(View.java:19330)
03-20 16:33:57.581: E/AndroidRuntime(24884):    at android.os.Handler.handleCallback(Handler.java:733)
03-20 16:33:57.581: E/AndroidRuntime(24884):    at android.os.Handler.dispatchMessage(Handler.java:95)
03-20 16:33:57.581: E/AndroidRuntime(24884):    at android.os.Looper.loop(Looper.java:157)
03-20 16:33:57.581: E/AndroidRuntime(24884):    at android.app.ActivityThread.main(ActivityThread.java:5356)
03-20 16:33:57.581: E/AndroidRuntime(24884):    at java.lang.reflect.Method.invokeNative(Native Method)
03-20 16:33:57.581: E/AndroidRuntime(24884):    at java.lang.reflect.Method.invoke(Method.java:515)
03-20 16:33:57.581: E/AndroidRuntime(24884):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
03-20 16:33:57.581: E/AndroidRuntime(24884):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
03-20 16:33:57.581: E/AndroidRuntime(24884):    at dalvik.system.NativeStart.main(Native Method
Steve Benett

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:

public interface Callback {
    void doSomething();
}
public class YourActivity implements Callback {
    ...
    @Override
    public void doSomething() {
         // your implementation here
    }
}

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

public class ServerFragment extends Fragment { 

    Callback iCallback;
    ...
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            iCallback = (Callback) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException();
        }
    }

    @Override
    public View onCreateView(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(new OnClickListener() {
            @Override
            public void onClick(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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Call an activity method from a fragment

From Dev

Call activity from fragment in android

From Dev

Accessing fragment methods from parent Activity

From Dev

Call method in fragment from another fragment/activity

From Dev

Call Fragment method from Activity

From Dev

How to call method inside the fragment from activity?

From Dev

Call fragment method from activity but view is null

From Dev

Call AsyncTask in the parent activity from fragment

From Dev

Call dynamic fragment method from activity

From Dev

Call Tabbed Fragment method from Activity

From Dev

Call method in child fragment in parent fragment from activity

From Dev

Using Activity methods in Fragment

From Dev

Call activity from fragment in android

From Dev

Call method in fragment from another fragment/activity

From Dev

Call a method in a drawer fragment from the base activity

From Dev

Call methods of sub-activity from base activity

From Dev

Call Fragment method from Activity

From Dev

Call fragment method from activity but view is null

From Dev

how to call a fragment from activity

From Dev

Call AsyncTask in the parent activity from fragment

From Dev

How to call a fragment from another activity

From Dev

Call service methods from activity without binding

From Dev

How to call an activity's methods from a separate class

From Dev

How to call Activity methods from inside a Fragment

From Dev

Call method in child fragment in parent fragment from activity

From Dev

Using Activity methods in Fragment

From Dev

How call method from Fragment in Activity?

From Dev

How to call ViewHolder method from Activity/Fragment

From Dev

Call method from Main Activity into Fragment Adapter

Related Related

HotTag

Archive