user5239794
user5239794

Reputation:

Store data in Application class when I open application second time

I store data in my application class

public class VApp extends Application {

@Override
public void onCreate() {
    super.onCreate();
}

public Double getGrandTotal() {
    return grandTotal;
}

public Double setGrandTotal(Double grandTotal) {
    this.grandTotal = grandTotal;
    return grandTotal;
}
public  Double grandTotal = 0.0;
}

I use setGrandTotal() and getGrandTotal() method in other activity to store data its work fine, my problem is when I close application an start again than grand total becomes 0.0, I want to store data in grand total when I open appliction second time.

Upvotes: 1

Views: 56

Answers (2)

Jiang Qi
Jiang Qi

Reputation: 4448

public Double getGrandTotal() {
    if (grandTotal == 0) {
        SharedPreferences sharedPreferences = getSharedPreferences("config", Context.MODE_PRIVATE);
        String value = sharedPreferences.getString("grandTotal", "0");
        grandTotal = Double.valueOf(value);
    }
    return grandTotal;
}

public Double setGrandTotal(Double grandTotal) {
    this.grandTotal = grandTotal;
    SharedPreferences sharedPreferences = getSharedPreferences("config", Context.MODE_PRIVATE);
    sharedPreferences.edit().putString("grandTotal", String.valueOf(grandTotal)).apply();
    return grandTotal;
}

public Double grandTotal = 0.0;

Upvotes: 1

Rashid
Rashid

Reputation: 1750

you should store your data in a Database or Cache of your app. i recommend you to use SharedPreferences

Below are the useful links to help you

SharedPreferences|Android

Tutorials Point Shared Preferences

Upvotes: 0

Related Questions