0

I have an activity like below. In this activity I have more than 10 images. Images change with next and back buttons. I want to save the state of this activity. For example , if I am at 5th image and I closed the activity or app. Now when I start the activity or app then it starts from the 1st image, not from the 5th image. It should start from the 5th image where I have left it. Kindly help me with code or edit my code.

 int[] images = { R.drawable.r1, R.drawable.r2, R.drawable.r3, R.drawable.r4, R.drawable.r5, R.drawable.r6, R.drawable.r7, R.drawable.r8, R.drawable.r9, R.drawable.r10 /// so on}; 
   public void onCreate(Bundle savedInstanceState) { 
   super.onCreate(savedInstanceState);
      setContentView(R.layout.main); 

    hImageViewPic = (ImageView)findViewById(R.id.idImageViewPic); 
     iButton = (Button) findViewById(R.id.bNext); 
     gButton = (Button) findViewById(R.id.bPrev);
      //Just set one Click listener for the image 
    iButton.setOnClickListener(iButtonChangeImageListener); 
    gButton.setOnClickListener(gButtonChangeImageListener);
    } 
     View.OnClickListener iButtonChangeImageListener = new OnClickListener() { 
      public void onClick(View v) {
   //Increase Counter to move to next Image 
      currentImage++;
      currentImage = currentImage % images.length;
      hImageViewPic.setImageResource(images[currentImage]);
      }
     }; 
  View.OnClickListener gButtonChangeImageListener = new OnClickListener() { 
    public void onClick(View v) {
     //Increase Counter to move to next Image 
    currentImage--; 
    currentImage = (currentImage + images.length) % images.length; 
     hImageViewPic.setImageResource(images[currentImage]);
    }
     };

Kindly suggest with code.

margino
  • 3
  • 1

3 Answers3

0

Try to use SharedPreferences to store your last image position.

    private ImageView hImageViewPic;
    private Button iButton;
    private Button gButton;

    private SharedPreferences sharedPreferences;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        hImageViewPic = (ImageView) findViewById(R.id.idImageViewPic);
        iButton = (Button) findViewById(R.id.bNext);
        gButton = (Button) findViewById(R.id.bPrev);

        sharedPreferences = getSharedPreferences(getString(R.string.app_name), MODE_PRIVATE);

        hImageViewPic.setImageResource(images[sharedPreferences.getInt("LastSeenImagePosition", 0)]);
        //Just set one Click listener for the image
        iButton.setOnClickListener(iButtonChangeImageListener);
        gButton.setOnClickListener(gButtonChangeImageListener);
    }
    View.OnClickListener iButtonChangeImageListener = new OnClickListener() {
        public void onClick(View v) {
            //Increase Counter to move to next Image
            currentImage++;
            currentImage = currentImage % images.length;
            hImageViewPic.setImageResource(images[currentImage]);
            SharedPreferences.Editor editor = sharedPreferences.edit();
            editor.putBoolean("LastSeenImagePosition", currentImage);
            editor.commit();
        }
    };
    View.OnClickListener gButtonChangeImageListener = new OnClickListener() {
        public void onClick(View v) {
            //Increase Counter to move to next Image
            currentImage--;
            currentImage = (currentImage + images.length) % images.length;
            hImageViewPic.setImageResource(images[currentImage]);
            SharedPreferences.Editor editor = sharedPreferences.edit();
            editor.putBoolean("LastSeenImagePosition", currentImage);
            editor.commit();
        }
    };
Haresh Chhelana
  • 24,720
  • 5
  • 57
  • 67
0
 int currentImage;
   int[] images = { R.drawable.r1, R.drawable.r2, R.drawable.r3, R.drawable.r4, R.drawable.r5,   R.drawable.r6, R.drawable.r7, R.drawable.r8, R.drawable.r9, R.drawable.r10 /// so on}; 
   public void onCreate(Bundle savedInstanceState) { 
   super.onCreate(savedInstanceState);
      setContentView(R.layout.main); 

   SharedPreferences settings = this.getSharedPreferences("imageNumber", 0);
   if(settings != null){
           currentImage= settings .getInt("lastImage", 0);
    }
    hImageViewPic = (ImageView)findViewById(R.id.idImageViewPic); 
     iButton = (Button) findViewById(R.id.bNext); 
     gButton = (Button) findViewById(R.id.bPrev);
      //Just set one Click listener for the image 
    iButton.setOnClickListener(iButtonChangeImageListener); 
    gButton.setOnClickListener(gButtonChangeImageListener);
    } 
     View.OnClickListener iButtonChangeImageListener = new OnClickListener() { 
      public void onClick(View v) {
   //Increase Counter to move to next Image 
      currentImage++;
      currentImage = currentImage % images.length;
      hImageViewPic.setImageResource(images[currentImage]);
      }
     }; 
  View.OnClickListener gButtonChangeImageListener = new OnClickListener() { 
    public void onClick(View v) {
     //Increase Counter to move to next Image 
    currentImage--; 
    currentImage = (currentImage + images.length) % images.length; 
     hImageViewPic.setImageResource(images[currentImage]);
    }
     };

at the onPause method:

@Override
protected void onPause(){
    super.onPause();
    SharedPreferences settings = getSharedPreferences("imageNumber", 0);
    SharedPreferences.Editor editor = settings.edit();
    editor.putInt("lastImage",currentImage  );
    editor.commit();
}
mmlooloo
  • 18,937
  • 5
  • 45
  • 64
  • Hello dear in some of cases onStop() not be called so you can not write this code in it. – Haresh Chhelana Sep 26 '14 at 07:16
  • @Haresh Chhelana hello why onStop is not called? `in some of cases onStop() not be called` can you tell me those cases? according to android doc it is always called. – mmlooloo Sep 26 '14 at 07:17
  • when the system is low on memory then onStop() not called. – Haresh Chhelana Sep 26 '14 at 07:22
  • @Haresh Chhelana sorry but it is called in any cases because if the `activity` is on `foreground` never be killed if it is on `background` `onStop` already called. – mmlooloo Sep 26 '14 at 07:24
  • @margino I have just checked the doc again you must put last code in onPause because as shown in the graph it is possibly to kill by android at onPause. look at the [graph](http://developer.android.com/reference/android/app/Activity.html) and the outgoing arrow from onPause before onStop. – mmlooloo Sep 27 '14 at 01:03
  • Instead of saving the values in onPause, read the answer I wrote below. There is a proper way to save the activity state. – Dror Fichman Sep 27 '14 at 17:17
  • @DrorFichman brother read question carefully: **I closed the activity or app.** it is not working for this requirement. – mmlooloo Sep 27 '14 at 17:20
  • I read the question, have you read the documentation from the link in my answer? onSaveInstanceState - "Called to retrieve per-instance state from an activity before being killed so that the state can be restored". Have you tried it and it didn't work? – Dror Fichman Sep 27 '14 at 19:22
  • @DrorFichman you got confused between when activity is destroying and while the process of app still is alive, (for example when you rotate your phone you can use this), and when the process of your app is destroying. When the process of your app is killed you can not, you must use other option like saving in a file and database or sharedpreferances, it is obvious for me you can try yourself. you can even ask on SO if you do not trust me, so you will diffidently get this answer. the doc assumes the app process is alive. – mmlooloo Sep 28 '14 at 02:42
  • Hi, you've made the question much clearer now. In this case you're right, I think. I wish you would have written sooner that the perocess is killed, so far you wrote the app is killed, or you close the app. And one more question- to what scenerio are you preparing here? Why should the app's process be killed? I'm asking because the OS gives you a chance to handle exiting the properly, and the only reason I know it can't is when some signal (such as SIGSEGV) is sent. So.. is it this or something else? – Dror Fichman Sep 28 '14 at 04:03
  • @DrorFichman when you close your app by pressing the back button your process of your app is likely to be killed because of low memory, but android set priority to kill which process, look at [Process lifecycle](http://developer.android.com/guide/components/processes-and-threads.html) – mmlooloo Sep 28 '14 at 04:30
  • When navigating back the activity is not saved, as android says: " .. when a user navigates back from activity B to activity A: there is no need to call onSaveInstanceState(Bundle) on B ". So for this case, when you want to persist data for aome future activity, I guess your solution is the way to go. As for activity recreation due to lack of memory, I think that onSaveInstanceState is called. – Dror Fichman Sep 28 '14 at 05:25
0

The suggestions above may work but the way to do it is using the activity's - onSaveInstanceState and onRestoreInstanceState. You can look at the example in: Saving Android Activity state using Save Instance State

Or read in the android's manual : http://developer.android.com/reference/android/app/Activity.html#onSaveInstanceState(android.os.Bundle)

The main advantage is that the values are saved only when really needed.

Community
  • 1
  • 1
Dror Fichman
  • 1,559
  • 1
  • 14
  • 16