0

I want to make use of some TextViews(cityField, updatedField) I have in my activity inside my fragment. I know it would have been easier to make use of them in the activity instead, but the problem is that I have to join in with some codes on Fragment due to getting some JSON data

Already I've gotten the id for the codes on activity

cityField = findViewById(R.id.textView4);
updatedField = findViewById(R.id.textView9);

Now I want to make use of them in my fragment So the question is - is it possible? if it's possible, how?

Already, I checked some answers on this site - Send data from activity to fragment in Android How to pass data from Activity to Fragment using bundle

but they directly didn't solve my problem.

Chinez
  • 551
  • 2
  • 6
  • 29
  • Hello, just to make sure. Your question is about to send data from the fragment to activity ? Then with this data you can change the value of 'cityField'and 'updatedField'? – rguzman Dec 16 '20 at 13:03
  • 1
    this tutorial can help you do that https://medium.com/@gunayadem.dev/sharing-data-between-activities-and-fragments-in-mvvm-made-simple-with-sharedviewmodel-21d04011cb6e – Jeferson Macedo Dec 16 '20 at 13:08

5 Answers5

1

You should create a SharedViewModel and let the Activity set values of variables like cityField and updateField while the Fragment Observe the changes done to them, basically the Activity will Set and Fragment will Get

for more information about ViewModels check this: https://developer.android.com/topic/libraries/architecture/viewmodel

Mahmoud Omara
  • 533
  • 6
  • 22
1

You can access these instances from your fragment like this:

String cityFieldFragment = (activity as YourActivity).cityField;

String updatedFieldFragment = (activity as YourActivity).updatedField;

If I understood this right, the fragment lives in your activity, so you are able to access the data from it, just make sure that the instances are public.

BWappsAndmore
  • 491
  • 3
  • 17
  • Thanks, I tried this in the fragment but didn't work – Chinez Dec 16 '20 at 13:15
  • downvoted, having an object of the needed fragment to call methods in while it will work, it will cause too many problems with the lifecycle of the app, the ViewModel is the best approach for this while it is slightly advanced. – Mahmoud Omara Dec 16 '20 at 13:17
0

if the fragment is not already created you can make use of the constructor of the fragment and pass arguments to it. If the fragment is already created, create a method inside the fragment and invoke it in the activity.

0

Let's say you want to pass it from an activity to another activity that contains fragments. First, you need to pass the data like so :

Intent intent = new Intent();
                    intent.setClass(FirstActivity.this, SecondActivity.class);
                    intent.putExtra("name", username);
                    intent.putExtra("pass", password);
                    startActivity(intent);

In this case, the fragment is inside the SecondActivity.java but to receive the data, we need code inside the Fragment.java instead of the SecondActivity.java.

Bundle extras = getActivity().getIntent().getExtras();
        if (extras != null) {
            getUsername = extras.getString("name");
            getPassword = extras.getString("pass");
        }

I haven't tried to pass the data directly to the fragment, but you can try with the same code. All you need to do is changing the intent instead of intent.setClass(FirstActivity.this, SecondActivity.class); to intent.setClass(FirstActivity.this, Fragment.class);.

Hiraeths
  • 380
  • 3
  • 16
0

Have you tried with SharedPreferences?

in you MainActivity

// create sharedPrefences file with the name "info"
SharedPreferences sp = this.getSharedPreferences("info",0);
// here the name and default value
sp.getString("name", "default value");
// create editor
SharedPreferences.Editor editor = sp.edit();
// enter a new value
editor.putString("name", "new value");
// save changes
editor.apply();

in your Fragment

SharedPreferences sp = getActivity().getSharedPreferences("info",0);
String str = sp.getString("name", "default value"); //will get you the new value you entered
J El
  • 100
  • 7