Make a method similar to pressing the return key on QQ and does not destroy the Activity (that is, do not call the Activity. finish (), the system does not call onDestroy), but is similar to pressing the Home Key to make the Activity similar to "paused" (that is, only onPause and onDestroy are called ).
The Code is as follows:
[Java] public boolean onKeyDown (int keyCode, KeyEvent event ){
If (keyCode = KeyEvent. KEYCODE_BACK ){
Intent intent = new Intent (Intent. ACTION_MAIN );
Intent. setFlags (Intent. FLAG_ACTIVITY_NEW_TASK); // note
Intent. addCategory (Intent. CATEGORY_HOME );
This. startActivity (intent );
Return true;
}
Return super. onKeyDown (keyCode, event );
}
Public boolean onKeyDown (int keyCode, KeyEvent event ){
If (keyCode = KeyEvent. KEYCODE_BACK ){
Intent intent = new Intent (Intent. ACTION_MAIN );
Intent. setFlags (Intent. FLAG_ACTIVITY_NEW_TASK); // note
Intent. addCategory (Intent. CATEGORY_HOME );
This. startActivity (intent );
Return true;
}
Return super. onKeyDown (keyCode, event );
}
Pay special attention to intent. setFlags (Intent. FLAG_ACTIVITY_NEW_TASK); this sentence must be added because the launchMode of the Activity is executed in standard by default, if this tag is not added, a new Activity is created and placed in the same Task as the current Activity. The FLAG_ACTIVITY_NEW_TASK description FLAG_ACTIVITY_NEW_TASK mark is as follows:
When the Intent object passed to startActivity () contains the FLAG_ACTIVITY_NEW_TASK tag, the system searches for different tasks for the activity to be started. If the affinity attribute of the activity to be started is different from that of all current tasks, the system creates a new task with the affinity attribute, and press the activity to be started into the new task stack; otherwise, press the activity into the stack with the same affinity attribute.
From Peking University-Google Android lab