1

I have this issue of sending some data back and forth between a fragment and its container activity, I succeeded in doing it. What puzzles me is sending my data from the fragment to the activity, at first I implemented OnResume(), OnStop() and sent the data through an intent and that created an infinite loop so I removed them. Then I did setRetainInstance(true) and it worked and gave me the wanted behavior.

My Question is How my data are really being sent and where in the fragment lifecycle ?

MeknessiHamida
  • 185
  • 1
  • 8
  • 28
  • 1
    https://developer.android.com/training/basics/fragments/communicating.html – Nongthonbam Tonthoi May 05 '17 at 10:30
  • 1
    i answered on this link. you can check on this. http://stackoverflow.com/questions/43277624/call-fragment-b-from-fragment-a-using-viewpager-tabs/43278790#43278790 – Noorul May 05 '17 at 10:30
  • 1
    you can check this link also http://stackoverflow.com/questions/12739909/send-data-from-activity-to-fragment-in-android/38741476#38741476 – Noorul May 05 '17 at 10:31
  • I am not looking for the how to do it, I am not implementing any of the lifecycle methods nor intents, nothing I just specified the `setRetainState(true)`, Why the data is being sent and how I want to undrestand the logic behind it – MeknessiHamida May 05 '17 at 10:40
  • to add some context in the container activity its `new Fragment()` each time I enter my fragment. – MeknessiHamida May 05 '17 at 10:42

2 Answers2

1

The Right approach is to use Interfaces. Don't use onStop or setRetainInstance()

See this. It will solve you problem. Pass data from fragment to actvity

Community
  • 1
  • 1
Ishaan Kumar
  • 968
  • 1
  • 11
  • 24
1

You can also achieve this by using Interface, using an EventBus like LocalBroadcastManager, or starting a new Activity with an Intent and some form of flag passed into its extras Bundle or something else.

Here is an example about using Interface:

1. Add function sendDataToActivity() into the interface (EventListener).

//EventListener.java

public interface EventListener {

    public void sendDataToActivity(String data);
}

2. Implement this functions in your MainActivity.

// MainActivity.java

public class MainActivity extends AppCompatActivity implements EventListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public void sendDataToActivity(String data) {
        Log.i("MainActivity", "sendDataToActivity: " + data);
    }
}

3. Create the listener in MyFragment and attach it to the Activity.

4. Finally, call function using listener.sendDataToActivity("Hello World!").

// MyFragment.java 

public class MyFragment extends Fragment {

    private EventListener listener;

    @Override
    public void onAttach(Activity activity)
    {
        super.onAttach(activity);
        if(activity instanceof EventListener) {
            listener = (EventListener)activity;
        } else {
            // Throw an error!
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_my, container, false);

        // Send data
        listener.sendDataToActivity("Hello World!");

        return view;
    }

    @Override
    public void onDetach() {
        super.onDetach();

        listener = null;
    }
}

Hope this will help~

Ferdous Ahamed
  • 21,438
  • 5
  • 52
  • 61
  • 1
    +1 for the effort and explanation, am aware that I can do it using an interface, but my problem is to understand how I did it now ? does the `setRetainState(true)` is the one doing it for me althoug am making a new instance of myfragment each time or what ? – MeknessiHamida May 05 '17 at 13:17