javad
javad

Reputation: 835

Another way that you can exchange data between activities

What is different between static field and other ways in store data when the application is run? I'm using static fields for pass data between activities and worked perfectly:

I defined this class :

public class Info 
{
    public static int ID      = 0;
    public static String NAME = "TEST";
    public static TestClass testclass = null;
}

and I can store my data anywhere:

Info.ID = 5;
Info.NAME = "USER!";
Info.testclass = new TestClass();

and I can get my data anywhere:

Info.ID
Info.NAME
Info.testclass

Upvotes: 0

Views: 570

Answers (6)

Well, that way doesn't always work in Android. Your static values are hold only while your app is running. Imagine you are sharing some content with action SEND, so you are starting another app which actually share your content (Facebook, email, etc.). Android may decide to stop completely your app if there are no resources enough to launch other app. In that point, the process of your app is completely gone and, with it, your static values. When going back to your app from the app that shared the content, you've lost your values.

I'd better use Intent extras, combined with Parcelable objects if you need to serialize more complex data.

You can easily try it if you enable in a device the option Don't keep activities under developer options, which destroys every activity as soon as the user leaves it.

Upvotes: 0

NigelK
NigelK

Reputation: 8490

It is usual to pass data between activities using extras within the intent. Such data persists for the lifetime of the receiving activity (when finished with, the garbage collector can free up the memory).

Or you can store values using SharedPreferences. These will persist between sessions and are stored as key/value pairs in a file (so don't impact memory use as such).

Or you can hold values in static fields (as you are doing here) which persist for the lifetime of your application session. However there is a significant risk with this in that the garbage collector cannot free up memory that is referenced by such fields unless you set them to null when you no longer need the reference. You should never store a reference to an activity/context/view in a static field since you'll leak the entire activity which can amout to a significant amount of memory usage.

http://android-developers.blogspot.fr/2009/01/avoiding-memory-leaks.html

You can pass a class instance within an intent if it is Serializable, e.g.:

Intent intent = new Intent(this, whatever.class);
Bundle b = new Bundle();
b.putSerializable("data", my_object);
intent.putExtras(b);
startActivity(intent);

And in the receiving activity, cast it back to whatever class your object is:

Bundle b = getIntent().getExtras();
my_object = (whatever class is it) b.getSerializable("data");

Many java classes implement Serializable and it is very simple to make your own classes serializable too.

Upvotes: 3

amalBit
amalBit

Reputation: 12181

There is something called an Application Class in android. Its like a global singleton.

  • In other words, that Application Class will be common for that entire application.
  • Application class will be that first class to load.

So it will be easier to store some randomly used values in the application class.

public class Info extends Application 
{     
     public static int ID      = 0;
     public static String NAME = "TEST";
}

Then call it in any activity by:

Info info= ((YourApplication)this.getApplication());

And in your manifest:

<application
android:name="<your package name>.GlobalApplication">

........
........

</application>

Upvotes: 0

Meenal
Meenal

Reputation: 2877

You want to share data between activities,you can use intent or shared prefernce. The difference in using these tow and static data is that,intent and shared prefrence ,at some static data can be empty or null.but sending data using above two methods gurantees that you will get data in next activity ,unless you forcefully remove preference you can refer this link for more info Static class in Java (Android) - use or not use

Upvotes: 0

Embattled Swag
Embattled Swag

Reputation: 1469

If you're changing activities I'm assuming you're using intents. What you can do is send data with the intent with myIntent.putExtra("some string",someString);.

Then you can receive the info in your new activity using

Intent intent = getIntent();
String someString = intent.getExtra("some string");

Upvotes: 2

Illegal Argument
Illegal Argument

Reputation: 10348

You can use intents for passing data between activities. Your first Activity.java

public void onClick(View v)
  {
      Intent timer = new Intent (FirstActivity.this,SecondActivity.class);
      timer.putExtra("beefType", 5000);
      startActivity(timer);
  }

Then in your SecondActivity.java file do:

nt beefType = getIntent().getIntExtra("beefType", -1);

Upvotes: 0

Related Questions