Passing values between Fragment and Activity

3

I have a fragment that consists of several buttons. The goal is that when I press one of these buttons the fragment disappears and passes the value associated with that button to the activity that is in the background. What is happening is that when I click the button, the fragment does not disappear and the activity does not receive the value of the button. I'm using Callback . I do not have any errors or exceptions.

Fragment Code:

(...)
 public HoroscopeChoice() {}
    /******************************
     * Callback
     ********/
    public static void setOnInfoChangedListener(OnInfoChangedListener callback) {
        mCallback = callback;
    }

    public interface OnInfoChangedListener {
        public void onInfoChanged(String horosocopo);
     }
public View onCreateView(final LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_horoscope_choice,
                container, false);

        Button aquarius;
        aquarius  = (Button) view.findViewById(R.id.aquarius1);

        final int id = view.getId();

        View.OnClickListener onClickListener = new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                String horoscopo = onClick2(v.getId());
                Log.d("HoroscopeChoice", "ao clicar no botao->"+horoscopo);
                mCallback.onInfoChanged(horoscopo);
            }
        };

        aquarius.setOnClickListener(onClickListener);
(...)

Activity Code:

(...)
public void onInfoChanged(String horoscopo) {
        Log.d("SchedulerActivity","OnInfoChanged na Scheduler->"+horoscope);

        mHoroscopeDisplay = (TextView) findViewById(R.id.dailyHoroscope4);
        mHoroscopeDisplay.setText(horoscopo);
    }

When I do Log.d in the fragment I get the horoscope, already in the activity appears empty. What do I have to do?

    
asked by anonymous 18.06.2016 / 20:53

3 answers

0

The only thing missing from my fragment and what I discovered afterwards was the:

dismiss()

to disappear when the button is pressed.

    
21.06.2016 / 22:44
3

Hello, prothfind. To begin, the idea is that your Activity implements an interface that Fragment knows and calls. So let's create the interface that will be called when there was a click on the fragment:

public interface OnFragmentButtonClickListener  {
    public void onButtonClick();
}

Then let's make Activity inherit from this interface and implement the method:

public class MinhaActivity extends AppCompact implements OnFragmentButtonClickListener {

    //... Outros métodos comuns da Activity

    public void onButtonClick() {
       //O botão do fragment foi clicado, faça o que tem que fazer na activity!
    }

}

Right, in the onAttach() method of your fragment, which is called during the fragment lifecycle when a fragment is appended to Activity, capture the Activity using polymorphism for terms in the form of an OnFragmentButtonClickListener instance. This way:

public void FragmentTwo extends Fragment {

   OnFragmentButtonClickListener onFragmentButtonClickListener

   @Override
    public void onAttach(Context context) {
        if(context instanceof OnFragmentButtonClickListener) {
            onFragmentButtonClickListener = (OnFragmentButtonClickListener) context;
        }
        super.onAttach(context);
    }

    //Métodos comuns ao fragment
}

Now, in the onClickListener of your button call the interface that is implemented by Activity so that the Activity can match the click on the fragment:

fragmentButton.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
       //onFragmentButtonClickListener.onButtonClick(); Lembrando que onFragmentButtonClickListener pode ser null caso a activity não implemente a interface.
    } 
});

Why do I prefer this answer to that of João Gouveia? By abstraction. In João Gouveia's response you are trapping your Fragment to MyActivity and this can bring you some problems as well as breaking some good practices. It may be that your fragment is attached to several other Activities besides MyActivity. It may also happen that your fragment in the future will no longer be attached to MyActivity, which will generate a search for references for maintenance. Working with interfaces makes your code more decoupled and with better scalability. Abs!

    
21.06.2016 / 13:58
1

You can create a variable in your activity, in addition to a setter, and "set" its value from within the fragment:

public class MyActivity extends Activity {
    private String horoscopo;

    public void setHoroscopo(String horoscopo) {
        this.horoscopo = horoscopo;
    }
}    

and in the fragment you call:

((MyActivity) getActivity).setHoroscopo(horoscopo);
    
19.06.2016 / 00:14