user2219097
user2219097

Reputation: 357

Android integer increments when activity is entered

Very Simple question. I want to update an int value, every time the user enters that Activity e.g.

first time entering Activity int = 1 second time entering Activity int = 2 and so on..

This is the code I am using

public class confirmTaskForm extends FragmentActivity {

    private int id = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

       id++;  
    }   
}

However every time the Activity is entered, the int value is always the same = 1. It might sound easy but i'd really appreciate help.

Upvotes: 0

Views: 957

Answers (3)

Adnan
Adnan

Reputation: 5075

Make id static or put it in SharedPreferences if you want to keep counting even if your application restarts.

You may use this code to keep counter in SharedPreference.

private int count = 0;

protected void onCreate(Bundle savedInstanceState) {
         // getting shared preferences.
         SharedPreferences.Editor editor = getPreferences(MODE_PRIVATE).edit();
         // getting already saved count -  0 in case of first time
        int startCount = getPreferences(MODE_PRIVATE).getInt("count_key",count);
        // update count                
        startCount++;
        //restoring updated value
        getPreferences(MODE_PRIVATE).edit().putInt("count_key",startCount).commit();

        count = startCount;
      }

Upvotes: 0

Dadroid
Dadroid

Reputation: 1434

You can save it in sharedPreferences :

private int id=0;
protected void onCreate(android.os.Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    getFromPreference();
    id++;
    saveInPreference();
}

private void getFromPreference() {
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
    id = prefs.getInt("Count", -1);
}

private void saveInPreference() {
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
    Editor editor = prefs.edit();
    editor.putInt("Count", id);
    editor.commit();
}

Upvotes: 0

Blundell
Blundell

Reputation: 76458

You need to keep a reference outside of your Activity, as every time you navigate away it is reset or GC'd.

public class MyApplication extends Application {
     public static int myIdCache = 0;
}

public class confirmTaskForm extends FragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
         MyApplication.myIdCache++;  
    }   
}

AndroidManifest:

<application
  android:name="com.my.package.MyApplication"
  ... other things

The above is not a good recommended coding practice


You can also keep state using SharedPreferences

public class confirmTaskForm extends FragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
       SharedPreferences prefs = getDefaultSharedPreferences();
       prefs.edit().putInteger("id", prefs.getInteger("id", 0) + 1).commit();

       Log.d("TAG", "Id is: " + prefs.getInteger("id"));  
    }   
}

Upvotes: 1

Related Questions