Flutter AndroidView throws Unhandled Exception: PlatformException - android

Created native UI for Video calling using opentok library in. Flutter.
The UI is rendering successfully for the first time, But gives below exception when user. press the back button and again rejoins the call.
Unhandled Exception: PlatformException(error, java.lang.IllegalStateException: The Android view returned from PlatformView#getView() was already added to a parent view.
This is the code that we are using to create native View.
return AndroidView(
viewType: 'opentok-video-container',
creationParams: args,
creationParamsCodec: PlatFormSpecificView._decoder);```
How can we destroy or recreate AndroidView onBackpressed event of flutter?

Related

Snackbar message being replicated

I have created a bottom navigation bar in which i have three fragments, for now,let's say fragment 1, 2 and 3. I have enabled a live data observer and it shows a message whenever an api returns the error message. The message is then shown to the user via the snackbar. I had some issues while showing the messages as my app was crashing. I have rectified the error.
The app is no more crashing but I ran into another problem. Let's say there is an error message "User not found" in fragment 3. The message is displayed in the snackbar. But when I navigate back to the fragment 1 or 2, the same error message is displayed in the snackbar. I have checked the api response and there is no error response.
private val errorObserver = Observer<Int>{
activity?.let { it1 -> Snackbar.make(it1.findViewById(android.R.id.content), it, Snackbar.LENGTH_SHORT).show() }}
This is the code I used to solve the initial problem of crashing. I don't know how to solve the second one.
Your problem is probably (you haven't posted much code) that you're holding your error state in something like a LiveData or StateFlow. When your Fragments start and begin observing that error state, if there's a current value then the observer receives that immediately, and handles it (e.g. by showing a Snackbar).
This is fine when your UI is meant to be updating to display the current state, but it seems like your errors are an event, something transient that occurs and then goes away. You basically need to clear that error value once it's handled, so that anything that observes that observable won't see that same error.
People in the comments are mentioning the SingleLiveEvent approach, but that's an old workaround that the Android team considers an antipattern now. The recommended way of consuming UI events is explained here, but it basically goes like this:
In a ViewModel, some UI state object (e.g. an error state, or an object describing the entire UI with an error state field in it) updates to hold an error value
an observer sees this change, handles the error (e.g. displaying a message), and then tells the ViewModel the error event has been consumed
the ViewModel updates the error state / UI state again with the "no error" state, or whatever (maybe the next error if there's a queue of them)
So in your case, as soon as you display the snackbar, you'd tell the ViewModel (or however you're doing things) to clear that error, because it's an event that's been handled. This is different from a persistent error state, e.g. showing a warning icon if there's a problem that needs addressing, which you'd want to show all the time (until that error state changes)

Android: LiveData observeForEver produce crash because the views are null

For the purposes of my application I have live data that observesForEver and updates views. When I exit the application to background and return to foreground the application crashes in the views that is in the observers.
Here is the crash:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.emoona.music, PID: 7897
java.lang.NullPointerException: this.titleTxt must not be null
at com.emoona.torahboxmusic.features.playmusic.playlist.SelectPlaylistFragment.updateUI(SelectPlaylistFragment.kt:157)
................
My code:
viewModel.globalController.music.observeForever { music ->
val music = music ?: return#observeForever
this.titleTxt.text = music.title
}
I declare the observer on onViewCreated
What should I do to avoid this crash? Why are views null in the observer?
I specify I don't want to do an observe(viewLifeCycle) because I need it to observeForEver

Log Stacktrace to FirebaseDatabase in UncaughtExceptionHandler, problem calling default uncaughtException method

I'm currently stuck with UncaughtExceptionHandler and Firebase Database in my Android Kotlin App.
My plan:
In the case of an uncaught exception, log the stack trace to Google Firebase.
I already have an BaseCompatActivity with an UncaughtExceptionHandler. All activities inherit from this class.
The only and the interesting part from BaseCompatActivity follows:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val currentUncaughtExceptionHandler = Thread.getDefaultUncaughtExceptionHandler()
Thread.setDefaultUncaughtExceptionHandler({ thread, exception ->
val exStackTraceString = StringWriter()
exception.printStackTrace(PrintWriter(exStackTraceString))
FirebaseDatabase.getInstance().getReference("/exception-log").push().setValue(exStackTraceString.toString()).addOnCompleteListener {
currentUncaughtExceptionHandler.uncaughtException(thread, exception)
}
})
}
Everything is working except the the default method call for currentUncaughtExceptionHandler.uncaughtException(thread, exception) because this line is never called. It seems that the complete listener never gets called. This leads to an non-closed, unusable App after the exception occured :-(
The logging into the Firebase Database is working. The only broken thing is the listener callback method.
I don't want to use System.exit() because this breaks the Exception Chain.
Can you help me?
From the documentation of Thread.setDefaultUncaughtExceptionHandler:
"Set the default handler invoked when a thread abruptly terminates due to an uncaught exception"
Your call to your Firebase Database uses an asynchronous callback. Without more information, it's hard to be sure, but considering your UI is getting stuck, something off of your Main thread is getting killed. You're able to make the call, but the result doesn't have a thread to return to because the thread has been terminated.
If you're using Firebase, I'd suggest looking at Crashlytics, they have a method for handling this.

React Native - Navigator callbacks

When I am using navigator, how do we hook into 'willFocus' and 'didFocus' events?
I am trying below code:
this.refs.sideBarNavigator.navigationContext.addListener('willFocus', this._navigatorWillFocus);
this.refs.sideBarNavigator.navigationContext.addListener('didFocus', this._navigatorDidFocus);
but the callbacks don't seem to be called when a scene is popped, pushed or replaced.
I am not sure if I am getting navigationContext correctly.

Unload event in Phonegap

I'm using PhoneGap and jQuery Mobile to develop an Android app.
I'm trying to bind a function call to the event of the user leaving the page (back button or clicking some other link).
What I have so far:
$(window).unload(function () {
//my function
})
But it does not work.
You should be using the pagebeforehide of jQuery Mobile . From jQuery Mobile documentation:
pagebeforehide event:
Triggered on the "fromPage" we are transitioning away from, before the actual transition animation is kicked off. Callbacks for this event will receive a data object as their 2nd arg. This data object has the following properties on it:
nextPage (object)
A jQuery collection object that contains the page DOM element that we are transitioning to.
Note that this event will not be dispatched during the transition of the first page at application startup since there is no previously active page.

Categories

Resources