简体   繁体   中英

how to pass string from activity to fragment?

Activity A

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_ActivityA);

    Bundle b = new Bundle();
    b.putString("mobile", "123456789");
    FragmentA fragmentA = new FragmentA ();
    fragmentA .setArguments(b);}

FragmentA

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

    View v = inflater.inflate(R.layout.fragment_fragmentA, container, false);

        String txtMobile = getArguments().getString("mobile");
        Log.i("mobile2",txtMobile);
        mobile.setText(txtMobile);}

I have tried above solution but showing error that Attempt to invoke virtual method 'java.lang.String android.os.Bundle.getString(java.lang.String)' on a null object reference

Create your fragment like this

public static MyFragment newInstance(String string) {

    Bundle args = new Bundle();
    args.putString("string",string);
    MyFragment fragment = new MyFragment();
    fragment.setArguments(args);
    return fragment;
}

And then in Fragment onCreate use getArguments() for pulling that String. Don't forget to check getArguments() for null.

Try to fetch the argument in onCreate method of fragment with default value constructor:

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    String txtMobile = getArguments().getString("mobile", "");
}

and make sure you commit the fragment transaction after settting the arguments in fragment. :)

First check that your argument is not null. For example @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

View v = inflater.inflate(R.layout.fragment_fragmentA, container, false);

if(getArguments()!=null){

    String txtMobile = getArguments().getString("mobile");
    Log.i("mobile2",txtMobile);
    mobile.setText(txtMobile);

} }

try this:

  @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        if (savedInstanceState != null){            
            String txtMobile = savedInstanceState.getString("mobile");                
        }
    }

May be It's about the way you are adding fragment to the class:

    Bundle b = new Bundle();
    b.putString("mobile", "123456789");
    FragmentA fragmentA = new FragmentA ();
    fragmentA .setArguments(b);

    fm.beginTransaction()
    .replace(placeholder, new FragmentA(), tabId)
    .commit();

can you provide more data about how did you add it ?

you can use inteface to pass data from activity to fragment

In your activity

public interface FragmentRefreshListener {
    void onRefresh(String mydata);
}

public FragmentRefreshListener fragmentRefreshListener;

public void setFragmentRefreshListener(FragmentRefreshListener fragmentRefreshListener) {
    this.fragmentRefreshListener = fragmentRefreshListener;
}

public FragmentRefreshListener getFragmentRefreshListener() {
    return fragmentRefreshListener;
}

Then inside your fragment add below code

((MainActivity) getActivity()).setFragmentRefreshListener(new MainActivity.FragmentRefreshListener() {

        @Override
        public void onRefresh(String mydata) {

      // write your logic here

        }
    });

use following in your activity to pass data from activity to fragment

getFragmentRefreshListener().onRefresh("dummy text");

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