Passing information between Framents on Android

Asked

Viewed 37 times

0

Hello, Please, I need help. I’m trying to use the Android Studio template to make a simple application that in a Fragment (Pegavaloresfragment) in the onCreateView method takes an option of a Spinner that will be used in the other Fragment (Segundafragment).

For communication between the two Fragment I have a Communicator interface and a Mainactivity that implements Communicator.

My problem is the onCreateView method of Pegavaloresfragment. After Spinner’s choice I play the value for a static attribute of the Mainactivity class. This static attribute is returned to Segundafragment via a getCurso() method (this method is in Interface and implemented in Mainactivity).

The problem is that the value received from Spinner that was placed in the static attribute of the Mainactivity class is lost soon after the end of the onCreateView (Return rootView;) method of Pegavaloresfragment.

The Mainactivity class

public class MainActivity extends AppCompatActivity implements Comunicador {
public static String vCurso;
public String getCurso() {
    return(MainActivity.vCurso);
}
...

The Interface

public interface Comunicador{
    String getCurso();
}

The class Pegavaloresfragment

public class PegaValoresFragment extends Fragment {
...
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    opcoesCurso = (Spinner) rootView.findViewById(R.id.spinCurso);
    ArrayAdapter adapterCurso = ArrayAdapter.createFromResource(getActivity(),R.array.opcoes_cursos,android.R.layout.simple_spinner_item);
    opcoesCurso.setAdapter(adapterCurso);

    AdapterView.OnItemSelectedListener escolhaCurso = new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
            String itemCurso = opcoesCurso.getSelectedItem().toString();

            // PegaValoresFragment.setCurso(itemCurso);

            //Toast.makeText(getApplicationContext(),"Curso Escolhido foi : "+itemCurso, Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onNothingSelected(AdapterView<?> adapterView) {

        }
    };

    MainActivity.vCurso= opcoesCurso.getSelectedItem().toString();
    ....
    return rootView;
}
...

}

The class Segundafragment

public class SegundaFragment extends Fragment {

Comunicador comm;

public String getCurso(){
    return(comm.getCurso());
}

...

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.segunda_fragment, container, false);
    ...
    String teste = getCurso();

    textView.setText(" Curso getCurso() => "+teste);

    ...
    return rootView;
}

}

Please, could you help me figure out where I’m going wrong? Thank you

  • One of the ways to solve this would be by creating a class to store this value and you would have access anywhere in your app.

1 answer

0

Possible solution.

Class that will receive the requested values.

   import android.os.Bundle;
   import java.util.ArrayList;
   import java.util.List;

   public class GlobalProperties {

   private static GlobalProperties instance = new GlobalProperties();

   public GlobalProperties() {
   }

  public static GlobalProperties getInstance() {
       return instance;
  }

  private String curso;

  public String getCurso() {
     return curso
  }

  public void setCurso(String _curso) {
     this.curso = _curso;
  }
}   

Class that receives user value and stores in the above class.

  public class PegaValoresFragment extends Fragment {


  GlobalProperties gp;

  public View onCreateView(LayoutInflater inflater, ViewGroup container,
                     Bundle savedInstanceState) {
  opcoesCurso = (Spinner) rootView.findViewById(R.id.spinCurso);
  ArrayAdapter adapterCurso =       ArrayAdapter.createFromResource(getActivity(),R.array.opcoes_cursos,android.R.layout.simple_spinner_item);
opcoesCurso.setAdapter(adapterCurso);

AdapterView.OnItemSelectedListener escolhaCurso = new AdapterView.OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
        String itemCurso = opcoesCurso.getSelectedItem().toString();

        // PegaValoresFragment.setCurso(itemCurso);

        //Toast.makeText(getApplicationContext(),"Curso Escolhido foi : "+itemCurso, Toast.LENGTH_SHORT).show();


    }

    @Override
    public void onNothingSelected(AdapterView<?> adapterView) {

    }
};
gp = GlobalProperties.getInstance();
gp.setCurso( opcoesCurso.getSelectedItem().toString() );

....
return rootView;

} ...

Class that receives the value passed

public class Segundafragment extends Fragment {

GlobalProperties gp;

...

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                     Bundle savedInstanceState) {
 View rootView = inflater.inflate(R.layout.segunda_fragment, container, false);
...
gp = GlobalProperties.getInstance();

String teste = gp.getCurso();

textView.setText(" Curso getCurso() => "+teste);

...
return rootView; 
}
  • Hello Reginaldo. Thank you very much for your help, but you are returning NULL. ("Course getCurso() => null"). I tried to pass the event code onCreateView to the event onActivityCreated of Pegavaloresfragment, but continues returning NULL.

  • Change line: gp.setCurso( opcoesCurso.getSelectedItem(). toString() ); for : gp.setCurso( "Testing" );

  • Thank you very much. With this change came the Testing.

  • Which leads us to believe that setCurso is not receiving a proper string. It seems that optionsCurso.getSelectedItem(). toString() is not a string.

  • Hi Reginaldo. Thanks for your help. I did the test by assigning a value to a vCurso string = "Test" and doing gp.setCurso(vCurso); Debugging shows that when leaving the onCreateView method all values are lost.

  • Also testing I found that I can keep the data in the onCreateView method but in this method I still do not have access to Spinner, I will only have access to it in the next Fragment life cycle method.

  • I tried to pass the value with static attributes to onCreateView, without success. In my readings I understood that the method that could store the information would be onSaveInstanceState but it does not go through this method. Once again, thank you.

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.