Android Activity & Timer Loop - android

I am writing an app that say has two activities A & B. A is a display page while B is a settings page.
In runtime, there is a timer loop running in A which keep on updating the display. And the settings in B affects some tasks in the timer loop.
I want it possible for users to switch from A to B in runtime and make changes in B, while the timer loop is still running in A and will response to the changes made in B.
Is it possible?
Or should I place the timer loop in somewhere else?
I'm new in Android & Java. Please kindly help. Thx.

Related

Android app - Information screen

I'm developing an android app that is going to be used on a information screen. This app contains 5 (weather, next run, opening hours, etc...) activities, the plan is to loop through these activities with a timeinterval. So it starts with first activity, sleeps for like 5 seconds and then jump to next activity, and when it comes to the last activity, it should start in the front again.
What is the best solution to create a navigation system like this? Have Android some special features for things like this, or is the default startActivity() the only way?
And there is a little twist with this, some of the activities needs to retrive data from external sources by HTTP. So the activities must be done with data-query before it starts the activity.
You use startActivity to swap between activities. After you are finished with an activity, remember to call finish() - otherwise there will be a lot of instances of the same activity.
But if you want something like an information screen, you could use a single activity but use Fragments. That way you simply inflate a single fragment, and keep a timer to swap between them. You can still show the same content, but there are some differences between activity and fragment in inflating and finding the views by id.

How can I fix this intent issue?

TL;DR:
How can I give one intent a preference over another?
If two intents are run at the same time, I want only one of those intents to start.
My game has a 20 second timer running in the background service, and when that timer ends, a new activity (game over screen) starts through an intent. The user is pressing buttons to change activities (again, through intent) in those 20 seconds.
The problem is, if the user pushes a button at the same time that the timer ends, the next activity starts rather than the game over screen. Now, the timer is over, and the user is stuck randomly pushing buttons for no reason. How can I fix this? Is there any way to make sure that the Game Over screen will open up after 20 seconds?
I haven't provided much code, because it is mainly just starting intents. If you need any detail, feel free to ask me.
do timer checking on user click event should do it. or you can use flag/boolean to maintain state.
i dont know your code, just use simple conditional if..
if (timer <= 0){
//open whatever u want and reset timer again..
}
I am suggesting you to take a global variable as a flag,
let say int = 0
Now initialize it as "0" and on your both intents give condition as below:
if(i == "0"){
.
.
.
//fire your intent..
i= 1;
}
,And then flush that variable to "0" whenever you want.Hope you got idea.

Android - Start a new activity while the application is in the background

The first activity of my application is a splash screen, where some animation is displayed while some loading occurs in a background thread using AsyncTask.
Once the loading in the background is done, I want to start a new activity. What is the correct way to do that ?
Start a new activity directly from the onPostExecute method of the AsyncTask class.
Check if the current activity is displayed before starting the new activity :
If the current activity is display, start the new activity.
If the current activity is NOT display, use a flag (boolean) and check the flag during the onResume method in order to start the new activity there.
My main concern is :
If my application went to the background (due to an incoming phone call, a home key press, ...) and the background thread (AsyncTask) finished executing, and a new activity is started from the onPostExecute method while my application is still in the background : What happens ?
Will the new activity start directly as soon as my application is visible again ?
I faced a similar situation: my application went to background and after some time the app started an intent displaying another activity. However, the app's behavior now depends on the os that it's running:
on pre 4.4 devices the app silently opens the new activity and remains in background. When the user resumes the application, he is prompted with the second activity already.
on 4.4 devices (tested on 4.4.2 and 4.4.4) the app opens the second activity, but after 3-4 seconds, the app pops to foreground, interrumping the user.
Hope it helps anybody. I'm still looking solutions for the second case in order to prevent the app from popping to foreground.
From my experience i am answering your question
Question1
If you using AsyncTask you have to start new activity in OnPostExecute(). In my experience this is the correct way of doing it.
Question2
When ever your press the home key or receiving phone call. Your activity will go in to background until you exit the app by pressing back button(at that time your app is exited). So when you come back to your app your new activity will be visible if background process get finished. Otherwise you will see the start splash screen.
I think that it is dependent upon the OS of android. It has defined some built in priority model for each of the components.
Look at the answer given by commonsware.
change process priority in android
this gives brief idea about components priority.

android: ArrayIndexOutOfBoundsException when i reopen the app after some 5-10 mins

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

Best way to terminate a wizard in android

I have a reservation wizard in android, when i say wizard i mean i have multiple activities (meaning screens) that are passed from one to another until booking is complete. in each step of the way my info may be invalid and so ill have to back track 2-3 activities and start all over, all the activities i back tracked should be destroyed, they may need to invalidate a singleton data container that they filled with their data.
I thought of 2 ways to do this:
start all activities related to the wizard with startActivityForResult() so i get notification when an activity is finished and with which error code (finished OK or error occurred) and act accordingly (for instance: if in the middle an activity fails it will return failed and the previous one will return failed as well until i get to the wizard's first activity where i either declare the error with a dialog or show the 'thank you' screen.)
I can send an react on intents with certain parameters so i have a 'close and clean' intent message that close down the previous activity etc...
which way is better, any other interesting\efficient way to do this ?
I Got a hint that got me thinking in a different direction:
use an invisible Activity, or alternatively , a local service,that is the 'brains' that known on each turn of the way what action is to be taken. to remove a complete stack of unneeded activities i should use the FLAG_ACTIVITY_CLEAR_TOP on the bottom most activity i want to use next.
Another options is to enclose all
the activities i want to play with
their pposition on the stack inside
an ActivityGroup Object which keeps
all activities alive (like in Tab)
and on the stack, but you control
which one is displayed and which is
not. the activity group will contain
the state machine logic regarding
which Actvity should not be
displayed and which should be
disposed.

Categories

Resources