I have a snippet of code that calls startActivityForResult() to pick an image from the Android gallery. I have trouble understanding the lifecycle of the fragment from when startActivityForResult() is called and onActivityResult() is activated.
My activity retrieves and loads information onto a listview. It then allows user to insert pictures into the listview by sending an intent to the camera/gallery app using startActivityForResult(). This part works perfectly. The problem is that the list loses its data when the app returns from the intent and has to retrieve the information again. I have setRetainFragment(true) already.
My question is, is there any way to retain this data from when the intent is started and when it is returned? My guess would be to save an instance of it in onPause() and onResumse() but I don't quite understand how its lifecycle goes during this event.
Thank you in advance!
I have found a solution to my problem. To answer my question about the lifecycles, the fragment does go through the onDetach(), onAttach(), onCreateView() process. However, because i set onRetainInstance(true), it skips onDestroy() and onCreate(). The reason why my data is loss, is because I retrieve the data during onCreateView() and since onCreateView() is called everytime, so is retrieving data. From this, all I had to do was check if the list is empty before retrieving my data.
Another side problem, however, is that my variables are not retained. Although the list is retained and therefore my listview remains the same, boolean and int variables are reset and I don't know have a solution for this yet. If anyone can help, that would be great!
Related
I have three tabs in my view. Every time when I press the each tab some layout will be inflated and a web service call is done. I am populating some data into my inflated view through this web service call. Everything is working fine. But the problem is I don't want to call the web service every time when I press the tab for the second time. Previous data should be populated automatically without making a call to the web service. I don't think I can use onResume() method for this. Googled out many pages to find this answer but nothing is helpful. Please post the code if necessary. Thanks in advance.
These are my workarounds till now.
TabHost setCurrentTab only calls oncreate method for Activity in Tab once
http://developer.android.com/training/basics/activity-lifecycle/recreating.html
http://jnastase.alner.net/archive/2010/10/27/handling-orientation-change-in-android.aspx
Your webservice should return some data, keep this data to your local memory and check every time either this data is null or not, if null then call service otherwise inflate from local data.
First of all, you should know the state of your 'call': not calling, pending, called. You must store it somewhere (activity, shared preferences, database etc) then making a call. Now, for data: you should implement some caching (again, in activity, shared preferences, database or even in plain files).
Then, in any state of a call, you can show data from cache, if it exists, and when call is done you can store new data in cache and show it.
whenever any changes made in one tab, store value in some variable, whenever next time the page is initialized, initialize with old selected value. Please try it out.. It may solve the problem..
How to save current results / data before going to child activity so that I can restore the data when will press system up button at child activity?
use onPause and onResume method of the activity
More info in the training guides
Your activities data isnt lost when it goes deeper in the hierarchy unless you finish the activity.
Another solution which is better is use startActivityForResult
That is explained in detail here
You could use saveInstanceState for saving the data needed later when reopening the activity.
Check this answer: https://stackoverflow.com/a/151940/1101730
EDIT: It seem like this is usable when the activity is killed, which might not be your case.
So I have been looking at tutorials and messing with things for hours. I have my main activity which creates a map of object that it gets from the database. To avoid unnecessary database queries, I am trying to save that map using the onSaveInstanceState method and then restore it with the onRestoreInstanceState. I can see that it gets saved correctly using the debugger, but if I set break points in the onRestoreInstaceState method the program never breaks. Then I thought I would just do it with the onCreate() method but it's not breaking there either. The program should break in both of those places when returning from a different activity right?
Those two methods are only called if the activity is killed and restarted.
The good news is, if your activity was not killed, there's no need to restore your state. It should still be just the way it was.
If you do need to do something when switching back from another Activity, that's what onResume() is for.
See the chart for more.
I have this problem. I have an activity and a fragment inside it. I am downloading some data using async task in activity's onCreate and then using it in the fragment's onCreate (something like getActivity().getData()). I am putting the fragment into a view after the data is loaded so it runs without trouble. The problem is that when I'm relaunching the activity from background and this fragment is active it loads immediately and throws an NullPointerException because the data isnt loaded yet. My idea was to check for this in the fragment's onCreate and if I get null data I'll just destroy it and call some activity's method for reloading data and then lauch the fragment again - is it even possible?
Do you have any solution for this or maybe a better approach?
If it's possible to cache the data on the phone, I would consider it.
If you have to reload the data on every (re-)start (more like: every resume) of the app, take a look at the activity lifecycle in the android documentation:
http://developer.android.com/reference/android/app/Activity.html
Your problem should be solved, if you #Overwrite onResume() of your activity and load your data there, instead of onCreate()
I have a problem with Activity life cycle. In my server communication Activity I am downloading the list of items from the server and then setting up the Adapter for the ListView.
Everything is fine but if I press Home button on this screen and after some while (e.g. 3 hours or more) return back to the screen via application manager, application crashes. The problem is in the onTextChanged() method (which is usefull for searching via EditText) where I am calling the setAdapter() method again. There is nullPointerException because my array was somehow erased.
Why is the onTextChanged() method called again during restoring? And why was the array erased?
Thank you for your help.
Please check the activity lifecycle diagram on:
https://developer.android.com/reference/android/app/Activity.html
Your Activity goes to Pause state after you press Home.
And after 3hrs or even a shorter time,it may be killed by system for resource management.
So it need to be created again on next launch.
I think you should add code to handle onDestroy() and onStop().
Retrieve the data the same way you did originally, by overriding the
onResume()
method, checking if the data is existent or not beforehand.