Android komunikacja między fragmentami.

0

Witam,
mam problem mianowicie posiadam fragmenty :
klawiatury:

public class keyboard_fragment extends Fragment {
    Button equal;
    View rootView;
    public keyboard_fragment() {
    }




    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_keyboard, container, false);





        equal = (Button) rootView.findViewById(R.id.equalButton);
        equal.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
                //fragmentTransaction.setCustomAnimations(R.anim.top_in,0,0,R.anim.top_out);
                fragmentTransaction.replace(R.id.keyboard, new final_fragment());
                fragmentTransaction.addToBackStack(null);

                FragmentTransaction fragmentTransaction2 = getFragmentManager().beginTransaction();
                //fragmentTransaction2.setCustomAnimations(R.anim.top_in,0,0,R.anim.top_out);
                fragmentTransaction2.replace(R.id.digits, new buttonBack_fragment());
                fragmentTransaction2.addToBackStack(null);


                fragmentTransaction2.commit();
                fragmentTransaction.commit();


            }
        });

        return rootView;
    }


    public View getView(){
        return rootView;
    }
} 

i fragment z ekranem.

public class digitizer_fragment extends Fragment {
    View rootView;
    public digitizer_fragment() {
    }




    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_digitizer, container, false);



        return rootView;
    }

    public View getView(){
        return rootView;
    }
} 

w pierwszym są przyciski (aplikacja to coś w stylu kalkulatora), a w drugim wyświetlanie. Próbowałem już wszystkiego co mogę wykombinować, ostatnie nad czym się poddałem to ten stan:

public class CalculateIIt extends Activity {
    Button fk1,fk2,fk3,fk4,fk5,fk6,fk7,fk8,fk9,fk0,fkA,fkB,fkC,fkD,fkE,fkF;
    Fragment kibord, digiti;
    TextView convertFrom,convertTo;
    View rootViewKeyboardFragment,rootViewDigitizerFragment;
    boolean toOrFrom=false; // to[true] or from[false]

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);



        setContentView(R.layout.activity_calculate_iit);
        getActionBar().hide();
        digiti = new digitizer_fragment();
        kibord = new keyboard_fragment();

        rootViewKeyboardFragment = kibord.getView();
        rootViewDigitizerFragment = digiti.getView();
        //komentarz

        convertFrom = (TextView)rootViewDigitizerFragment.findViewById(R.id.convertFrom);
        convertTo = (TextView)rootViewDigitizerFragment.findViewById(R.id.convertTo);



        if (savedInstanceState == null) {
            getFragmentManager().beginTransaction()
                    .add(R.id.keyboard, kibord)
                    .add(R.id.digits, digiti)
                    .commit();
        }




        try {
            fk1 = (Button)rootViewKeyboardFragment.findViewById(R.id.fk1);
        }catch (NullPointerException e){
            Log.v("Error","Error while comunicating between views");
        }finally {
            fk1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    textviewFill("1");
                }
            });
        }


    }



    public void textviewFill (String buttonValue){
        if (!toOrFrom){
            if(convertFrom.getText().length()==0){
                convertFrom.setText(buttonValue);
            }else convertFrom.append(buttonValue);
        }else{
            if(convertTo.getText().length()==0){
                convertTo.setText(buttonValue);
            }else convertTo.append(buttonValue);
        }
    }


}

Próbowałem pobrać View z fragmentu ponieważ dostawałem null exception, a wykonując wewnątrz fragmentu działa interakcja z przyciskami (za to wyswietlacz nie), więc pomyśałem że zadziała :(.

1

Wysyłanie sygnałów za pomocą event bus: https://github.com/square/otto

Szybki sposób:

keyboard_fragment keyboard_fragment= (keyboard_fragment) getSupportFragmentManager().findFragmentById(R.id.keyboard);
digitizer_fragment digitizer_fragment = (digitizer_fragment) getSupportFragmentManager().findFragmentById(R.id.digits);

// wywołujesz ten kod w aktywności, w której są "zakotwiczone" te fragmenty. Możesz na nich wywołać dowolną metodę
0

Wysyłka do drugiego fragmentu

Fragment fragment = new Fragment();
 Bundle bundle = new Bundle();
 bundle.putInt(key, value);
 fragment.setArguments(bundle);

Odbiór z drugim fragmencie

Bundle bundle = this.getArguments();
 int myInt = bundle.getInt(key, defaultValue);

1 użytkowników online, w tym zalogowanych: 0, gości: 1