Skip to content Skip to sidebar Skip to footer

Send Intent From Main Activity To Two Fragment

I have 2 Fragment and I have to send some id to the Fragment. I use this: public void onItemLongClick(View view, int position) { FragmentManager fm = getSupportFragmentManager(

Solution 1:

Its very unusual that, you're trying to pass some data to two Fragment at the same time. It would be great if you could write the situation you have there in brief in your question.

Anyway, @PrerakSola came up with a solution for saving the data you want to pass in a SharedPreference and I do think it should work in your case.

You're trying to pass a movie id to actionOption as well as to editOption. You might try to store the id first in a SharedPreference like this.

From your Activity

public void onItemLongClick(View view, int position) {

    // ... Your code 

    // Save the movie id
    SharedPreferences pref = getSharedPreferences("MY_APPLICATION", MODE_PRIVATE);
    pref.edit().putInt("MOVIE_ID", movie.getId()).commit();

    // Do not pass any bundle to the Fragment. Just transact the Fragment here
}

Now from your Fragment's onCreateView fetch the value from preference.

SharedPreferences pref = getActivity().getSharedPreferences("MY_APPLICATION", MODE_PRIVATE);
String movieID = pref.getInt("MOVIE_ID", 0);

Another way you might try to have a public static int variable which might contain the movie id and you can access it from anywhere from your code.

Hope that helps!


Solution 2:

Something like this , you can do it

public interface SetData {
    public void data(String id);
}

From your activity class or on item click listner
 SetData setData;  
 setData.setDrawerEnabled("anydata");

Infragment , YourFragment extends Fragment implements SetData

Solution 3:

hi yesterday i have done same thing and how it work, i'll give you idea.

It already answered but just i want to share my experiance.This way is perfect.

First of all create two interfaces in your activity,

public interface TaskListener1 {
    public void onResultAvailable(String result);
}

public interface TaskListener2 {
    public void onResultAvailable(String result);
}

Now come to your activity then call like this where you want to send data to fragment.I'm just giving you example.You can make it as you want.

    class TestAsyncTask extends AsyncTask<Void, String, Void> {

    String response_result;
    public TaskListener1 taskListener1 = null;
    public TaskListener2 taskListener2 = null;

    public TestAsyncTask(TaskListener1 taskListener1, TaskListener2 taskListener2) {
        this.taskListener1 = taskListener1;
        this.taskListener2 = taskListener2;

    }
    @Override
    protected Void doInBackground(Void... unused) {

        response_result = "Test data what you want to send"; 

        return null;
    }

    @Override
    protected void onPostExecute(Void unused) {

            taskListener1.onResultAvailable(response_result);
            taskListener2.onResultAvailable(response_result);

    }
}

Call like this,

 new TestAsyncTask(new Fragment1), new Fragment2)).execute();

And how to get data in fragment,

First fragment,

 public class Fragment1 extends Fragment implements YourActivity.TaskListener1 {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    view = inflater.inflate(R.layout.fragment1, container, false);
    return view;

}

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


}


@Override
public void onResultAvailable(String result) {

    Logs.d("TAG", "Fragment result1:" + result);
}
}

Second fragment,

public class Fragment2 extends Fragment implements YourActivity.TaskListener2 {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    view = inflater.inflate(R.layout.fragment2, container, false);
    return view;

}

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

}


@Override
public void onResultAvailable(String result) {

    Logs.d("TAG", "Fragment result2:" + result);
}
}

Thanks hope this will help somebody.


Post a Comment for "Send Intent From Main Activity To Two Fragment"