Can I override Android methods like onPause(), onResume() in Processing?

edited March 2017 in Android Mode

Hi! I want to know that can I override Android methods like onPause(), onResume() in Processing? I also want to override "back" key. Because by default if I press back key it closes the sketch. I dont want that to happend. Thanks in advance :)

Answers

  • of course you can & in some cases you must! (services, video,& so on)- as for the back button you cannot decide:: that is the user right to close your app you are on a phone

  • thanks akenator for your response. can you give me an example because i have searched over internet but couldnt find any working example of android functions in processing. i really need it for my game. and as far as back button is concerned i want to override it as well because processing sketch closes when you press back soft key.

  • overriding on pause() or on resume() is useful when you want to release some service, camera, player, or (onResume()) when you want to reinitialize the same, or when you want to save some data. I ame not sure that it is what you want; if you want (i presume???) to prevent the user from "killing" your app when back button is pressed you have to import the android keyEvent (import android.view.KeyEvent) and write some code using the event KeyEvent.KEYCODE_BACK. There is also a backPressed android method but as for me it does not work.

  • yes you got my point. thats exactly what i want. can u tell me that do i have to use same syntax as in android eclipse? or is it change for processing?

  • @khubaib: what do you mean? Using eclipse+processing or using eclipse for android genuine code? - As for your problem i think that it is very similar except that some kind of keyevents could exist for android and not implemented for processing, i dont know; but on keyDown, keyPressed, surfaceKeyDown and so on are very near. So you create your boolean method listening for one of them, then you look at the keyCode and if event.getKeyCode == .keyEvent.KEYCODE_BACK you do nothing or only toggle some boolean to verify...

  • Answer ✓

    looking here:: https://code.google.com/p/processing/source/browse/trunk/processing/android/core/src/processing/core/PApplet.java - line968 - i found the solution. Here the code:

    import android.view.KeyEvent;
    
    void setup() {
    
    orientation (PORTRAIT);
    
    }
    
    void draw() {
    background(0);
    fill (255);
    textSize(24);
    text("backButton magic", 100,200);
    
    }
    
    boolean surfaceKeyDown(int code, KeyEvent event) {
    ////  System.out.println("got onKeyDown for " + code + " " + event);//try to uncomment //to see the keycode
      if (event.getKeyCode() == 4) {
     System.out.println("nothing must happen");
    }
    return false;
    }
    
    boolean surfaceKeyUp(int code, KeyEvent event) {
      return true;
    
    }
    
  • thanks a lot akenaton :)

  • PAkPAk
    edited March 2017

    Hello. When trying to use this code I get:

    public boolean surfaceKeyUp(int code, KeyEvent event) { ^^^^^^^ The return type is incompatible with PApplet.surfaceKeyUp(int, KeyEvent)

    How can I get through? thanks

  • Ok, I realized now it is a void function, however the code doesn't work.

    How could I solve this?

  • @PAk

    What are you trying to do?

    Kf

  • Trying to override that horrible situation, that when you press the back button, your application closes.

  • edited April 2017

    Check this: https://forum.processing.org/two/discussion/comment/94520/#Comment_94520

    Btw, start a new post and refer this post there... this is an old post and we don't want to bother the OP with new messages...

    Kf

Sign In or Register to comment.