0

This kind of questions appear periodically. Sorry if this has been covered before, but I'm a newbie and couldn't find the appropriate answer. It deals with the correct implementation of communication between classes and activities.

I made a gallery app. It has 3 main activities: the Main one, to search for filenames using a pattern; a Thumb one, that shows all the images that matched the pattern as thumbnails in a gridview, and a Photo activity, that opens a full sized image when you click a thumb in Thumbs. I pass to the Photo activity via an Intent the filenames (an array), and the position (an int) of the clicked thumb in the gridview.

This third Photo activity has only one view on it: a TouchImageView, that I adapted for previous/next switching and zooming according to where you shortclick on the image (left, right or middle). Moreover, I added a longclick listener to Photo to show EXIF info.

The thing is working, but I am not happy with the implementation... Some things are not right.

One of the problems I am experiencing is that, if I click on the right of the image to see the next in the Photo activity, it switches fine (position++), but when rotating the device the original one at position appears.

What is happening is that Photo is destroyed when rotating the image, and for some reason it restarts again, without obeying super.onCreate(savedInstanceState), loading again the Extras (the position only changed in Photo, not on the parent activities).

I tried with startActivityForResult instead of startActivity, but failed...

Of course I can do something contrived to save the position data, but there should be something "conceptual" that I am not understanding about how activities work, and I want to do this right.

Can someone please explain me what I am doing wrong, which is the best method to implement what I want, and why?

Thanks a lot!!!

Luis A. Florit
  • 2,169
  • 1
  • 33
  • 58
  • 1
    Since you haven't posted any code whatsoever (so please do so), I will ask the obvious: Did you override `onSaveInstanceState()`? – A--C Dec 16 '12 at 21:41
  • @A--C: Yes, I did. I wrote that above. – Luis A. Florit Dec 16 '12 at 22:52
  • Well you said it's not obeying in onCreate() so maybe you're having issues with saving the bundle. Either way attach code. – A--C Dec 16 '12 at 22:58
  • @A--C: I wrote `super.onCreate(savedInstanceState);` as the first line in `public void onCreate(Bundle savedInstanceState)`. This is what you were referring to? – Luis A. Florit Dec 16 '12 at 23:08
  • No, I meant that to use your bundle you must first save your bundle. Gaurang's answer is what I was asking about. Do you override the method `onSaveInstanceState(Bundle icicle)`? Also, in your `onCreate()` do you use the Bundle for anything else but calling `super.onCreate(savedInstanceState);` Just calling `super.onCreate(savedInstanceState);` won't do a thing. – A--C Dec 16 '12 at 23:11
  • I see!! I am beginning to understand now... I'll answer the code posted by Gaurang below. Thanks!! – Luis A. Florit Dec 16 '12 at 23:26
  • @A--C: From your answer, I understand that using `Bundle`, `super.onCreate(savedInstanceState);` etc is useless unless you overwrite the `onSaveInstanceState(Bundle ???)`. Did I understand correctly? – Luis A. Florit Dec 17 '12 at 00:33
  • All I'm saying is that if you have a nice saved Bundle, you actually have to use it. If all you call is `super.onCreate` then you are letting the superclass deal with the Bundle, but the superclass is limited, it does not know how to handle all your extra saved keys (such as position). Which is why after calling super.onCreate, you have to actually **access** the Bundle, find the saved position nd go on from there. You should definitely look at tutorials for this. – A--C Dec 17 '12 at 00:40
  • @A--C: Yes, I need to learn this. Thanks!! – Luis A. Florit Dec 17 '12 at 00:42

1 Answers1

3

Try this code to store the values for the activity

Long value;

protected void onSaveInstanceState(Bundle onOrientChange) {
    super.onSaveInstanceState(onOrientChange);
    onOrientChange.putLong("myValue", value);  
}

And restore the values in onCreate():

public void onCreate(Bundle onOrientChange) {
  if (onOrientChange!= null){
   value = onOrientChange.getLong("myValue");
  }
}

Usually you restore your state in onCreate(). It is possible to restore it in onRestoreInstanceState() as well, but not very common. (onRestoreInstanceState() is called after onStart(), whereas onCreate() is called before onStart().

Use the put methods to store values in onSaveInstanceState()

Abid H. Mujtaba
  • 1,890
  • 1
  • 24
  • 20
  • I see... It turned out that I had no clue about how to use this savedinstance thing (I still don't have much yet...). So what I did was to change my `public void onCreate(Bundle savedInstanceState)` and replace it by your `public void onCreate(Bundle onOreintChange)`, and adding the other code you suggested, and it seems it is working fine now. Now: is there a problem NOT to use a `onCreate(Bundle savedInstanceState)` at all? What I mean is that, if I don't override the `savedInstanceState` method, is it necessary to use `Bundle` at all? THANKS!! – Luis A. Florit Dec 16 '12 at 23:37