-1

I have two activities Activity_1 and Activity_2 respectively. Now I want to send data between these activities. I have two options :

1 - Using Intent.putExtra()
2 - Using static variable

So please, tell me what is the best approach to send data between these activities.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Om Prakash Gupta
  • 111
  • 3
  • 15
  • 1
    Yes you can use static varaiable but it might end up with a memory leak so passing data using intent is the best one .. aur ye questions ko downvote pta ni kon kri ja ra .. :P khair .. go for passing data in intent .. happy coding – Adeel Turk Mar 06 '18 at 14:02
  • 1
    Possible duplicate of [Static variable or pass variable via Bundle?](https://stackoverflow.com/questions/21158608/static-variable-or-pass-variable-via-bundle) – Minas Mina Mar 06 '18 at 15:13
  • nice explained @ Adeel Turk. – Om Prakash Gupta Mar 07 '18 at 05:33

1 Answers1

0

You can do as you wish. You could even store the data into a database, and have your second Activity read it, if you want.

But Android offers a intra-activity communication model which is standard for all applications. Why not use it?

So, Please tell me what is the better approach to send data between these activities.

You already know the answer for that.

You won't find a framework that advises to pass data between components via static variables.


As a personal note, I do not get why public static non-final fields are allowed in java.


EDIT

in my answer above I'm talking about dynamic data. If you are concerned with constants, then I don't see why you shouldn't use public static final fields.

payloc91
  • 3,724
  • 1
  • 17
  • 45
  • I agree with you, but In some case, if need share complex data over 3 activities, we may use static. such as the case, A->B, B->C, and both A, B and C can update the shared data. We can use intra-activity, but seems not convenient. What's your idea on this case? – Stony Aug 20 '19 at 01:02