2
public class ActivityA extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_one);
    }
}

activity_one.xml

<fragment
    android:id="@+id/fragment"
    class="com.emoontech.waternow.FragmentA"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

FragmentA

public class FragmentA extends Fragment{
    public static FragmentEditEvent newInstance(String param1, String param2) {
        FragmentEditEvent fragment = new FragmentEditEvent();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }
}

//From Another Activity on button click

Intent intent = new Intent(this, ActivityA.class);
intent.putString("param1","val1");
intent.putString("param2","val2");
startActivity(intent);

//How to send these two values to FragmentA?

touchchandra
  • 1,396
  • 3
  • 20
  • 34
  • Check this [Answer](http://stackoverflow.com/a/16036693/4636437) question is similar – GIRIDHARAN Sep 23 '15 at 18:49
  • possible duplicate of [How to pass values between Fragments](http://stackoverflow.com/questions/16036572/how-to-pass-values-between-fragments) – ChesuCR Sep 23 '15 at 18:56
  • I already looked into it. Do you say that, i cant use in activity_main.xml? do i have to create the fragment instance inside the onCreate() of activity and pass arguments? – touchchandra Sep 23 '15 at 19:04

3 Answers3

3

answer i made there https://stackoverflow.com/a/32748751/3301009 to share data between activities is very general and can also work for fragments

Community
  • 1
  • 1
Jerome B.
  • 234
  • 2
  • 8
2

try this

        // From ActivityA
        // retrieve the content of the intent from the previous activity
        protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fragment); // set content view to layout

        String param1Text= getIntent().getStringExtra("param1");
        String param2Text = getIntent().getStringExtra("param2");

        // this will pass the parameter to the fragment
        Fragment frament = FragmentA.newInstance(param1Text, param2Text);
        // use the fragment for what you desire

    }
Muyide Ibukun
  • 1,104
  • 13
  • 20
1

Found that its not possible to pass argument to the faragment directly but alternative is to use fragment manager and do findFragmentById() in activity to get fragment and set the date as needed.

Edit: If I declare a fragment in an XML layout, how do I pass it a Bundle?

touchchandra
  • 1,396
  • 3
  • 20
  • 34