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.
The default implementation of Activity
's onBackPressed()
probably won't tell you a lot about the actual Activity
/application lifetime. You should dig much dipper to understand the internal Android
(and Linux
) "mechanics" on application/process killing.
What an application developer should know is that once an Activity
is in background (Home button pressed, incoming call received etc., i.e. onPause()
followed by onStop()
have been invoked) its process may (similar to what you did with android.os.Process.killProcess(...)
) or may NOT be killed. See Multitasking the Android Way by Dianne Hackborn for the reference.
As to finishing an Activity
by pressing the back button, it does not mean its instance will be immediately killed and the memory garbage collected (see this answer). It just means a new instance of the Activity
will be created next time you navigate back to it.
Regarding your code and the statement that
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 the case when the system didn't kill the process while the Activity
were in background (again, it is not guaranteed). If it did, the Toast
would have shown false
.
In order to check that a new instance of MainActivity
is created each time you press the back button and then navigate back to the app, I don't recommend to use a static variable, - it appears to be not that obvious (see, for instance, is it possible for Android VM to garbage collect static variables... or Are static fields open for garbage collection?).
Besides you're simply switching between true
and false
that might be confusing. Instead of using a static variable you might use a non-static one incrementing it, for example, or toast the hash code of the current Activity
instance, like Toast.makeText(this,"is " + this.hashCode(), Toast.LENGTH_LONG).show()
. By doing this the Activity
lifecycle should act as per the documentation.
If I override onBackPressed() ... I always get the Toast 'is false' after I enter the app.
This is more or less similar to what if the system kills your app's process.