0

I have my MyActivity that contains three fragments: 1 ListFragment (listFragmentTest) and 2 Fragments. My listFragmentTest contains a button. If the user click on this button it will be launched a new activity (activityTest). This activityTest contains an EditText Field, where the user can enter a title.

After click on the Button "Save" (in the activityTest) I want to send the String "title" to the listFragmentTest and show the title in an EditText Field of my listFragmentTest.

Here is the code from my activityTest for the button:

public void buttonSaveTitle(){
    saveButtonTitleToFragment = (Button) findViewById(R.id.save_button_title_to_fragment);
    saveButtonTitleToFragment.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            EditText editTextTitle = (EditText) findViewById(R.id.edit_text_title_activity);
            String title = editTextTitle.getText().toString();
            //And here: Send the String to the listFragment
        }
    });
}

I'm new to android development and I tried it with intent and bundle, but without a solution.


EDIT:

I cannot use findFragmentById. The ListFragment has no Id, because it's only a Tab. I've searched and find that I've to set the tag with FragmentTransaction.add. But I don't know where I have to insert the FragmentTransaction.add in my project. Or is there another solution without using an id or tag?

Sarah_1999
  • 33
  • 1
  • 6

5 Answers5

0

Find Fragment using Id or TAG, Then call a defined method.

Such as:

  MyFragment myFrag = (MyFragment)
                      getSupportFragmentManager().findFragmentById(R.id.my_fragment);
myFrag.updateString(string);

updateString will be a method that you've defined yourself in the fragment to update the UI.

Zain
  • 2,336
  • 1
  • 16
  • 25
0

See This answer. It uses startActivityAsResult and setResult to send data back.

Community
  • 1
  • 1
Yoav Sternberg
  • 6,421
  • 3
  • 28
  • 30
0

Start the activityTest as starting it as a result and send the data back to previous Activity.

int REQUEST_TITLE = 12345;

private void pickTitle() {
    Intent pickTitleIntent = new Intent(MyActivity.this, activityTest.class);
    startActivityForResult(pickTitleIntent, REQUEST_TITLE);
}

In your activityTest do this when done

Intent returnIntent = new Intent();
returnIntent.putExtra("title", title);
setResult(RESULT_OK, returnIntent);
finish();

In your activityTest listen for result and update your fragment

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // Check which request we're responding to
    if (requestCode == REQUEST_TITLE ) {
        // Make sure the request was successful
        if (resultCode == RESULT_OK) {
           // Get the string
           String title = data.getString("title");
           MyFragment myFrag = (MyFragment) getSupportFragmentManager().findFragmentById(R.id.my_fragment);
           myFrag.updateTitle(title);
        }
    }
}
Bojan Kseneman
  • 15,488
  • 2
  • 54
  • 59
  • Thank you very much for your answer! :) I don't understand the line `if (requestCode == ENTER_TITLE)`. What I should enter instead of "ENTER_TITLE"? – Sarah_1999 May 08 '15 at 17:19
  • Sorry. REQUEST_TITLE ... or name it whatever you want. It's just an int. I suggest you make a class RequestCodes and put it in as a public static so you can access it everywhere. Make sure the value is the same when you request and when you get the result – Bojan Kseneman May 08 '15 at 17:22
  • Thanks! I will try your version now :) – Sarah_1999 May 08 '15 at 17:27
0

You can put bundle in Fragment

MyFragmentClass mFrag = new MyFragmentClass();
Bundle bundle = new Bundle();
bundle.putString("somekey", "someValue");   
mFrag.setArguments(bundle);

getSupportFragmentManager().beginTransaction().replace(R.id.someFrag, mFrag).commit();
Murtaza Khursheed Hussain
  • 15,176
  • 7
  • 58
  • 83
0

You can put Bundle in Fragment

MyFragmentClass mFrag = new MyFragmentClass();
Bundle bundle = new Bundle();
bundle.putString("somekey", "someValue");   
mFrag.setArguments(bundle);

getSupportFragmentManager().beginTransaction().replace(R.id.someFrag, mFrag).commit();

To retrieve value from bundle

Bundle bundle = this.getArguments();
String val = bundle.getString("someValue", "");
Murtaza Khursheed Hussain
  • 15,176
  • 7
  • 58
  • 83