-4

I am working on Fragments, I would like to know whether it is possible to pass data from a fragment A to a fragment B directly, without forwarding the data to their attached Activity.

Nicola Gallazzi
  • 7,897
  • 6
  • 45
  • 64
  • 1
    what do you mean by without touching ? please explain it clearly – Quick learner Mar 16 '17 at 11:44
  • Welcome to [Stack Overflow](http://stackoverflow.com/) ! Please read [How to Ask Question](http://stackoverflow.com/help/how-to-ask) and provide a [Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve) ! – Saurabh Bhandari Mar 16 '17 at 11:45
  • Possible duplicate of [how to retain data between Fragments](http://stackoverflow.com/questions/21329670/how-to-retain-data-between-fragments) – Yatin Mar 16 '17 at 11:56

4 Answers4

0

There are number of ways you can do that. One of them is by sending data in arguments like this:

private int data;
private static String PARAM_MY = "param";

public static MyFragment newInstance(int data) {
    MyFragment fragment = new MyFragment ();
    Bundle args = new Bundle();
    args.putInt(PARAM_MY , data);
    fragment.setArguments(args);
    return fragment;
}

and you can retreive it onCreate() :

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if (getArguments() != null) {
        data = getArguments().getInt(PARAM_MY);
    }
}

Other way is to use Interfaces.

Umair Adil
  • 968
  • 1
  • 10
  • 24
0

I don't think so. Every fragment is attached to an activity, there's no direct connection between two fragments, unless they are connected to the same activity. If you want to communicate between fragments, you'll have to define interfaces insides fragment and make the attached activity implement the interfaces. In the attached activity you have the two fragments instances, so you can pass (using the activity) data between fragments. It's strictly recommended by best practices, take a look at documentation for details:

https://developer.android.com/training/basics/fragments/communicating.html

Nicola Gallazzi
  • 7,897
  • 6
  • 45
  • 64
0

If the activity is the same you can keep the data in the activity.

Activity

public class MyActivity extends Activity {

    private MyObject myObject = new MyObject();

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

    public MyObject getMyObject() {
        return myObject;
    } 
} 

Fragment

public class MyFragment extends Fragment { 

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

        MyActivity activity = (MyActivity) getActivity();
        MyObject myObjectFromActivity = activity.getMyObject();
        return view; 
    } 
} 
Thiago Neves
  • 91
  • 1
  • 7
0

The main question would be what are you trying to achieve and what sort of data are you trying to pass?

One of the apps I wrote for work used the Main Activity to hold all the controllers so they could easily be accessed from any view, or framgment, or associated controller. A good example would be a data-controller holding all shared app data.

Are you looking to pass strings and ints, or more complex objects?