Jan 23 | 8:25 AM |
Mark M. | has entered the room |
Mark M. | turned on guest access |
Jan 23 | 9:00 AM |
Tad F. | has entered the room |
Tad F. |
Hi Mark!
|
Mark M. |
hello, Tad!
|
Mark M. |
how can I help you today?
|
Tad F. |
I have been trying to properly implement an UncaughtExceptionHandler in my app, that will essentially properly close resources down, then restart the app.
|
Tad F. |
I read through your blurb in one of your books on this.
|
Tad F. |
Currently, I'm using Jake Wharton's ProcessPhoenix like this:
|
Tad F. |
View paste
(12 more lines)
|
Tad F. |
but based on what I read in your text, you don't subscribe to the getRuntime.exit() approach which he (ultimately) uses in this library, is that correct?
|
Tad F. |
Currently, my flow looks like:
|
Tad F. |
Application class starts, initializes long running resources, THEN my launch activity (Splash Screen) starts, and passes control to MainActivity.
|
Mark M. |
quoting Jake's documentation: "This should only be used for things like fundamental state changes in your debug builds (e.g., changing from staging to production)."
|
Mark M. |
so, I don't think Jake is expecting you to use this in production
|
Mark M. |
sorry, in release builds
|
Tad F. |
Right. I saw that. So I was a little unclear about how to implement what you suggested.
|
Jan 23 | 9:05 AM |
Mark M. |
OK... um, what did I suggest? :-)
|
Mark M. |
I don't think that I have ever done some sort of auto-restart of an app
|
Tad F. |
And in my case, I need to move all initialization into the launching activity so when it re-starts all the static stuff gets set up properly, etc.?
|
Tad F. |
Let me find it and I'll post it here, hang on a sec...
|
Mark M. |
I would be comfortable with ACRA-style solutions that fork a dedicated process to display a crash-related UI to collect notes from the user
|
Tad F. |
Here's what you said: Set relevant static data members to null, then start up your launcher activity, using FLAG_ACTIVITY_SINGLE_TOP and FLAG_ACTIVITY_CLEAR_TOP to wipe out all other activities in your task. This should reset you to your original state, as if the user had launched the app.
|
Tad F. | |
Mark M. |
OK
|
Tad F. |
Right now I'm doing initialization/cleanup in my application class.
|
Mark M. |
I'm not sure how you do cleanup in an Application subclass specifically, as onTerminate() does not get called
|
Tad F. |
There are static methods in there for initializing/releasing resources.
|
Mark M. |
ah, OK
|
Tad F. |
I do the cleanup by calling that static method from MainActivity on a backpress
|
Mark M. |
then in principle you should be able to call those static methods from your uncaught exception handler
|
Jan 23 | 9:10 AM |
Tad F. |
Right - and I am doing that.
|
Tad F. |
But the issue is the restart
|
Tad F. |
Because initialization gets called from the app class right now.
|
Tad F. |
On restart, does that class get recreated?
|
Mark M. |
if by "restart" you are referring to passage of mine that you quoted, then no
|
Mark M. |
you would need to identify what logic of your Application onCreate() you need to re-apply, then isolate that into a static method that your uncaught exception handler would call
|
Tad F. |
I think what I mean by "restart" is a) cleanup, b) re-launch the app (which would cause a re-initialization of resources)
|
Tad F. |
OK - so application won't get created again itself, correct?
|
Mark M. |
correct
|
Tad F. |
And how can I be assured that the back stack is completely cleared out, what do I need to do?
|
Tad F. |
right now the app class doesn't launch anything of course - Android is doing that for me based on manifest settings.
|
Mark M. |
I think the flags that I mentioned will cover that
|
Tad F. |
And then do I manually do a startActivity at my SplashActivity entry point (the launching activity)?
|
Mark M. |
whether you start a splash activity or your real "main" activity is up to you
|
Mark M. |
if the splash screen is purely for branding purposes, I doubt that you really want to re-emphasize your brand immediately after a crash
|
Mark M. |
if real work is going on there, though, then you probably have no choice
|
Jan 23 | 9:15 AM |
Tad F. |
OK - the SplashActivity is doing some checks that are important to get done prior to MainActivity being launched (which is one reason I made it the launcher), so I will do that. Maybe hide the branding or something the second...N time through.
|
Mark M. |
BTW, with respect to the flags, I keep referring back to https://stackoverflow.com/questions/29321261/wh...
|
Mark M. |
so, I'd double-check what I discussed in that question and answers to confirm what flags are the right combo for your scenario
|
Mark M. |
(this stuff makes my head hurt)
|
Tad F. |
OK I will look at that. Btw some other SO posts talk about using a "pendingIntent", but I don't need to do that right? Just a "regular" startActivity with an intent using the flags in your post?
|
Mark M. |
what you are probably seeing are people who try using stuff like AlarmManager to allow the process to terminate, then automatically fire up the activity again at a later point
|
Mark M. |
that technique was never reliable and will fail outright on Android 10, as you cannot start an activity from the background
|
Mark M. |
and whether that Android 10 limitation will cause a problem for you here, I can't say
|
Mark M. |
but yes, a regular startActivity() should be all that you need
|
Jan 23 | 9:20 AM |
Tad F. |
OK one other related item that I've never been able to figure out. Even though at one point or another instead of that ProcessPhoenix approach I've had a System.exit(), or Runtime.getRuntime.exit() or Runtime.getRuntime.halt(). But none of those three ever seemed to actually stop the process - I'd see my uncaughtException output in the logs, but the app would "appear" to restart (i.e. my MainActivity class would appear), but it was clear that in fact it had not properly initialized.
|
Tad F. |
Does Android have some rule about attempting to re-create the last activity before the crash or something?
|
Tad F. |
some behavior I should say
|
Mark M. |
frankly, I have never quite figured out Android's rule set here
|
Tad F. |
"appear to restart" is a misnomer. I would see my MainActivity pop up.
|
Mark M. |
and I haven't spent much time with System.exit(), particularly in recent years
|
Mark M. |
and this is an area where I would not be surprised if device manufacturers tinker around with stock Android
|
Mark M. |
so, YMMV, and I don't really know what to tell you
|
Tad F. |
OK. Gut feel just tells me that doing all the shutdown/re-initialization from within that exceptionhandler is the way to go.
|
Mark M. |
somehow restarting the entire process would be cleaner and, in theory, safer -- it's just not really supported
|
Tad F. |
Another topic - I am in the need of incorporating a simple palette of drawing widgets into my app. Basically I want to incorporate a simplified version of what you can do in PowerPoint to add text boxes, shapes, free-form drawing onto a canvas, then animate those objects in various ways (entry/exit animations, motion path animations, etc.).
|
Tad F. |
Are you familiar with a good library that supports this kind of thing?
|
Mark M. |
no, but I haven't ever looked for one
|
Mark M. |
to be honest, I'll be a bit surprised if there is such a library, at least as open source
|
Jan 23 | 9:25 AM |
Tad F. |
It can be low-level - I've built these kinds of graphics processing modules in my past life, but they are a ton of work and I was hoping for a solid bootstrap library.
|
Tad F. |
ok
|
Mark M. |
the "ton of work" part is why I will be a bit surprised if there is such a library, at least as open source :-)
|
Tad F. |
Right.
|
Tad F. |
Never hurts to ask :)
|
Mark M. |
there are plenty of libraries that involve a ton of work, but that tends to be for stuff that gets widely used
|
Mark M. |
your use case, while reasonable, is fairly specialized
|
Tad F. |
Btw, I am nearing a milestone to decide on a go/no-go decision for commercialization (i.e. launch a business) that incorporates this app I've been working with. I want to have a lot of this reviewed (architecture review, implementation hot spot review, etc.), and see what needs to be re-done, thrown out, tweaked, etc. for 'real live production'.
|
Tad F. |
Is that something you consult on?
|
Mark M. |
yes
|
Tad F. |
OK - how can I email you to set up a time to discuss
|
Tad F. |
?
|
Tad F. |
or contact you, or whatever.
|
Mark M. |
all roads lead to Rome -- all @commonsware.com addresses lead to me
|
Tad F. |
ok great.
|
Mark M. |
so, while officially I'm mmurphy@commonsware.com, feel free to make up an address :-)
|
Tad F. |
Won't be in the near term, but decision will be before June 1 of this year - I'm doing a bunch of market research now.
|
Tad F. |
Thanks, as always, for your input!
|
Mark M. |
you're welcome, and I wish you luck on the project and in the market research work!
|
Jan 23 | 9:30 AM |
Tad F. | has left the room |
Mark M. | turned off guest access |