1

i tried to put and get extra from activity to fragment . but something is wrong! anybody have idea? my case is diffrent because i wanna do it in fragment

myActivity :

if(email.matches(users.user1)&&password.matches(users.pass1)){
        Intent intent = new Intent(LoginActivity.this,MainActivity.class);
        Intent i = new Intent(LoginActivity.this,ProfileFragment.class);
        i.putExtra("pn", users.pn1);
        i.putExtra("name", users.name1);
        i.putExtra("family", users.family1);
        i.putExtra("rank", users.rank1);
        startActivity(intent);
        finish();
    }

myfragment

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_profile2, container, false);

    final TextView pn =getActivity().findViewById(R.id.pn);
    final TextView name =getActivity().findViewById(R.id.name);
    final TextView family =getActivity().findViewById(R.id.family);
    final TextView user =getActivity().findViewById(R.id.user);
    final TextView rank =getActivity().findViewById(R.id.rank);

    String pnget = getActivity().getIntent().getStringExtra("pn");
    String nameget = getActivity().getIntent().getStringExtra("name");
    String familyget = getActivity().getIntent().getStringExtra("family");
    String userget = getActivity().getIntent().getStringExtra("user");
    String rankget = getActivity().getIntent().getStringExtra("rank");

    pn.setText(pnget);
    name.setText(nameget);
    family.setText(familyget);
    user.setText(userget);
    rank.setText(rankget);
}

Hi . i tried to put and get extra from activity to fragment . but something is wrong! anybody have idea?

  • Possible duplicate of [How do I get extra data from intent on Android?](https://stackoverflow.com/questions/4233873/how-do-i-get-extra-data-from-intent-on-android) – Matthew Vanlandingham Dec 08 '17 at 05:13
  • but my case is diffrent . i wanna put and get extra from activity to fragment! –  Dec 08 '17 at 06:50

3 Answers3

2

You start an intent call intent. But the intent have data is i, and it have't started yet.

You can use setArgument and getArgument to send and receive data from activity to fragment or from fragment to fragment:

In YourReceiveFragment:

public static Fragment newInstance(String data1, String data2, ...) {
    Fragment f = new YourReceiveFragment();
    Bundle bundle = new Bundle();
    bundle.putString(DATA_RECEIVE1, data1);
    bundle.putString(DATA_RECEIVE2, data2);
    f.setArguments(bundle);
    return f;
}

In your activity: Just call it:

Fragment f = YourReceiveFragment.newInstance(yourString1, yourString2);

                            FragmentTransaction ft = getFragmentManager().beginTransaction();
                            ft.add(R.id.main_container, f).commit();

Then in YourReceiveFragment:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (getArguments() != null){
        String dataReceive1 = getArguments().getString(DATA_RECEIVE1);
        String dataReceive2 = getArguments().getString(DATA_RECEIVE2);
    }
}

In case you want to send data from an activity to a fragment in another activity, use interface or an easier way is just pass the data to the activity which contains fragment and from that, send data to fragment.

Dzung Vu
  • 56
  • 1
  • 6
0

You can send data with the bundle, that is recommended

To Pass Data from Activity

fragment = new YourFragment();

if (fragment != null) {
    FragmentManager fragmentManager = getFragmentManager();

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

    fragment.setArguments(bundle);

    fragmentManager.beginTransaction().replace(R.id.container, fragment).commit();
}

To receive data from Fragment

  String var = getArguments().getString("value");
noman404
  • 928
  • 1
  • 8
  • 23
0

You can't create a Fragment with startActivity. You need to create the fragment with bundle like this:

ProfileFragment fragment = new ProfileFragment();
Bundle args = new Bundle();

args.putString("name", users.name1);
args.putString("family", users.family1);
fragment.setArguments(args);

// then tell the FragmentManager to attach the fragment
// to the activity
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.your_placeholder, fragment);
ft.commit();

Then in your onCreateView get them with:

String name = getArguments().getString("name", ""); 
String family = getArguments().getString("family", ""); 

Please remember that you need to move the return code to the last of onCreateView method and change your code to something like this:

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

   TextView pn =view.findViewById(R.id.pn);
   ...

   return view;
}
ישו אוהב אותך
  • 28,609
  • 11
  • 78
  • 96