2

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();
    actionOption actionOption = new actionOption();
    actionOption.show(fm,"fragment_edit_name");
    ToDoModule movie = dbList.get(position);
    int y= movie.getId();
    Bundle args = new Bundle();
    args.putInt("exampleInt", y);
    actionOption.setArguments(args);

    EditOption editOption = new EditOption();

    ToDoModule bl = dbList.get(position);
    int z= movie.getId();
    Bundle zs = new Bundle();
    zs.putInt("int", y);
    editOption.setArguments(zs);
}

First Fragment is working, but the second is not sent. Cannot send value to EditOption?

How to solve it?

Reaz Murshed
  • 22,528
  • 12
  • 75
  • 92

3 Answers3

1

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
Amit Ranjan
  • 567
  • 2
  • 11
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!

Reaz Murshed
  • 22,528
  • 12
  • 75
  • 92
  • thank you but when received id id = pref.getInt("id",0); all it take 0, how i can change 0 ? –  Sep 26 '16 at 09:41
  • Check if you committed after setting the value in the preference. You should get the correct id then. `pref.edit().putString("MOVIE_ID", movie.getId()).commit();` – Reaz Murshed Sep 26 '16 at 09:44
  • oh thank you your answer is working , i have a error in update data base and i solve it thank you very much i will accept your answer please vote to the question ^_^ –  Sep 26 '16 at 09:55
0

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.

Saveen
  • 3,925
  • 14
  • 35
  • 40