I have 5 images and i want to set these as src of single ImageButton. Each time on click it should change and it must retain its value if app closes
<p>Its like a fan regulator everytime image (src should be of ImageButton)should change on click and retain its value if app closes.
What should be the best way ?
i want to use just one java file</p>

<p><strong>GLOBAL VAR count = 0;</strong></p>

<p><strong>In onCreate</strong></p>

<pre><code>aButton4 = (ImageButton) findViewById(R.id.imageButton4);
SharedPreferences sharedPreferences = getSharedPreferences("NAME", Context.MODE_PRIVATE);
int f = sharedPreferences.getInt("count_key", count);
if (count == 0) {
        aButton4.setImageResource(R.drawable.reg0);
    }
    if (count == 1) {
        aButton4.setImageResource(R.drawable.reg1);
    }
    if (count == 2) {
        aButton4.setImageResource(R.drawable.reg2);
    }
    if (count == 3) {
        aButton4.setImageResource(R.drawable.reg3);
    }
    if (count == 4) {
        aButton4.setImageResource(R.drawable.reg4);
    }
</code></pre>

<p>**Then **</p>

<pre><code>    aButton4.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            count++;
            SharedPreferences sharedPreferences = getSharedPreferences("NAME", Context.MODE_PRIVATE);
            int f = sharedPreferences.getInt("clicked4", count);
            SharedPreferences.Editor editor = sharedPreferences.edit();
            editor = sharedPreferences.edit();
            if (count&lt;5) {
                if (count == 1) {
                    toggleSound.start();
                    aButton4.setImageResource(R.drawable.reg1);
                    editor.putInt("clicked4", count);
                    editor.commit();
                }
                if (count == 2) {
                    toggleSound.start();
                    aButton4.setImageResource(R.drawable.reg2);
                    editor.putInt("clicked4", count);
                    editor.commit();
                }
                if (count == 3) {
                    toggleSound.start();
                    aButton4.setImageResource(R.drawable.reg3);
                    editor.putInt("clicked4", count);
                    editor.commit();
                }
                if (count == 4) {
                    toggleSound.start();
                    aButton4.setImageResource(R.drawable.reg4);
                    editor.putInt("clicked4", count);
                    editor.commit();
                }
            }
            else {
                count = 0;
                toggleSound.start();
                aButton4.setImageResource(R.drawable.reg0);
                editor.putInt("clicked4", count);
                editor.commit();
            }
        }
    });

}
</code></pre>