0

I'm making app that counts user's income and outcome. And recently I found out that if user has pressed home button from calculator(I use basic calculator, by android) than all data that he/she didn't save will be erased. How can I notify users about this so they could save all unsaved data? I want to show dialog when this happens but I don't know how to detect home button press. May be somebody had this issue too and could help me?

Thank you.

Steve
  • 81
  • 2
  • 10

3 Answers3

1

On press of home button the activity will call onPause() method you can override this method to write actions.Also you can use

public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
        super.onSaveInstanceState(outState, outPersistentState);
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
    }

to restore your values.

Jinesh Francis
  • 3,377
  • 3
  • 22
  • 37
  • Thank you but I've tried this method and it worked only if I was in the app not in the calculator – Steve Jun 08 '16 at 12:52
  • @Steve Wait, are you trying to save data from the Android calculator app? Not your app? – Da-Jin Jun 08 '16 at 13:02
  • @da-jin-c What I'm trying to do is : to show dialog if user pressed home button from calculator app which was launched from y app. – Steve Jun 08 '16 at 13:15
  • While it's technically possible to show a dialog when the user presses the home button from the calculator app, I do not think that will accomplish what you think it will. If I understand correctly you want to have them save data in the calculator? What if the calculator app they are using doesn't have that function? – Da-Jin Jun 08 '16 at 16:55
  • @da-jin-c Sorry for so late response. No, in my dialog user will see something like : Would you like to save all what you've done today?(in my app, not in the calculator) – Steve Jun 09 '16 at 05:08
  • @da-jin-c Can you please tell me about those possibilities? – Steve Jun 09 '16 at 14:48
  • Generally apps save automatically. Manual saving is very rare, but if your app really needs to let the user choose whether or not they want to save, I would recommend an auto-recovery type of system. Save their progress in a separate file whenever they leave the app, and then when they open the app again, restore the state to where they left off, but let them choose if they want to permanently "save" their progress – Da-Jin Jun 09 '16 at 19:13
  • If you really really want to create a dialog when they hit the home button, you could run a service in the background that checks for home button keydown event. Similar to http://stackoverflow.com/a/11030799/1224186 – Da-Jin Jun 09 '16 at 19:15
1

You can override onKeyDown as follows

public boolean onKeyDown( int keyCode, KeyEvent event) {


    if (keyCode == KeyEvent .KEYCODE_HOME) {
     //Dialog here to notify user
    return true ;
        }else{
    return super .onKeyDown(keyCode, event );
    }
    }
1

Try this.

@Override
    public void onAttachedToWindow() {
        super.onAttachedToWindow();
        this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);           
    }

@Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {     

        if(keyCode == KeyEvent.KEYCODE_HOME)
        {
           //YOUR CODE
        }
    });
jmarkstar
  • 1,335
  • 14
  • 21