In my application i have a listView.
I populated this listView 10 items at a time.
So, i fetch the 1st 10 items in the background when i am showing a spalsh screen. and once the splash screen goes away user can see the 1st 10 items.
Now in the Activity which contains this listView i wrote something like this:
onLastItemReached(){
getNextten();
}
works fine. and when i am going to someother activity from this listViewActivity i set the flag so that all the items browsed till then will be retained when back is pressed(i mean if user scrolled down to 100 items and clicked one item and went to some activity and clicks device back button, he will be shown the 100th item and the rest 99 items doesn't need to be fetched again)
the problem is when i close the application and open it after some 5-10 mins it gets crahsed saying:
ArrayIndexOutOfBoundsException
if i am reopening it in 1-3 mins it works fine.
Sorry i cannot post any code because all the activity has around 800 lines.
Any one has any idea why i get this exception always. and 1 thing i want to know is, is there anything like if an app is in background for more than 5 mins remove all its data
Edit:
my closing my application, i mean when i am on the ListView Screen i press Home button, reopen the app after 5-10 mins
This is the StackTrace i get, I was trying to Handle unCaughtExceptions:
threadid=1: thread exiting with uncaught exception (group=0x40020660)
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.Android.WiC_MobileApp/com.Android.WiC_MobileApp.FeedListViewActivity}: java.lang.ArrayIndexOutOfBoundsException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1728)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1747)
at android.app.ActivityThread.access$1500(ActivityThread.java:155)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:993)
So after looking at my log cat it says the error is at line 201, and that line contains:
Log.d("array of product names", SplashActivity.products_array[0] + "--"
+ SplashActivity.products_array[1]);
This is an array populated in SplashActivity and made public static.
if an app is in background for more than 5 mins remove all its data
This could actually happen within 1 minute. It's just dependent on the environment that the system is running on. When you press the home button (or back button on the top activity) the system will put your app in a hibernation sort of mode. It keeps it in memory until that memory is needed. This allows the user to quickly go back to it on the event that they simply wanted to switch real quick and go back. After a few minutes though, if the user opened up a game or something, the OS may decide to wipe the app from memory. In which case, when the user reopens your app, you have to rebuild everything from scratch.
The reason your app crashes is because you're trying to retrieve the element before the app has actually rebuilt the ListView. If the app is still in memory, it works because everything is still built. If the app was removed, then the list must be rebuilt, re-inflated, or whatever. Then you try to retrieve the element.
When your app is in the background, Android can kill it at any time (simply by killing the process that your activities are running in). If the user then wants to return to your app, Android creates a new process for your app and then recreates only the activity that was on the top of the activity stack (ie: the one that was showing when your app went to the background).
From the logs it looks like your app crashes when trying to launch FeedListViewActivity. Since this is probably the activity that was showing when your app went to the background it looks like Android is trying to recreate the activity, which would imply that your process was killed while the app was in the background (see my explanation above).
You say that the data you are trying to access is set up by SplashActivity and kept in a static variable. The problem is that when Android creates a new process for you, it only recreates the top activity, not all the activities underneath that. It doesn't recreate the SplashActivity so that variable has not yet been initialized.
Here are some possible alternatives to deal with this:
Check if the process has been recreated in onCreate() of FeedListViewActivity and, if so, you'll need to recreate the list of data that the SplashActivity had previously set up for you. You can tell if the process has been recreated by having SplashActivity set a public static boolean variable to true when it runs and by checking that variable in FeedListViewActivity.
Check if the process has been recreated in onCreate() of FeedListViewActivity and, if so, simply redirect to your SplashActivity to start the application all over again.
Persist the data by saving it to a Bundle in onSaveInstanceState(). Android will call this method on your activity before the process gets killed so you have a chance to save any data you may need later when the activity/process is recreated. You'll need to restore the data in onCreate() and/or onRestoreInstanceState().
Persist the data in a file or in an SQLlite database.
You are getting ArrayIndexOutOfBoundsException because every time when you reaches to last item you are trying to read 10 more records.
But suppose your array has 50 records. When you reach to 50 you are again going to read new 10 records. it means 51-60
which are not available that's why you are getting exception
onLastItemReached(){
if(totalDispayed < yourArray.length()) <-----
getNextten();
}
You need to implement something like as i have mentioned above
Related
I have an App with 2 activities A -> B. From B the user can update data being displayed by A, so I want A to show fresh data when the user navigates back (actually not so simple, some network involved so data can be updated at any time).
The activities are listening to ddbb changes so the views can be updated when data changes. Problem is data can change while activities are in background, so I am not sure when and how i should listen for changes. I can think of two approaches:
Listen for changes during the whole Activity lifecycle (onCreate - onDestroy) and just update the views when the data changes. This will update views from background activities.
Listen for changes only when the Activity is being active or displayed (between onStart/onStop or onResume/onPause) and force a view update every time the activity comes to the foreground (since changes might have happened while activity wasn't listening).
Mixed approaches; keep the listeners the whole lifecycle but only update views when activity comes to foreground.
Im not sure which is the correct way to handle data observing while in background.
Option 2 sounds more reasonable, but having to update the views when the activity comes to foreground may lead to UI lag right when the user starts interacting with the activity.
Option 1 will cause a lot of updates every time data is updated.
Thoughts on this?
You can choose any of the three options that suits your particular use case. Though I will say that the conventional use is with start/stop. onStart says that the activity is visible to the user in some way (either fully or partially) and onStop says that the activity is definitely not visible. So if you don't care to update the UI while the activity is not showing at all, use these lifecycle states.
You just may need to be prepared to capture data that you may have missed while the activity was stopped before it gets started again (for example, if the user pressed the home button, then came back to the activity via the task switcher. So performing your query again and rebuilding the contents of views from scratch may be necessary.
I know I haven't tried anything well frankly because I don't know why this error occurs but while searching for answers on google,I think it has something to do with parcelable.And parcelable is used when we want to pass objects of custom classes.Well I am using a custom class and passing its object too ,using parceleable
1 So here is the scenario.There are a few fragments(suppose say songsfragment).One of them generates a list of all the songs.I click on any song and a new activity(say it playeractivity) is launched and the song is played in the service.Everything runs fine...
2 I go to the settings kill the app...and wait for some time (i am guessing the android os calls the activity's ondestroy method......is my guess wrong ?)
3 I open the activity from the last opened activites the song starts again(everything is normal till here)....now if i press back button instead of songsfragment,my app crashes with the error i mentioned....So why does this exactly happen???
The app doesn't crash with the same error(please read the point number 2 again)if I don't wait for some time and open the activity instantly.So this gives me a feeling that has it something to do with onSavedInstances???
I have There Activities(A,B,C) within my Application.When i start the Application
Activity A:
A:onCreate()
A:onStart()
A:onResume()
Using intent i am calling Second Activity(A -> B):
A:onPause()
B:onCreate()
B:onStart()
B:onResume()
A:onStop()
Then I click the "Home" button So the App goes to background:Now
B:onPause()
B:onStop()
After 1 or 2 hour later Again i go to home page within my device and Click the App icon it runs like:
B:onDestroy()
A:onRestart()
A:onStart()
A:onResume()
But i need to go which one Activity i quit like this,
B:onRestart()
B:onStart()
B:onResume()
I have read some articles it says like that activity killed by the system because of no longer visible.Is there any possible to fix my issue...
Thanks in advance...
You may be confusing two different things here:
Android does not kill an activity if it needs memory. What it does is that it kills the whole process that the activity is running in. In general that means that Android kills all of your activities in this situation. However, it remembers the activity stack and when the user returns to the application, Android will create a new process and then recreate each activity (in turn, as needed). It starts by recreating the activity that was on the top of the activity stack (ie: where the user left the application).
Android assumes that if the user leaves a task for a long period of time (I think this is something like 30 minutes) then he is no longer interested in that task and there is no point in remembering where the user was in the activity stack of that task because he probably doesn't care anymore. In this case, what happens is that when the user returns to the task (or restarts the application that was on the top of the activity stack in that task) Android simply clears the task back to the root activity. This has the effect that it looks like the application is starting all over again. This is the desired (and documented behaviour).
What you want to do is prevent Android from clearing the task in situarion #2. You do it by adding
android:alwaysRetainTaskState="true"
to the <activity> tag of the root activity (ie: the activity that starts your application, the one with ACTION_MAIN and CATEGORY_LAUNCHER).
I don't believe this is something you can surely control. If your activity is in background for a lot of time and meanwhile other applications need memory, the system will kill your activity to free memory.
What I have:
I have 3 activities in my app with a basic nested structure:
Activity 1 --> Activity 2 --> Activity 3
Activity 2 contains data embedded in an ArrayList<CustomClass>. CustomClass extends application (as taught here) and Activity 3 uses this to modify the CustomClass data for Activity 2.
What I want:
When the user hits the Home button and then opens my App again later, I want to return to whichever activity was on the top of my stack. (I thought this is supposed to be Android default behaviour.)
It works fine on my Eclipse emulator but on both my Google Nexus One and Huawei Honour, the task is completely restarted.
I have tried setting my activities to run in standard, singleTop and singleTask launch modes. The best result I could get on the phones was to return to Activity 1 (which is just a form) and still display what I had entered into the text fields before creating Activity 2.
I did also notice that if I hit Home then check the Running Applications under Settings, my app is not listed. Maybe Android might be destroying my task and I need to save the activity state. But this doesn't make sense if I press home and immediately re-enter the app?
Advice is appreciated.
There is no guarantee that once you hit the Home button and your Activity goes in onPause() it will keep its state (even it it keeps the same state, there is no guarantee on the actual amount of time the activity will remain in memory). This depends on the device in particular and how it handles its internal memory. (see this figure which shows the Android Activity Lifecycle).
The best practice is to always save your state in the onSaveInstanceState method. This post provides a good and easy example on how to do that.
I'm building an application using a service and the fragment compatibility pack and am running into some weird behavior. I have a FragmentActivity and a ListFragment which implements LoaderCallbacks, and when the FragmentActivity starts it kicks off a service to download data and fill in a database.
Suppose I'm on that screen, and I navigate past it by clicking on one of the rows of the ListFragment. If I then hit a nullpointer, or any other exception that causes the regular force close dialog to pop up, I hit the force close button on the dialog and the activity I'm on does indeed get shut down, but I end up back on the FragmentActivity screen, rather than say my Android/launcher home screen.
To me this says that the app is crashing but somehow the fragment activity is getting restarted. The behavior occurs if I hit the red stop button in the debug view in eclipse - the current activity gets killed but that damn fragment activity is still there.
Obviously I don't want my app to ever get a force close dialog, but really what I'm trying to figure out is if this is a symptom of me not coding something correctly. Currently I never stop the service, mostly because it gets reused over and over again throughout the app and I haven't gotten around to coding up a way to close the service safely without prematurely ending a new request to it.
Is the service keeping a reference to the Activity via the ListFragment and LoaderCallback or something? Why is this FragmentActivity seemingly invincible?
This is happening because your process is killed, but there are other activities on the activity stack. This is the expected behavior, as the system attempts to start a new process on the next activity on the stack once your offending activity was removed. See here and here.
If you want to remove all of your activities off the stack after a force close, one thing you can do is hit the back button until you are taken to the home screen. Unless you override the behavior of the back button in your app, it destroys your current activity and takes you to the previous activity. Once there are no more activities left, you will be taken to the home screen and can start your app again from eclipse, and it will start you off at your main activity.
i had run into similar problems, with Databse loading and Nullpointer Exceptions. I don't know your special case but it might help to separate the Databse out into a Content Provider if you aren't already doing that, that way you are decoupling the Fragments form the Database more, it might help with the errors.