BajajG
BajajG

Reputation: 2174

Lifetime of class objects : Java

My knowledge about the lifetime of Java variables and class objects is limited and hence I am asking this question. I know we can not create static classes in java. Referring to questions on SO, I know that only nested classes in Java can be static.

My requirement: I have an android application with different activities. In one activity, I am asking for user details like username and other preferences and I want these to be accessible from other activities also. I have a class with different class members that store/retrieve these values (using setters/getters).

public class ClassTest {

public int x;
String a;

/**
 * @return the a
 */
public String getA() {
    return a;
}
/**
 * @param a the a to set
 */
public void setA(String a) {
    this.a = a;
}

/**
 * @return the x
 */
public int getX() {
    return x;
}

/**
 * @param x the x to set
 */
public void setX(int x) {
    this.x = x;
}
}

However, when I want to access the value of these variables from another activity, I create an object of this class and there is my problem- all the class members are now initialised again and the stored values are gone!

ClassTest object=new ClassTest();
object.setA("foo bar");

Using the object in another activity, I create a new object of the class:

ClassTest object=new ClassTest();

and there! The values are gone!

One thing I can do is save these in properties file and read them whenever I want but I wish there is a way I can do this using the class object.

Any ideas on how I can achieve this are welcome!

Thanks in advance :)

Upvotes: 1

Views: 1080

Answers (2)

Afzaal Ahmad Zeeshan
Afzaal Ahmad Zeeshan

Reputation: 15860

The values would be gone, because you're creating a new instance of the Class Object.

You should create a Bundle and use its value.

Intent i = new Intent(this, ActivityTwo.class);
String data = "Love for all, Hatred for none!";
//Create the bundle
Bundle bundle = new Bundle();
//Add your data to bundle
bundle.putString("something", data);  
//Add the bundle to the intent
i.putExtras(bundle);

We have: Intent.putExtra(String,Bundle) vs Intent.putExtra(Bundle) and http://developer.android.com/reference/android/content/Intent.html#putExtra(java.lang.String, android.os.Bundle) to help you get started

To Save data even after .onDestroy()

To save the data after the app is destoryed or closed totally, you need to save the Application state or data in the System. You can use SQLite database provided by the Android OS.

Once the application is closed, the onCreate() method would always create new objects. It doesn't save any objects in itself. You would be required to save the data in the File System, using a .txt file as you're saying. Or by saving the UserName and Password encrypted inside the SQLite databases.

Upvotes: 1

Aniket Thakur
Aniket Thakur

Reputation: 68915

That is why we have

intent.putExtra("classTestObject", object);

You dont have to create Object instance again in each activity. Just pass the reference.

Upvotes: 1

Related Questions