-1

So basically I'm trying to pass an EditText String to another fragment which is in the same Activity. Then pass that data to another Activity, depending on which button the pressed. My problem is the application works fine but I just don't see the string being created. I've tried to use a textview just to check if it works when I pass it through the first data, but nothing shows.

I just want to pass the string to another fragment then depending on the button they press pass it on the whichever Activity.

This is my code

Passing my Data to the next Fragment and going to the fragment.

mNextBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(getActivity(), ItemStorageFragment.class);
            intent.putExtra("itemName", addName.getText().toString());
            ((AddInventoryActivity)getActivity()).ToExpiration(null);
        }
    });

Grabbing the data from my First Fragment

Intent intent = getActivity().getIntent();
    value = intent.getStringExtra("itemName");

Then Sending it to the Activity

mToFreezer.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent in = new Intent(getActivity(), FreezerActivity.class);
            in.putExtra("itemNameToFreezer", value);
            startActivity(in);
        }
    });

Then Adding it to my RecyclerView

Intent i = getIntent();
    String string = i.getStringExtra("itemNameToFreezer");
    mDataset.add(string);
    mAdapter.notifyDataSetChanged();
Kristofer
  • 87
  • 1
  • 8
  • 3
    Possible duplicate of [Passing data between a fragment and its container activity](https://stackoverflow.com/questions/9343241/passing-data-between-a-fragment-and-its-container-activity) – ADM Mar 25 '18 at 18:14
  • The accepted answer represents bad practice and should not be used! --This is what Andoid Docs have to say about using `SharedPreferences` as suggested:: " It is using expensive operations which might slow down an app. Frequently changing properties or properties where loss can be tolerated should use other mechanisms." --> Instead use the `interface` model as recommend. It may appear more expensive in the initial implementation, but it is much more flexible and more powerful. – Barns Mar 27 '18 at 14:24

1 Answers1

0

Intent extras are fine to share data between Activities, but you cannot use them for Fragments directly, as you did in your code. I assume, you are a beginner, so I recommend you read in depth about Intents here: Android - Intents and Filters

There are several ways of passing data between Activities and Fragments, most common of which is passing arguments. Yet, in your situation I'd recommend using SharedPreferences. You can store String data at any point inside your Fragment or Activity and then easily take it out with these simple steps:

Input data into SharedPreferences:

SharedPreferences.Editor editor = getSharedPreferences("YourPrefsFile", MODE_PRIVATE).edit();
 editor.putString("name", "Elena");
 editor.putInt("idName", 12);
 editor.apply();

Get data from SharedPreferences:

SharedPreferences prefs = getSharedPreferences("YourPrefsFile", MODE_PRIVATE); 
String restoredText = prefs.getString("text", null);
if (restoredText != null) {
  String name = prefs.getString("name", "");
  int idName = prefs.getInt("idName", 0);
}
Sergey Emeliyanov
  • 5,158
  • 6
  • 29
  • 52
  • If using intents are fine, how come mine doesnt work. Also I tried yours should I retrieve the SharePreference in my second fragment then send it to the Activity again? – Kristofer Mar 25 '18 at 18:52
  • @Kristofer - I edited my answer, to point out your invalid `Intent` implementation – Sergey Emeliyanov Mar 25 '18 at 20:20
  • @Kristofer - You can think of `SharedPreferences` as a remote storage for basic data (Strings, numbers, booleans), which is accessible from virtually anywhere in your app, where you have `Context` - regardless: be it `Fragment`, `Activity`, or any other place. – Sergey Emeliyanov Mar 25 '18 at 20:23
  • @SerjArdovic This is an absolute horrid idea and does not reflect best practice! This is what Andoid Docs have to say about using `SharedPreferences` as you have suggested:: " It is using expensive operations which might slow down an app. Frequently changing properties or properties where loss can be tolerated should use other mechanisms." Please do not recommend bad practice in answers. – Barns Mar 27 '18 at 14:20