Android Studio fragment - onButtonPressed method

When creating a new fragment in Android Studio, it generates the onButtonPressed (Uri) method, how should it be connected to a user interface event, say, a click on a button declared in xml? How is this method intended to be used?

// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
    if (mListener != null) {
        mListener.onFragmentInteraction(uri);
    }
}
+5
source share
3 answers

Fragments are attached to activity onFragmentInteraction- this is the callback method that your activity uses to interact with the fragment

For example, the following action implements an interface from your fragment

public static class YourActivity extends Activity
        implements YourFragment.onFragmentInteraction{
    ...

    public void onFragmentInteraction(Uri uri) {
        // Do something with uri
    }
}

But since it TODOoffers

//TODO: , hook

, . :

mYourButton.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View arg0) {
        if (mListener != null) {
            mListener.onFragmentInteraction(Uri.parse("http://www.google.com"));
        }
    }
});
+2

, button:

button.setOnClickLisenter(new View.OnClickListener(){
    public void onClick(View view){
        // process and construct uri
        Uri uri = null;
        onButtonPressed(uri);
    }
};

onButtonPressed.

0

, , . , , , . , FRAGMENT ACTIVITY.

; INTERFACE, ACTIVITY, FRAGMENT , , LAYOUT.

: Kotlin, , Java, .

CALLBACK .

 interface ConcessionSelection {
         fun selectConcession(view : View)
 }

: , class ACTIVITY: AppCompatActivity(), INTERFACE class ACTIVITY: AppCompatActivity(), FRAGMENT.INTERFACE

CALLBACK.

 class ACTIVITY : AppCompatActivity(), INTERFACE {
     ...
     override fun CALLBACK  (view: View) {
         Log.i("ACTIVITY:CALLBACK","Activity Callback")
     }
     ...
 }

.

class FRAGMENT : Fragment() {  

    private var listener: ACTIVITY? = null
    ...
    override fun onAttach(context: Context) {
        super.onAttach(context)
        if (context is ACTIVITY) {
            listener = context
        } else {
            throw RuntimeException(context.toString() + " must implement OnFragmentInteractionListener")
        }
    }
    override fun onDetach() {
        super.onDetach()
        listener = null
    }
    ...
}

CALLBACK

<...>
...
<Button
        android:id="@+id/button"
        android:text="Button"
        android:onClick="CALLBACK"/> # Usually highlighted in red by IDE since this is resolved at runtime.
...
</...>

: , , , .

, , CALLBACK FRAGMENT, ACTIVITY . FRAGMENT onClickListener listener, ACTIVITY, . Android IDE, , , CALLBACK , ACTIVITY INTERFACE, XML. CALLBACK , , , , ListFragment onItemSelected.

The example onButtonPressedprovided by the template looks simple and does not provide a very useful implementation. Perhaps the Android site has a guide that explains how to intercept events and redirect to this method.

0
source

Source: https://habr.com/ru/post/1610609/


All Articles