I'm trying to create an application where there is a need of uploading images from my mobile. In this case, when I open my Gallery and choose a image and come back to my application, onCreate() is called again due to which the TextView, EditText and the booleans which I've used earlier are cleared.
Can you please help me how I can solve this issue.
Your activity is getting destroyed due to memory issue you might be doing some bitmap op or something check it out also try adding large heap=true in manifest
Related
My app represents funny sentences that my users upload, and in order to know which sentences are better I want to know if the user has taken a screenshot. The only related thing I found is from Google Maps. Anyone know how to make a "screenshot listener" or which method it invokes? Thanks.
One way to do this is by creating a class which is implementing FileObserver. Then add listener when you are loading activity ( by using onStart() activity's method) and remove listener when you are going out of your activity (by using onStop() activity's method).
Once you get FileObserver event, you have to be sure that is a picture creation.
Note that this method is not 100% safe but it's simple to make it works !
I have a method in my main class, that fetches some data from the internet. The thing is that after everything is done, if I change the screen orientation by moving the device, everything starts allover again(fetching data while displaying a loading screen). Is there somewhere I could put my method so that if my device's screen orientation changes, it won't erase everything that has been done until that moment? Thanks.
What is happening to you is that every time you rotate your activity is recreated, as per android good practices you should handle your activity being recreated because android may destroy your activity at any point if resources go low on the device. Take a look at saving the state of your activity and how to restore it and the link.
Example using onSaveInstanceState()
You can use a singleton class to store your data.
If you prefer a simpler way you can also put your data as static, so the orientation change will not throw them away.
I think that your Activity is getting recreated again. In that case,it will load again.
1). You can handle orientation change by overriding
public void onConfigurationChanged(Configuration newConfig)
and in your activity declaration in manifest file add the following line
android:configChanges="orientation|screenSize|keyboardHidden"
2). As Aerilys said in the above answer, you can use singleton class to store data. Before displaying your loading screen check if you single ton object has data or not. If yes then skip displaying your loading screen
I google a lot, but now I decided to ask..
My activity does only one simple thing - it downloads a picture from Internet using AsyncTask.
But there is a lot of problems, that I am not sure, how to solve..
1.] User rotates screen after AsyncTask have downloaded the picture.. How should I handle this screen rotation? Where to temporary save picture to be available after activity recreates?
I don't want to lock screen orientation (android:screenOrientation="landscape") or handle it by myself (android:configChanges="orientation")..
I just want to save the picture somewhere..
I know It can be done by using onRetainNonConfigurationInstance() and getLastNonConfigurationInstance(), but both are deprecated now. Bundle isn't appropriate for images, because it is made for String (Serializable) data. I can save picture to database or to file somewhere, but it is unnecessary. I read somewhere, that it can be done by Loader, but I don't know how? Is it possible?
2.] User rotates screen during AsyncTask is downloading a picture.
I want AsyncTask to continue with downloading. After download is complete, AsyncTask should save image to new (recreated) activity in onPostExecute method.
I think if AsyncTask is inner class of activity, it works..? But if I don't want it to be inner class I have to save reference to activity, for example in variable. But if I send reference to Activity as a parameter of constructor of AsyncTask, and Activity is recreated after screen rotation, the variable in AsyncTask does refer to original activity and so new one doesn't know about downloaded picture and it has to start download again..
Or is AsyncTask bad choice for downloading image? Should I use something else? Service?
So.. How to solve these problems? I am sure a lot of app developers have to solve this too.. But it is all Greek to me:(
When your AsyncTask has finished downloading the image, save it to your application's cache directory. Persist the path to the saved image in a property on your Activity class. You can then override the onSaveInstanceState method of your activity to pass the cached image path to itself during an orientation change. In your onCreate method check the savedInstanceState bundle for a saved image path. If that path exists, do not attempt to re-download the image. Instead, simply load the image from disk.
I've already answered somewhere. I found a very nice example: read this. This example for old APIs.
For new you can use setRetainInstance method of a fragment (if you use fragments).
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Android : Save application state on screen orientation change
In my app I have a listView. When starts application it automitacally loaded all phone contacts that exists in that phone. So when rotate my phone it's start load again, every time I rotate device it is start reading again and again. What should I do here, so I the contacts load just one time and even I rotate the phone.
android:configChanges = "orientation"
add this tag in your activity in manifest file.
Return the data in onRetainNonConfigurationInstance() and retrieve it in your onCreate() using getLastNonConfigurationInstance()
If getLastNonConfigurationInstance() returns null, request the data again.
These two functions are designed to be used to save data between instances of the same activity being recreated because of a configuration change like orientation.
As mentioned, you can alternatively stop Android destroying / recreating your activity on such a configuration change by adding the events you want to manually handle to android:configChanges = "XXX" in the activity tag in your manifest. This is suitable if you don't require different assets / layouts etc. when in portrait and landscape.
However, if you only add orientation to configChanges, you might experience the same problem when another event triggers the activity to be recreated - check out: Handling run time changes
You should use a 'Content Provider'. The provider will handle loading and will handle rotaions, onPauses and such. I made a frontend for a spanish digg like page and had the same problem. Here is the project so you can have a look at how you can solve the problem: http://code.google.com/p/meneameandroid/
Wanted to add some more to Yashwanth Kumar's ans. like you need to Override the OnConfigurationChange method in the activity where you are loading the content.
I am creating app that download 4-5 images from server and display as gallery.
It working fine in normal condition. but when there is incoming call or device is in sleep mode my activity load data from starting after onResume, i am not able to load data(Resume activity) from previous state of activity.
Is there any soulution?
Thanks
You should read how to handle runtime changes.
Basically, under some situations your Activity will be recreated. To preserve state you can pass objects between old and new Activity, using onSaveInstanceState() in old activity and onRestoreInstanceState() in new activity.
Check out Saving Cache Files. Save your images on pause and restore them on resume.
You should probably cache the files, so it works like this, check if files already exist, if not download, when files exist load pictures,
http://developer.android.com/guide/topics/data/data-storage.html#ExternalCache