-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support for responding to back button on Android #3948
Conversation
Marking as work in progress as I think the two remaining TODO items do need discussion to get it right. |
@MatejMagat305 which part are you trying to simplify? One of the reasons it is not simple is because the back button is a specific event with it's own callback handler in the app on the Java side... |
sorry, my mistake, I thought that:
in stackoverflow AKeyEvent_getKeyCode(event); is already in code and is not nesesery to add special handler ..., |
it can be done like that too andydotxyz#2 |
Are you sure about this? If you don't add: @Override
public void onBackPressed() { then the default back behaviour will occur. And without the ability to call the |
I was try it with fyne/main and my changes and it catch it (do not look to error it was course by mismash version ...): |
we can also insert |
Are you certain you're using the released The issue with going backwards isn't just if there is a callback or not, its so that the callback can decide whether or not to permit the back action... If the developer wants to display a confirmation before going back then how can you do it without providing the |
well take or do not ... |
How will the Go code be able to call finish activity without the bridge code exposing that API? |
change 89 row in you call for more contrete: https://github.com/andydotxyz/fyne/pull/2/files#diff-5d878dec0c0c4e09096aef345b55e1b73598999dae4a4a73f851ac1c47b7cd33R123 - re-set context on start and resume, btw why not use Quit interface instead of GoBack interface? https://github.com/andydotxyz/fyne/pull/2/files#diff-f51d860903d6227f965ed690092de5b8677b954e250fe77eff66c9c174309829R129 - Quit as finish ... make your origin like this: w.Canvas().SetOnTypedKey(func(ev *fyne.KeyEvent) {
if ev.Name == mobile.KeyBack {
log.Println("Intercepted and issued back")
a.Driver().Quit()
}
}) my trying (working) code was: package main
import (
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/widget"
)
func main() {
a := app.New()
w := a.NewWindow("Hello")
hello := widget.NewLabel("Hello Fyne!")
w.SetContent(container.NewVBox(
hello,
widget.NewButton("Hi!", func() {
hello.SetText("Welcome :)")
}),
widget.NewButton("exit", func() {
a.Driver().Quit()
}),
))
w.ShowAndRun()
} |
Unfortunately this is an over-simplification. Going "back" is not the same as quitting an app. Also because our keyboard handlers don't return whether or not something was intercepted the underlying driver does not know if you are going to handle the back button or not. |
well, it is not my library ..., but please look at context at start and resume ..., that is hide and it would cause problems in futures ... and if the biggest problem is handle it only we want we can do https://github.com/andydotxyz/fyne/pull/3/files#diff-5d878dec0c0c4e09096aef345b55e1b73598999dae4a4a73f851ac1c47b7cd33R615 and https://github.com/andydotxyz/fyne/pull/3/files#diff-5d878dec0c0c4e09096aef345b55e1b73598999dae4a4a73f851ac1c47b7cd33R149 but again you are shef here and the most important for me is actual (jobject) context and jvm on resume |
I'm sorry I don't understand this. The lifecycle code already handles the context on resumed apps? |
well I release that, if I close app (put on background) and open it again, the pointers to context and vm (clazz and jvm) are not actual ... (not work if you want call for example finish() on activity) the solution would be use function to set actual pointers: https://github.com/andydotxyz/fyne/pull/2/files#diff-5d878dec0c0c4e09096aef345b55e1b73598999dae4a4a73f851ac1c47b7cd33R123, again your solution is good and probably this error has not efekt to them, but for example bluetooth which I prepare it is important to have actual pointers ... if you want to try it (what I mean), you can:
It is probably to next P.R., but on this case is evident why it should be done |
I still don't understand. I am using |
please try it (my code), my pure search show me that the VM and CTX working(in general), but in some situation when I need request permision or do with actual pointers ... (but not in specific cases) or look the code in onCreate(...) save VM and CTX into global variables, after app moving into background (close app and reopen from bg) and re-start app will get in onResume(...) new VM and CTX which are different from global variables which are not make actual, this would be hiden error in some case ... tomorow I will share videos ... |
Yes you cannot (and should not) save these to global variables and assume they work forever. |
finaly we are at same wave, but I m not certian, therefore I put video here ..., as you see at video when the global variables (which are at package mabileinit) are not actualiting at onResume(...) something is wrong when I try call finish(), so can I create new PR. to fix that? or you will fix at this PR? |
I don't think we are on the same page. You can see that in the Fyne code So the context available in the package variables is always up to date... |
My apologies I finally saw this happen - it seemed to not always be a problem. Hopefully 8b545a8 resolves that aspect of this PR? |
As promised @Jacalz here is a video of the work :) 2023-07-29.10-44-11.mp4 |
Just for record the app in the video above is just this: // Package main loads a very basic Hello World graphical application.
package main
import (
"fmt"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/driver/mobile"
"fyne.io/fyne/v2/widget"
)
func main() {
a := app.New()
w := a.NewWindow("Hello")
count := 0
hello := widget.NewLabel("Trapped back 0 times")
w.Canvas().SetOnTypedKey(func(ev *fyne.KeyEvent) {
if ev.Name == mobile.KeyBack {
count++
hello.SetText(fmt.Sprintf("Trapped back %d times", count))
}
})
back := widget.NewButton("Go Back", nil)
if drv, ok := a.Driver().(mobile.Driver); ok {
back.OnTapped = func() {
drv.(mobile.Driver).GoBack()
}
} else {
back.Disable()
}
w.SetContent(container.NewVBox(
hello,
back,
))
w.ShowAndRun()
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks. Well done. This looks great :)
nice, thank ... maybe call origin function OnResume(...) in go is useless but thank for this fix |
Still todo:
Fixes #2910
Checklist:
Current usage: