模拟活动内的主页按钮行为

时间:2014-01-22 11:47:47

标签: android background android-activity

我想模拟软Home按钮的行为。我希望当用户点击我的应用程序的特定活动中的后退按钮时,这将进入后台并显示设备主页。我该怎么办(我知道我应该覆盖活动中的onBackPressed())

2 个答案:

答案 0 :(得分:6)

如果您知道如何覆盖onBackPressed(),则覆盖此方法,并在此方法中执行以下操作: -

Intent startMain = new Intent(Intent.ACTION_MAIN);
startMain.addCategory(Intent.CATEGORY_HOME);
startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(startMain);

希望这有帮助。

答案 1 :(得分:0)

尝试以下代码,它将为您提供帮助

@Override
public void onBackPressed() {
super.onBackPressed();
Intent l = new Intent(Friend_list.this, HomeActivity.class);
startActivity(l);
}