Processing UI code from a stream

I know how to use handlers to update user interface elements like progress toasts, etc.

The problem I encountered is when the context goes away, for example, when you click the "Back" or "Final processing" button for any reason. This often crashes my application.

I tried using getApplicationContext () (thinking that it would be available on my entire application), but that didn't work - instead, my application crashed!

I put try catch blocks around the entire UI update code, this works, but is this necessary?

So ... What is the way to cheat?

+2
android
source share
1 answer

The problem that I encountered is when the context leaves, for example, the user clicking the "Back" button or For some reason, the activity ends. This crashes my application often.

You will also get this by default if the user changes the orientation of the screen, as the original activity will be destroyed and a new one will be created.

I tried using getApplicationContext () (Thinking it would be available throughout my application), but it didn’t work, the application never crashed!

The application context is useless in terms of user interface management.

So ... What is the way to cheat?

In the end, you need your thread to deliver the event to the right activity. Some methods that people have used include:

  • Use a listener template (for example, a service manages a thread, activity registers and unregisters listeners with a service, a thread calls listeners in key events)
  • Put the β€œcurrent” instance of the action in the static data element that the stream uses to find out which one should be used (dangerous due to memory leaks and crashes if you need multiple instances).
  • Limit the background stream to those that cache the data that the action pulls (for example, through polling) as needed
+4
source share

All Articles