Lifecycle question - Android Q&A, Help & Troubleshooting

I'm a fairly new Android developer and am trying to get a better
understanding of how to handle the lifecycle events.
First, I'm under the impression that every new screen I create for my
app is an activity in it's own right. Is this correct, or is there a
better way to display different screens within the same activity.
If every screen should be it's own activity, how do I manage something
like a network connection that I want to keep alive for every screen,
but I want to disconnect from when the app actually goes away? Do I
need to handle the app being destroyed in every activity?
Before JellyBean this wasn't that much of an issue as the user pretty
much "backed out" of the app to get rid of it, so handling it in the
main activity was fine. But now I can switch between apps and kill
the app no matter what screen it's on, so that's causing me some
issues.
Thanks,
Dan

About your first question:
I hate that too lol. What I do now is declaring 2 screens in the layout file, but set one to visibility="gone"
Now when you want to switch to the other screen, do:
MainScreen.setVisibility(View.GONE)
SecondScreen.setVisibility(View.VISIBLE)
That will switch the screens.
Second question: network actions should always be done in a background service. At least I think they should; my app crashed when I wanted to do it in an activity.
I don't have an answer on the last question, sorry.
Hope I helped!
Sent from my Galaxy Nexus running Android 4.2 JB

Related

True multitasking possible?

Hi all,
I'm wonsering if true multitasking is possible...
I have been using the sgs2 for a few days now.and its.my first android.phone.
Im very happy with it but jus missing one feature...
When Im watching video in a browser or app Id like to be able to quickly do something else while the.audio keeps.playing.
Is this possible?
I do know that some apps have this featur (audio players etc.)
But id like to do this with the browser or cnn app
Greetings, dutchy
Sent from my GT-I9100 using XDA Premium App
I'm not sure if it would be possible, although the video is kept in memory, it has to close in order to open another app, I don't think you could have audio playing in background during a different application. My thought, though I'm still kinda n00b-ish with Android myself.
@SgtDutchyNL
It's possible for sure, but browsers intentionally pauses/stops playback when they're "minimized", because this is what most people want in most cases. I don't know any browser app which does run flash content in the background.
Shame there isnt an option for it, maybe its possible by editing some code?
Sent from my GT-I9100 using XDA Premium App
Coming from a Palm Pre (WebOS) I really hate multi-tasking on Android devices, switching from app to app sometimes doesn't even let you carry on where you started.
Bawezy said:
Coming from a Palm Pre (WebOS) I really hate multi-tasking on Android devices, switching from app to app sometimes doesn't even let you carry on where you started.
Click to expand...
Click to collapse
Really, I've never had an issue switching between browser, sms, email, contacts and maps, all whilst listening to music.
As for video in the browser, Flash has been built to only run whilst in focus to stop the battery from draining when animation is running in a tab in the background.
To help the issue of multitasking, try installing wavelauncher on market. it is exactly like the wave launcher in webos. but it still wont fix the fact that android will close your program in the background when it needs more memory (which is why you have to restart from scratch when you "alt-tab" around)
I have a Palm Pixi Plus which I use for work email. No doubt WebOS is amazing when it comes to multi tasking, their ecosystem(h/w and s/w) should mature but I doubt if it can reach anywhere near Android. For that matter I doubt if M$ or RIM or Intel can ever achieve this.
I've been using Android for the past 2+ yrs. I must admit Android's muti tasking is rubbish. All we need is a simple button X (similar to Windows PC) to close any damn app than closing/killing in a different way in each app/phone. Its confusing at times. Good thing is Galaxy S II is a very good phone. A complete package must say..
Sent from my GT-I9100 using XDA App
You guys misunderstood, i dont hate android's multitasking, in my opinion it doesnt match that of webOS as that was the mobile os where true multitasking was derived from. As gd as the android operating system is, Galaxy s2 is making me like android more.
Sent from my GT-I9100 using XDA App
I've also noticed the problem when using sites like mega upload where you have to wait a certain amount of time before downoading a file. If the counter is on 30 seconds, I think I have time to check an e-mail or a few tweets while waiting, so I do that and when I come back to the browser the counter is still at 30 seconds...
It's usually not a big deal but I find it amusing that android fanboys made fun of iOS because it doesn't have true multitasking, although from what I can see android just saves the state of your apps and restores it when you go back to the app... it's OK in most situation but it's hardly multitasking.
Sent from my GT-I9100 using XDA App
BlueScreenJunky said:
I've also noticed the problem when using sites like mega upload where you have to wait a certain amount of time before downoading a file. If the counter is on 30 seconds, I think I have time to check an e-mail or a few tweets while waiting, so I do that and when I come back to the browser the counter is still at 30 seconds...
It's usually not a big deal but I find it amusing that android fanboys made fun of iOS because it doesn't have true multitasking, although from what I can see android just saves the state of your apps and restores it when you go back to the app... it's OK in most situation but it's hardly multitasking.
Sent from my GT-I9100 using XDA App
Click to expand...
Click to collapse
Most sites like that do this on the desktop now to keep your focus on their ads. Not sure if megaupload does but a lot do so i could just be this.
mark2222 said:
Most sites like that do this on the desktop now to keep your focus on their ads. Not sure if megaupload does but a lot do so i could just be this.
Click to expand...
Click to collapse
Yes, this happens if you try the same on your computer too.
Huh ? Never happened to me on the computer but I'll check (it would make sense and seems doable w/ javascript), it could be that indeed.
Sent from my GT-I9100 using XDA App
BlueScreenJunky said:
It's usually not a big deal but I find it amusing that android fanboys made fun of iOS because it doesn't have true multitasking, although from what I can see android just saves the state of your apps and restores it when you go back to the app...
Click to expand...
Click to collapse
No, Android has true multitasking, but that doesn't mean every app should or have to use it, does it? If I browse through internet, I'm on a site with dynamic Flash/Javascript content and I exit browser then I want it to freeze any dynamic content to minimize power consumption. This is exactly what most of people want in most situations and this is why you have problems with Megaupload. It's possible to create a browser which will run Megaupload counter in the background, but no one has created one so far, because it wouldn't be that good.
You all should understand this isn't a technical issue, it's a design concern. If you switch/exit an app then OS (no matter: mobile or desktop one) has to do one of the following:
run it normally in the background
pause it
close it
give user a choice
Each of above has its pros and cons. Desktop OSes use first option, because they don't care about power and RAM consumption. They also use last option, because on a big screen, with mouse and full keyboard there is no problem to add such controls.
But using same solution on a phone would be a disaster, I think. Older mobile OSes use third option: they close app after exiting it, they don't have MT at all. iOS is somewhere between pause and close. Android is between pause and run: it pauses apps by default, but gives them a possibility to run in the background if they want. And this is a big difference between iOS and Android. On the iOS app can't run in the background even if both its developer and user want that. On the Android it depends fully on a developer choice.
What I want to say is: there is no single, perfect solution to this problem. If clicking "home" button would not pause an app by default then OS would be a piece of **** to me. Even if it would give me a possibility to easily pause/close an app - still POS. Pause should be a default action to make OS easy to use, so I think Android design is really good. One thing which could be done is to give more control to user. E.g. an option to resume/not pause an app on demand, so it will run similarly to foreground mode.
And how does work this awesome MT of webos (I don't really know)? Please, don't say it runs apps in the background by default - if so then sorry, but as I said it's a POS to me.
Brut.all said:
And how does work this awesome MT of webos (I don't really know)? Please, don't say it runs apps in the background by default - if so then sorry, but as I said it's a POS to me.
Click to expand...
Click to collapse
Multitasking on a WebOS, for those who haven't yet seen it: http://youtu.be/wZht-a0VlD8
Brut.all said:
No, Android has true multitasking, but that doesn't mean every app should or have to use it, does it? If I browse through internet, I'm on a site with dynamic Flash/Javascript content and I exit browser then I want it to freeze any dynamic content to minimize power consumption. This is exactly what most of people want in most situations and this is why you have problems with Megaupload. It's possible to create a browser which will run Megaupload counter in the background, but no one has created one so far, because it wouldn't be that good.
You all should understand this isn't a technical issue, it's a design concern. If you switch/exit an app then OS (no matter: mobile or desktop one) has to do one of the following:
run it normally in the background
pause it
close it
give user a choice
Each of above has its pros and cons. Desktop OSes use first option, because they don't care about power and RAM consumption. They also use last option, because on a big screen, with mouse and full keyboard there is no problem to add such controls.
But using same solution on a phone would be a disaster, I think. Older mobile OSes use third option: they close app after exiting it, they don't have MT at all. iOS is somewhere between pause and close. Android is between pause and run: it pauses apps by default, but gives them a possibility to run in the background if they want. And this is a big difference between iOS and Android. On the iOS app can't run in the background even if both its developer and user want that. On the Android it depends fully on a developer choice.
What I want to say is: there is no single, perfect solution to this problem. If clicking "home" button would not pause an app by default then OS would be a piece of **** to me. Even if it would give me a possibility to easily pause/close an app - still POS. Pause should be a default action to make OS easy to use, so I think Android design is really good. One thing which could be done is to give more control to user. E.g. an option to resume/not pause an app on demand, so it will run similarly to foreground mode.
And how does work this awesome MT of webos (I don't really know)? Please, don't say it runs apps in the background by default - if so then sorry, but as I said it's a POS to me.
Click to expand...
Click to collapse
Thanx for this very informative post.
And indeed I think the best solution would be for each app to have a default behavior as it is right now, but let the user force the app to keep running at all times if they need to for some reason and don't care about the performance hit.
Ty for all the info, but still, I just want the option to run some app normally in the background, its annoying when youre watching a movie and quickly need to check mail, i like having it playing on
Sent from my GT-I9100 using XDA Premium App
Intresting to me that good old WM 6.5 was true multitasking and could run for the best part of a week in standby Android in all it's forms, with background program managment, can barely do a couple of days !
Interesting in the video that I didn't see any evidence of true multitasking
Like some1 mentioned already, android is a multiple task/thread OS. So you can pretty much do anything. It's down to the developer. If you wanted music/vid to play in the background when not active in foreground tis easy to extend your application to have a service that constantly runs, and the app (UI) is just the controller. A good example of multitasking is seeing 2 things update where both are either critical data or processor intense. An example being your widgets update while live wallpaper is doing something. So to me, there is either multitasking or not, not really true multitasking. I think from the video of webOs I would call it a task/app/thread visual controller, and not a few words like "true multitasking".
Just my brain dumb on the issue

Is there one decent timer for Android?

This may be an area where the dumbphones win, but I've only been able to find one app that is just a timer--open the app, you've got a timer--and it's way too cumbersome.
When the app opens, I need to see:
A number pad.
A Next button (for moving from hh/mm/ss).
A Start button.
ETA: Also, number pad input needs to overwrite the current field, not insert.
These features have been available on every dumbphone for over a decade. Can't they be done in Android?
This isn't exactly what you are looking for, but I use it.
https://market.android.com/details?id=com.hybrid.stopwatch
It has a stopwatch, too, but remembers which mode you chose and launches in that mode.
Yeah, I tried that one and it has all the same issues as Kitchen Timer, though it's nicer to look at. Who decided an Android timer would use this cumbersome +/- interface? It's made worse by the fact that once you get a cursor in a field, it inserts rather than overwrites, and won't jump fields.
Ideally, the only actions necessary to set a timer should be open-0-0-7-3-5-start, no navigation, no clicking up and down as if we had a device with two buttons (i.e. a watch).
Yeah, I looked around for a timer too. This was the easiest to use that I found. I rarely have to change the time on it so for me I launch it and hit go. But if I was constantly changing the value, it would be annoying. Makes you want to go out and write your own!
I'm poking around in App Inventor to see what I can manage--I would be lost writing code from the ground up. I think I should at least be able to come up with something with multiple one-button presets on the starting page, and they can probably be user-programmable, but I don't think App Inventor has the commands to give focus to a numerical text-box (thereby highlighting the contents for replacement and launching the number pad) on app initialization.
If you want, take the time to send me some EXACT specifications and I'll see if I have time to whip something up; we're not talking 10 year global weather modeling here! LOL
TIA,
Roots
Rootstonian said:
If you want, take the time to send me some EXACT specifications and I'll see if I have time to whip something up; we're not talking 10 year global weather modeling here! LOL
TIA,
Roots
Click to expand...
Click to collapse
I'm actually making progress in App Inventor, but if it comes to naught, I'll PM you.
Well, it's not finished and it's not pretty, but it's a timer
So far, I can't get the box to grab focus when the app opens, but it's still quicker for entering a new value than the others on the market. The empty red bar will bring up the number pad. Hit hours, mins, or secs to start the timer with the relevant units. Start/Pause work, and if you enter, for instance, "90 + Mins" it will convert it to 1 hour 30 mins. More than two digits in the Hours box will break the UI, but it will count them down
The preset buttons and persistent notification box don't do anything yet, and it doesn't remember the last value entered if you close the app yet, but the core functionality is there.
http://dl.dropbox.com/u/7299403/TimerSAUR.apk
ETA: enabled persistent notifications and shake-to-stop.
ETA2: remembers last value entered and whether Persist was checked, made some visual tweaks and added an icon.
ETA3: re-skinned the whole thing and hid the preset buttons for now. I can add features, but I'd call it a complete app, and more functional than anything on the market, for my purposes.
ETA4: I, um, even fixed those script errors that caused FCs if you tried to start a timer with all fields empty. Details.
D'oh! As far as I can tell, the App Inventor "clock" function pauses if the device is sleeping--not exactly practical for a timer :/
Maybe I'll have another run at it tomorrow.
Maybe you can save the time that it paused, then calculate how much time has passed when it wakes up and start from there. That won't help much if your time expires while it is sleeping, though. Will App Inventor let you prevent sleeping?
Talking to App Inventor community members, I think the clock provided is just not workable for something like this. Maybe if I'm willing to build a windmill to turn a doorknob, I could figure something out, but I'd probably be better advised to start learning real code if I want to pursue the matter.
Better yet, I could take Rootstonian up on his/her offer
Yeah, it shouldn't be too difficult to build. And since you already have some of the GUI worked out, you can just pass that on.
I've built one Android app but it was a while ago so I'm very rusty. But it sounds like it can be done pretty quickly.
Jason

[Q] Service vs. intents in the manifest file - efficiency

Hi,
This is the first time I've started a thread so home I'm in the right place etc.
Just working on my first ever app. I created it as a service as that's what I thought I needed to monitor the battery status, screen status etc. I created some intent filters within the program itself to do that and assumed the process had to be running to get them.
Then I read I could have put the intents in the manifest file and my app will be started up if it isn't already running. That sounds quite neat as I can get rid of the notification icon I have to have in the notification area (I had to use setForeground to stop Android killing off the service and then immediately re-starting it). But -
Am I going to use up more battery with this second method? (using intents to wake the process) I'm not sure how often Android is going to terminate my process but if it's quite often then the overhead of my app starting up and reading some variables I'd have to persist could be a drain.
Any rule of thumb help here? I know the answer might have a bit of "depends" in it but really looking for some general advice.
Thanks.
Peter.

[Q] Android touch data logs

For a research I am doing, I need to some way to track user touches when they are using the phone in general daily basis. The user will be fully aware about what they are recording. Any method to do would be great.
What have I tried so far?
Method 1. Create an service with overlay transparent view.
Problem Due to obvious security flaws this is prevented starting with ICS. The input touches on the transparent view is not transferred to background and hence user is not able to interact with phone normally. I tried various methods with overlay view defining as type phone or type system alert or switching between them during program execution.
Method 2. View with 1% screen size make with touch outside model
Problem As problem as previous. Touch outside only returns the if touch event happened outside without even initial x, y coordinates.
There are other methods I tried but those are highlighted. Currently, I am thinking about other options:
Option 1 - The pointer location option in developer options: In settings there is this pointer location option that I can utilize. When that option is on, all the info about touch are shown in the top panel. If I can have access to those data afterwards that would be fine too, despite the fact that there will be drawings on the screen when user is using the phone. I have traced the source code of ICS and found the core class that is making that tracking possible. I recreated that class into an service. The logcat shows all the info about touch when I am running it from app. Only problem is the same as problem 1. I cannot track it outside current app. So, if it logs the tracking info even when pointer option is turned on, how will be able to get the information later to use?
This option seems the easiest.
Option 2 - Android NDK If above method is not possible is it possible to do so using NDK? Right direction to this route is also great.
Option 3 - Custom ROM Do I really need to go for Custom ROM while doing this? I am pretty sure this is 100% sure way to do it. But it is seeming very impractical in this particular research and would like to avoid as much as possible.
I would appreciate any suggestion to the path that I can follow. Thank you all in advance.

App idea: tell time through vibration

Hello,
I have recently bought a watch with Wear OS 2. I quite like it, but I am missing one nice feature from competition watches. I am blind. Apple watch and some other specialised devices can tell you time by certain vibration patterns. e.g. 5:30 am - 5 short vibrations, then pause, then 3 short vibrations... etc. I am thinking about creating something with Tasker, but this would require connection to the phone all the time.
Would someone take this as an interesting project? Or maybe would someone give me a helping hand in developing this? I am a programmer but never programmed for Android unfortunately.
what would be your preferred way of activating the vibration?
i'm thinking about an empty watchface which could, whenever it's activated either by gesture or button, start the time pattern.
also please let me know how exactly such a vibration pattern would best look like. (first hours then minutes, ok. what about 11:34 ? also seconds are not needed i guess?)
I might do a quick app
I really like this idea because the watchface would be sooo battery friendly
btw: any idea for a good name? I think I would go for "time feel watchface" if no one's got a better idea
if everything goes right, I will have a first test version by tomorrow
update: basic watchface running. gonna implement vibration pattern tomorrow.
need info:
-i assume it's no problem that the watchface is always black, this is to save as much battery as possible
-any symbol maybe for blindness i could use for the icon / watchface preview?
-should the vibration pattern start again if you touch the screen?
----I find this problematic because screen usually is only on for a few seconds, maybe i have to let it stay on as long as the pattern is running and it would go to standby immediatly after that... so i'm not quite sure how to handle this
-is a 24 hour mode needed or should it even be default?
Hi, thanks for your response. Yes this is aimed at blind people specifically, I am blind my self. Yes blind people use smart watches, mostly from Apple, but there are also some users of Android Wear OS.
wow I read your reply just now and it seems cool. I have a few points:
A watch face is an interesting idea, but for example I would prefer it to be just an app which even does not launch a screen, does the vibration and exits. I use information I have on my watch face, I use screenreader to read info about upcoming events, battery etc. You may ask why do I need this vibration way of telling time when I can read it from the watch face? Well it is more discrete and does not bother surroundings. I could assign a specific gesture in the screenreader which would launch the app and do vibrations.
Honestly, I am probably the only Wear OS user in Czech Republic and I have to get connected with other blind people to get their opinions.
I think 12 hour format should be OK so far.
so basically you just need an app that vibrates the time when launched?
for example 5:33 x x x x x pause x x x pause x x x?
then i need to rethink that. but that should be even easier
Yes, for me this would be enough. Ideally if the app would do it without displaying any window so that the screenreader does not read anything. Is that possible? I am sorry I did not clarify this at the begining.
As to how to do vibrations:
I imagine this schema:
1 - one short vibration
2 - two short vibrations
3 - three short vibrations,
4 - four short vibrations,
5 - one long vibration
6 - one long vibration, short pause, one short vibration
...
9 - one long viberation, short pause, four short vibrations
0 - long vibration, short pause, long vibration
e.g.
10:56 - short vibration, long pause, long vibration, short pause, long vibration, long pause, long vibration, long pause, long vibration, short pause, short vibration
here's a first prototype.
do you know how to install apk to your watch? you need to enable ADB and WiFi Debugging to install wireless over ADB
let me know what you think. is it too fast?
i know it's not perfect, for example you need to exit the app to relaunch the pattern. i can fix that - can i let the app run the pattern and immediately exit? so you wouldn't need to exit manually everytime
Hello, you are amazing! So fast! Thank you very much. I tried the app.I have few questions:
1. Vibrations are quite fast. Could you make it a bit slower? Let's say that the long pause among numbers could be 1 second?
2. Could you make it so that the app does not open any window? That after launching the icon everything happens in the background?
3. Will this work also on phones or only on watch?
Thank you so much. If you don't mind, I would like to spread the word about this among other Wear OS users.
1. sure. i actually use two different long pause durations: one between the digits and one between hours/minutes
---i need to do this because leading zeros are ignored. i could also do leading zeros so you always have 4 digits and pause can be constant, but imho that's not as efficient
2. In this new version the window exits after launch, I hope that's fine. also fixes not working on second launch and makes the app not stay in RAM.
3. This version is only for Wear OS, but I could copy it to an android app.
Of course you can spread this app however you like. when it's done I can attempt to publish it to the play store.
please check this new version. is it still too fast?
also 12 a.m. seems to be treated as "0". should i change this?
new version with fixed 12 a.m.
if you are fine with this state i'm going to finish it and publish it to play store
Wow thanks, this look very well. I still have a problem with the app creating a window. The problem is that when the window is created and closed again, the screenreader reacts to that - in this case after closing the app window it reads info about previously opened window or a watch face. That is a bit problematic as my idea was to not make any sound at all, so that one can check for the time discretely.
Would it be possible to not display any window at all? Maybe running as a service which would be waiting for the launcher icon to be pressed? I am not good at Android internals so maybe it is a stupid idea.
krecoun said:
Wow thanks, this look very well. I still have a problem with the app creating a window. The problem is that when the window is created and closed again, the screenreader reacts to that - in this case after closing the app window it reads info about previously opened window or a watch face. That is a bit problematic as my idea was to not make any sound at all, so that one can check for the time discretely.
Would it be possible to not display any window at all? Maybe running as a service which would be waiting for the launcher icon to be pressed? I am not good at Android internals so maybe it is a stupid idea.
Click to expand...
Click to collapse
now I understand the problem. I need to look into this later. but I guess that's doable.
update: I tried to replicate your problem. I enabled talkback and started the app. only thing I can hear is a click when launching the app. is that what you are talking about? If so, I don't think that can be prevented as it's caused by the launch of the app.
-sorry, I didn't read correctly. so the problem is that it starts reading the previous app. that's a problem. I didn't find an easy way to fix this - as far as I read you cannot launch an app without starting an activity. at least not with an app. maybe i could make the watch sleep after launching? or you can find a solution with tasker or something similar
Hypothetic idea:
Let's say that the app would be running as a service after start. I don't know if this is possible in Wear OS.
1. After clicking the launcher icon, check if the service is already running.
2. If not, launch it. You can display even a window and close it, well while launching the service we probably won't avoid that but that's acceptable.
3. If yes, don't show anything but send some intend / activity / however it is called in Android to the running service which will do the vibration.
Is it doable?
the thing is afaik an app launch always needs to launch an activity.
if i'm wrong i hope someone corrects me
How about this?
https://stackoverflow.com/questions/2704084/how-to-launch-an-activity-without-a-ui
krecoun said:
How about this?
https://stackoverflow.com/questions/2704084/how-to-launch-an-activity-without-a-ui
Click to expand...
Click to collapse
it says you can have an app that's a service only, which starts on boot an can receive intents. but how do you call it? you need to activate something. you could maybe do it as a compilation, but as I see it it's not possible by launching an app.
I see two easy solutions:
-let the app run in an empty window
or
-let the device go to standby after vibration
update: I looked into this a bit more. I guess my solutions would not work. an app can't send the device to sleep and if I show an activity the screenreader always reads the app name...
so I can't help you any further. maybe someone else can
Hi, sorry for looong delay. I must say that I am using your app quite successfully. Sometimes it talks but mostly it does not and vibrations work as expected. Great work. Maybe you could release this app into Playstore, some other blind people could benefit from that or make more suggestions.
krecoun said:
Hi, sorry for looong delay. I must say that I am using your app quite successfully. Sometimes it talks but mostly it does not and vibrations work as expected. Great work. Maybe you could release this app into Playstore, some other blind people could benefit from that or make more suggestions.
Click to expand...
Click to collapse
Thanks for your feedback, i'm glad you like it.
I find myself running it from time to time, just to see if I can feel the time
I'm gonna try to publish it when I find time. I'm currently starting work at a different project.
Just came across this thread. Very interesting project!
I, myself, am not blind, but I like the direction this is going! I am going to try it out!
Have you published this to the Play store as of yet? This could be good! ??
Yeh, this is a unique app concept for many!
sorry, for not responding in almost a year.
best would be to bake this function into the os, or make it activate by gesture... wouldn't this be awesome? telling time without ears and eyes, just by a movement...
I just remembered this app and thought i might finish it and add it to play store.
HOPEfully this weekend

Categories

Resources