Skip to content

No way to intercept the back button exiting the app #414

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

Closed
dzaima opened this issue Nov 29, 2017 · 4 comments
Closed

No way to intercept the back button exiting the app #414

dzaima opened this issue Nov 29, 2017 · 4 comments

Comments

@dzaima
Copy link

dzaima commented Nov 29, 2017

I can't seem to find a way to stop a Processing app from closing on clicking the back button - I'm trying to implement a page/menu system and the back button exiting no matter what is.. annoying.

In an older Processing version, there was onBackPressed, but in this commit, it was commented out. I've tried overriding onDestroy, keyPressed, keyReleased, stop on all the renderers with no success.

Is there a way to do this?

@codeanticode
Copy link
Contributor

codeanticode commented Jan 25, 2018

Thanks for this observation. The problem is with the transition to Fragment-based sketches, the onBackPressed() method could no longer be used, as it is not defined for Fragments. It could still be added in the main activity that contains the Processing sketch, but it is not immediately obvious what the best implementation could be. One possibility could be to use introspection to determine if the sketch has a function with some predetermined name, such as back(). If it does, call that function, otherwise continue with the default implementation of onBackPressed, so the activity quits:

public class MainActivity extends AppCompatActivity {
  ...
  @Override
  public void onBackPressed() {
    if (sketch != null) {
      // check if sketch has a method call back(), etc.
      return;
    }
    super.onBackPressed();
  }
}

@codeanticode
Copy link
Contributor

I think onBackPressed() could be added to ActivityAPI, which already contains a number of methods that are available in Activity and useful to implement in PApplet.

@codeanticode
Copy link
Contributor

Implemented with 0f54669

@dzaima
Copy link
Author

dzaima commented Jul 11, 2018

What's the proper way now to exit from the sketch if onBackPressed is overridden since exit() doesn't exist?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants