I tried the following code to restart my activity when the user attempts to launch the home screen by pressing the home button.
@Override
public void onSaveInstanceState(Bundle state){
try{
Intent i = new Intent(this, LockerAppActivity.class);
startActivity(i);
}catch(Exception ex){
Toast.makeText(this, ex.getMessage(), Toast.LENGTH_LONG);
}
}
There is no exception that occurs and the code fires. But the activity does not start. What am I doing wrong ?
Step #1: Delete your
onSaveInstanceState()implementation.Step #2: Do not attempt to “restart [your] activity when the user attempts to launch the home screen by pressing the home button”, because that is unnecessary.
Step #3: Fix whatever problems you think you are experiencing that are causing you to try this “restart” technique.
For example, given the class name
LockerAppActivity, I am guessing that you are trying to create one of those easily-defeated “lock screens”. Why you are bothering, considering anybody can get past them, is beyond me. Regardless, the proper way to ensure that your application runs when the user presses HOME is to be a home screen.