Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: What's the better practice, using a global string or intents with extra data?

Tags:

android

On a current Android project, I pass some data between a couple activities. I was just curious if there is a best practice on sending data between activities. I have a string that will be updated/appended based on the results of one activity, then used for a Facebook share two activities later. Should this string be set as a global static string, then shared, or should I pass the string using intent.PutExtra?

The global string is probably less code, but means another static variable. Alternatively, the intent Extra is fine, but seems repetitive since it's being passed through a couple activities. Either way will work, just would like to know if one is preferred above another.

like image 301
Kyle Clegg Avatar asked Apr 04 '12 23:04

Kyle Clegg


People also ask

How do you pass data between two activities through intent?

We can send the data using the putExtra() method from one activity and get the data from the second activity using the getStringExtra() method.

What is intent extras in Android?

These key-value pairs are known as Extras in the sense we are talking about Intents. We'll see an example of storing a string in this Intent object using a key. //creating and initializing an Intent object. Intent intent = new Intent(this, NextActivity.class); //attach the key value pair using putExtra to this intent.

Which of the following is used to pass data between activities in an app?

It's best to use intents. It has an extensive list of overloaded methods that can be used to better transfer many different data types between activities.

Which method of the Intent class allows you to pass information to the target?

putExtra method is used.


2 Answers

In my view, only the Intent will work. On Android your application has to be prepared for the event it is killed (for example, an incoming video call puts it into background and also consumes a lot of memory so the background apps are killed). When Android restores your app, it restarts the Activity that was showing and resends the Intent that started it, because these are saved to persistent storage. But the state of other classes (including their static variables) is not saved, and if you don't save them, is lost/reset.

like image 93
ᅙᄉᅙ Avatar answered Nov 09 '22 07:11

ᅙᄉᅙ


You should always avoid using global variables. Sometimes you'll need them and in most cases this comes due to an design issue. You should not use global variables "because of less code" or easier coding. BTW that only belongs to public static variables not to constants. Global variables make your life harder because of

  • Your code is harder to read (where does that variable come from..it's out of "scope")
  • Harder to test (who reads the variable? who modifies the variable?)
  • No access control (no getter/setter)
  • Threading
  • and many many more
like image 37
207 Avatar answered Nov 09 '22 07:11

207