0

I am a little bit lost in this, so I already passed data from my LoginActivity to my DashboardActivity. And it works now (thank god). But now, I would love to pass that data to a fragment.

I have this on my DashboardActivity:

Intent get = getIntent();
String userId = get.getStringExtra(LoginActivity.EXTRA_ID);

Bundle bundle = new Bundle();
bundle.putString("userId", userId);
Fragment fragment = new Fragment();
fragment.setArguments(bundle);

I have this on my Fragment:

Bundle bundle = getArguments();
String userId = bundle.getString("userId");
int uid = Integer.parseInt(userId);

Is it like this?

Francisco
  • 61
  • 3
  • 9
  • 2
    Does this answer your question? [Send data from activity to fragment in Android](https://stackoverflow.com/questions/12739909/send-data-from-activity-to-fragment-in-android) – Lalit Fauzdar Jun 05 '20 at 14:32
  • This might look silly but in here: bundle.putString(userId) I can't pass it like this, or can I? – Francisco Jun 05 '20 at 14:34
  • You need a key, like you have in your question. – Ryan M Jun 06 '20 at 10:17

2 Answers2

1

Getting data in Fragment and Activity are not done in the same way. To send extra to a fragment, You can do it this way.

In your Activity Class

Bundle bundle = new Bundle();
bundle.putString("userId", "value you want to send");

FragmentClass frag = new FragmentClass();
frag.setArguments(bundle);

Then in Fragement onCreateView

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    String strtext = getArguments().getString("userId");    
    return inflater.inflate(R.layout.fragment, container, false);
}

The issue with your code is you're getting the argument with a wrong id.

Right way

String userId = getArguments().getString("userId");
Peter Ola
  • 132
  • 8
  • I know I am asking too much. I just updated my question, should it be the way I have it there? And thank you so much for your answer. – Francisco Jun 05 '20 at 15:18
  • Is it normal to my DashoardFragment to crash? xD – Francisco Jun 05 '20 at 15:33
  • I believe that I am not defining my Fragment well. Like, the proper way. I don't know, because I believe that the problem I have is because of the way I am passing the data from my activity to my fragment. Because the value I get in my fragment is null, while I can print the id in my activity. – Francisco Jun 05 '20 at 17:18
0

From activity to fragment you should use bundle as below

Bundle bundle = new Bundle();
bundle.putString(LoginActivity.EXTRA_ID, "Extra value");
Fragment fragment = new Fragment();
fragment.setArguments(bundle);

Then in your Fragment, get the data for example in onCreate() method

Bundle bundle = this.getArguments();
if (bundle != null) {
        String extraId = bundle.getString(key, defaultValue);
}
  • I am so confused, I passed my userId data as a String through the Intent. And I got it in my DashboardActivity. And now I want to have access to the userId in my Fragment, so I should use the bundle to do it? – Francisco Jun 05 '20 at 14:46