CopyPastor

Detecting plagiarism made easy.

Score: 0.7505549788475037; Reported for: String similarity Open both answers

Possible Plagiarism

Plagiarized on 2015-04-14
by Sanjay Mohnani

Original Post

Original - Posted on 2015-01-21
by Jared Kells



            
Present in both answers; Present only in the new answer; Present only in the old answer;

Please follow the below steps to achieve your requirements-
1) In your modal you should keep track of the image, whether it's downloaded or not.
2) Insert a check in `collectionView:layout:heightForItemAtIndexPath:` method to return a default height in case the image is not downloaded else get the `image.size.height` and calculate the height of the cell from it.
3) Inside the method to create a collection view cell item insert a check to see if image is available or not, if it's available then set the image and return the cell else schedule the image for download here in BackGround thread and meanwhile return a default cell without image. When the image is downloaded switch to main thread, update the model data related to the cell to keep track that the image is downloaded for this cell and reload row for the same cell.
4) Calling reload on main thread to reload the cell for which the image is download triggers the datasource methods of the collection view for the corresponding row (i.e for the respective row 2 and 3 steps will again be followed).
5) Since, after downloading the image we have updated the model and called to reload row for the respective cell, so this time as the image is there in the modal for the respective row so the height will be calculated accordingly and the cell will be returned with the image.
These are just the steps to achieve your requirements, you need to convert the above steps to the code.

To answer the original question directly. savedInstancestate is null because your Activity is never being re-created.
Your Activity will only be re-created with a state bundle when:
- Configuration changes such as changing the orientation or phone language which may requires a new activity instance to be created. - You return to the app from the background after the OS has destroyed the activity.
Android will destroy background activities when under memory pressure or after they've been in the background for an extended period of time.
When testing your hello world example there are a few ways to leave and return to the Activity.
- When you press the back button the Activity is finished. Re-launching the app is a brand new instance. You aren't resuming from the background at all. - When you press the home button or use the task switcher the Activity will go into the background. When navigating back to the application onCreate will only be called if the Activity had to be destroyed.
In most cases if you're just pressing home and then launching the app again the activity won't need to be re-created. It already exists in memory so onCreate() won't be called.
There is an option under Settings -> Developer Options called "Don't keep activities". When it's enabled Android will always destroy activities and recreate them when they're backgrounded. This is a great option to leave enabled when developing because it simulates the worst case scenario. ( A low memory device recycling your activities all the time ).
The other answers are valuable in that they teach you the correct ways to store state but I didn't feel they really answered WHY your code wasn't working in the way you expected.

        
Present in both answers; Present only in the new answer; Present only in the old answer;