-4

How do I pass a variable of a Fragment to an activity?

For instance Fragment:

int diffDays = 0;
int diffHours = 0;
int diffMinutes = 0

Activity:

private void testMethod(){
     System.out.print(diffDays);
}
  • Duplicate of: use Google.... – Selvin Sep 08 '15 at 19:08
  • COuld not find a solution that is why I am asking – Alex mc clair Sep 08 '15 at 19:15
  • No, SO is not human search engine... Use internet search. Fx google started working before XXI century... You should know how to use it. – Selvin Sep 08 '15 at 19:16
  • I used it if you cannot help me do not comment unnecessary – Alex mc clair Sep 08 '15 at 19:18
  • 2
    What had you tried so far... Besides asking question here? Of course you didn't try to get activity in fragment, cast it to concrete activity or interface and use its method? – Selvin Sep 08 '15 at 19:21
  • If you could read properly I am not asking to get activity in fragment – Alex mc clair Sep 08 '15 at 19:32
  • If you could think properly you will know that it is the same... To **pass** var x from y to z you need z instance in y and use some method from Z to pass x... If you need to **get** field x from y in z you need y instance in z and field x should be accessible in z fx public.... – Selvin Sep 08 '15 at 19:36
  • You need to show what you've already attempted. What you have so far is not even close to useful enough to critique. As an aside: use `Log.d()` instead of `System.out.print()` – AutonomousApps Sep 08 '15 at 20:22

1 Answers1

0

Your fragment:

public class frag extends Fragment{
   private int data;
   public int getData(){
      return data;
   }
}

Then in your activity:

public class Activity{
   getFragData(){
      Fragment frag = new Frag();
      System.out.println(frag.getData());
   }
}
Joel Min
  • 3,387
  • 3
  • 19
  • 38