简体   繁体   中英

How to send data from main activity to fragment

public class MyActivity extends Activity implements ButtonFragement.OnFragmentInteractionListener, TextFragment.OnFragmentInteractionListener,Communicator {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my);
        ButtonFragement btnfrg=new ButtonFragement();
        TextFragment txtfrg= new TextFragment();
        FragmentManager fm=getFragmentManager();
        FragmentTransaction ft=fm.beginTransaction();
        ft.add(R.id.my_activity,btnfrg,"Fragment");
        ft.add(R.id.my_activity,txtfrg,"Second Fragment");
        ft.commit();
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.my, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onFragmentInteraction(Uri uri) {

    }

    @Override
    public void respond(String data) {
        FragmentManager fm=getFragmentManager();
        TextFragment f1= (TextFragment) fm.findFragmentById(R.id.textfrg);
        f1.changeText(data);

    }
}

This is my main_Activity code, here i am trying to send a data over the fragment but it gives me error at f1.changeText(data).Basic structure of my project is , on main Activity , i created two fragment. One with button and another with text. I want to show how many times the button was clicked on second fragment using a communicator interface. Here in "data" counter shows a how many times button was clicked but i am not able to transfer it over second fragment.


Complete Code for the Program---

public interface Communicator {
    public void respond(String data);
}

In TextFragment class i added this method----

  public void changeText(String data)
    {
        txt.setText(data);
    }

In ButtonFragment class i added and modified following method

public class ButtonFragement extends Fragment implements View.OnClickListener{
    int counter=0;
    private OnFragmentInteractionListener mListener;
    Button btn;
    Communicator comm;

    @Override
    public void onActivityCreated(Bundle savedInstanceState)
    {
        super.onActivityCreated(savedInstanceState);
        comm= (Communicator) getActivity();
        btn= (Button) getActivity().findViewById(R.id.button);
        btn.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        counter++;
       // comm.respond("The button was clicked "+counter+" times");
        comm.respond("hi");
    }

Here, i just added which i added in my program. My program get crash at... MainActiviy f1.changeText(data); But why i am not getting it.Can anyone Help me fixed this bug?

    Bundle bundle = new Bundle();
    bundle.putString("key", value);

    // set Fragmentclass Arguments

    YourFragment ff= new YourFragment ();

    ff.setArguments(bundle);

    transaction.add(R.id.my_activity, ff);

Using Bundle

From Activity:

Bundle bundle = new Bundle();
bundle.putString("message", "Hello!");
FragmentClass fragInfo = new FragmentClass();
fragInfo.setArguments(bundle);
transaction.replace(R.id.fragment_single, fragInfo);
transaction.commit();

Fragment:

Reading the value in the fragment

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
{
    String myValue = this.getArguments().getString("message");
    ...
    ...
}

You have no fragment with id R.id.textfrg . You are for some reason adding two fragments with id R.id.my_activity . One with tag "Fragment" and another with tag "Second Fragment" . So you are getting error. Your idea is rigth. This may be helpfull

TextFragment f1= (TextFragment) fm.findFragmentByTag("Second Fragment");

DO this way:

  1. On Activity Side.

      Fragment fragment = new GridTemplate(); Bundle bundle = new Bundle(); bundle.putInt("index",your value); fragment.setArguments(bundle); FragmentManager fragmentManager = getFragmentManager(); fragmentManager.beginTransaction() .replace(R.id.frame_container, fragment).commit(); 

here R.id.frame_container is a frame Layout or Relative Layout In which you have to add the fragment.

  1. On Fragment Side.

      int index = getArguments().getInt("index"); 

hope this wil solve your problem. Thanks

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM