0

Consider the situation: There is mainActivity. Form this activity we can call two fragments: fragmentA and fragmentB.

mainActivity has one getter getValue and one setter setValue.

Most of the time this is how application works: mainActivity is created. From it fragmentA is created. After back button is pressed in fragmentA, application moves back to mainActivity and goes to fragmentB.

What I want to do is, I want to setValue to mainActivity in fragmentA and get that value in fragmentB.

I am not able to do this since it looks like both fragments do not have the same instance of activity.

This is how I try to access setters/getters of mainActivity from fragmentA and fragmentB: ((MainActivity)getContext()).getValue();

What am I doing wrong?

Marius
  • 243
  • 1
  • 9

2 Answers2

1

I would recommend using the following method:

((MainActivity) getActivity()).getValue;

Also, make sure that the getValue() method in the MainActivity is set to public.

I hope this helped.

  • As much as I know - you should use getActivity for UI related work, getContext for knowing certain states of application. – Marius Apr 24 '17 at 07:47
  • Well, I use the method I mentioned for sending and receiving data to and from my main activity. Correct me if I misunderstood you. – Anas Imran Tasadduq Apr 24 '17 at 07:50
  • Coupling your Fragment to any one Activity is a poor design – OneCricketeer Apr 24 '17 at 07:53
  • @cricket_007 Could you elaborate your point, please? I don't get what you mean by coupling. – Anas Imran Tasadduq Apr 24 '17 at 07:56
  • A Fragment should never know which specifiic Fragment they are attached to. https://en.wikipedia.org/wiki/Coupling_(computer_programming) – OneCricketeer Apr 24 '17 at 08:01
  • "You should design each fragment as a modular and reusable activity component" -- Meaning, you should be able to re-use your Fragments in *any activity*... What you have isn't wrong, but there are different ways to do it, that is all – OneCricketeer Apr 24 '17 at 08:03
0

create a Bunde object in Activity

Bundle bundle = new Bundle();

insert values in bundle (can pass multiple values)
key can be any String and should be unique to each value

 bundle.setString(key,"value");

create object of the corresponding Fragment and attach the bundle to it

 fragmentobject.setArguments(bundle);

in the receiving Fragment create Bundle Object

Bundle fragmentbundle = getArguments();
String value = fragmentbundle.getString(key);

this is how you pass value between fragments and activities or activities and activities

Akshay More
  • 416
  • 4
  • 9
  • Sure, this might be the most "correct" way of doing it. However, I also need to pass BigDecimal value and as much as I know, you can't (or you need to convert it to String). – Marius Apr 24 '17 at 07:58
  • `String value = "1,000,000,000.999999999999999"; BigDecimal money = new BigDecimal(value.replaceAll(",", "")); System.out.println(money);` – Akshay More Apr 24 '17 at 08:10
  • @Marius `BigDecimal` is `Serializable`. – Kevin Krumwiede Apr 24 '17 at 08:22