Skip to content Skip to sidebar Skip to footer

How To Pass String Data From Activity To Fragment?

I want to pass string value from activity to fragment. For that I'm using bundle for transferring string value. PUT STRING ACTIVITY Passing string value :- Bundle bundle

Solution 1:

I advice you to follow this link1 and this . You should use interface. Check here

Bundlebundle=newBundle();
bundle.putString("edttext", "From Activity");
// set Fragmentclass ArgumentsFragmentclassfragobj=newFragmentclass();
fragobj.setArguments(bundle);

and in the Frament task

@OverridepublicViewonCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    String strtext = getArguments().getString("edttext");    
    return inflater.inflate(R.layout.fragment, container, false);
}

Solution 2:

Suppose you want to send String data from Activity to Fragment

In Fragment.java file add the following code,

publicstaticString name= null;

publicvoidsetName(Stringstring){
name = string;
}

In MainActivity.java from which you want to send String add the following code,

String stringYouWantToSend;

Fragmentfragment=newFragment();
fragment.setName(stringYouWantToSend);   

Solution 3:

The savedInstance variable will be empty the first time you run your app, it only gets filled when the onSaveInstanceState method is called, this usually happens when you rotate the screen, then savedInstance variable will be populated. Try this :

if(savedInstance != null){
  Log.d(TAG, "There's is something");
}else{
  Log.d(TAG, "Nothing in there");
}

First time in logcat you will see the first message, try turning your device and the message in the else will show.

As @james said, the recommended way is to use interfaces, or have a field class but this increases coupling and destroys the whole point of fragments.

Solution 4:

make public first in activity that object you required in fragment. then you can use like ((ACtivityName)getActivity()).objectName; i hope it help.this is not best way to pass data into fragment.

there are some other way also. create constructor of your fragment class and pass it and save in fragment like:

public MyFragment(String data){
       this.data = data;
    }

also you can use as bundle @james answer.

EDIT:-

First Way

privateclassMyFeagmentextendsFragment{
    private String data;
    private Object myObject;

    public MyFeagment(String data, Object anyObject) {
        this.data = data;
        this.myObject = myObject;
    }

    @Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        Log.i("My Fragment", "data :: " + data + " and myObject :"
                + myObject);
        returnsuper.onCreateView(inflater, container, savedInstanceState);
    }
}

let's say it is your framgnet. and put this code in your activity

FragmentManagermanager= getSupportFragmentManager();
    FragmentTransactiontransaction= manager.beginTransaction();
    transaction.add(R.id.fragment_container, newMyFeagment("My Data",
            "data"));
    transaction.commit();

Solution 5:

Easiest Approach but not Recommended

You can access activity data from fragment:

Activity:

publicclassMyActivityextendsActivity {

    privateString myString = "hello";

    @OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my);
        ...
    }

    publicStringgetMyData() {
        return myString;
    }
}

Fragment:

publicclassMyFragmentextendsFragment {

    @Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        MyActivityactivity= (MyActivity) getActivity();
        StringmyDataFromActivity= activity.getMyData();
        return view;
    }
}

Post a Comment for "How To Pass String Data From Activity To Fragment?"