I want to know the default implementation of onBackPressed()
in Activity
. How to deal with the Activity
recover in the default implementation of onBackPressed()
?.
The following is the issues I suffer from. I have a test Activity
code like this:
public class MainActivity extends Activity {
public static boolean test = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
protected void onResume() {
super.onResume();
Toast.makeText(this,"is "+test,Toast.LENGTH_LONG).show();
test = !test;
}
}
When I first enter the app, I get 'is false'. Then I click back button and get to the home screen. After that, when I enter the app, I get the Toast 'is true'. I think the onBackPressed()
should kill my app when it gets back to the home screen, but It does not. This is my question.
If I override onBackPressed()
like this
@Override
public void onBackPressed() {
// super.onBackPressed();
finish();
try {
android.os.Process.killProcess(android.os.Process.myPid());
} catch (Exception e) {
e.printStackTrace();
}
}
I always get the Toast 'is false' after I enter the app.
Can anyone explain this problem and tell me what the default implementation of onBackPressed()
?
I'd like to know the flow process in onBackPressed()
in detail. I have read some of the source code on onBackPressed()
, but I couldn't understand it well.
Thanks in advance.