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

publicvoidonItemLongClick(View view, int position) {

    // ... Your code // Save the movie idSharedPreferencespref= 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.

SharedPreferencespref= getActivity().getSharedPreferences("MY_APPLICATION", MODE_PRIVATE);
StringmovieID= 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

publicinterfaceSetData {
    publicvoiddata(String id);
}

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

Infragment , YourFragment extendsFragmentimplementsSetData

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,

publicinterfaceTaskListener1 {
    publicvoidonResultAvailable(String result);
}

publicinterfaceTaskListener2 {
    publicvoidonResultAvailable(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.

classTestAsyncTaskextendsAsyncTask<Void, String, Void> {

    String response_result;
    publicTaskListener1taskListener1=null;
    publicTaskListener2taskListener2=null;

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

    }
    @Overrideprotected Void doInBackground(Void... unused) {

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

        returnnull;
    }

    @OverrideprotectedvoidonPostExecute(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,

publicclassFragment1extendsFragmentimplementsYourActivity.TaskListener1 {

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

}

@OverridepublicvoidonActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);


}


@OverridepublicvoidonResultAvailable(String result) {

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

Second fragment,

publicclassFragment2extendsFragmentimplementsYourActivity.TaskListener2 {

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

}

@OverridepublicvoidonActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

}


@OverridepublicvoidonResultAvailable(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"