从最近启动应用程序时重定向到特定 Activity
Redirect to particular Activity when launching app from recents
假设您在 Activity A 中并按下主页按钮,您的应用现在将转到后台。现在长按你的主页按钮,你可以看到最近的应用程序。如果我点击我的应用程序,它应该是 activity 而不是 Activity A.
我认为这是一个奇怪的要求,但你可以这样做:
@Override
public void onResume() {
startActivity(new Intent(this, TargetActivity.class));
}
onResume() 在您的应用处于状态 'running'
之前被调用
参考此 post - How can I detect user pressing HOME key in my activity? 已实现以下逻辑。
使用上面的 post 检测主页按钮按下并在首选项中存储一个标志。当通过第一个 OnRestart() 方法从最近的列表启动应用程序时,将被触发,因此检查主页按钮按下标志并启动特定的 activity.
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
if (isApplicationSentToBackground(this)) {
// Do what you want to do on detecting Home Key being Pressed
preferences.setHomeButtonPressed(true);
}
}
@Override
protected void onRestart() {
super.onRestart();
// Do what you want to do on detecting app launching from recents section
if (preferences.isHomeButtonPressed()) {
Intent i = new Intent(this,
ParticularActivity.class);
startActivity(i);
preferences.setHomeButtonPressed(false);
}
}
public static boolean isApplicationSentToBackground(final Context context) {
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> tasks = am.getRunningTasks(1);
if (!tasks.isEmpty()) {
ComponentName topActivity = tasks.get(0).topActivity;
if (!topActivity.getPackageName().equals(context.getPackageName())) {
return true;
}
}
return false;
}
假设您在 Activity A 中并按下主页按钮,您的应用现在将转到后台。现在长按你的主页按钮,你可以看到最近的应用程序。如果我点击我的应用程序,它应该是 activity 而不是 Activity A.
我认为这是一个奇怪的要求,但你可以这样做:
@Override
public void onResume() {
startActivity(new Intent(this, TargetActivity.class));
}
onResume() 在您的应用处于状态 'running'
之前被调用参考此 post - How can I detect user pressing HOME key in my activity? 已实现以下逻辑。
使用上面的 post 检测主页按钮按下并在首选项中存储一个标志。当通过第一个 OnRestart() 方法从最近的列表启动应用程序时,将被触发,因此检查主页按钮按下标志并启动特定的 activity.
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
if (isApplicationSentToBackground(this)) {
// Do what you want to do on detecting Home Key being Pressed
preferences.setHomeButtonPressed(true);
}
}
@Override
protected void onRestart() {
super.onRestart();
// Do what you want to do on detecting app launching from recents section
if (preferences.isHomeButtonPressed()) {
Intent i = new Intent(this,
ParticularActivity.class);
startActivity(i);
preferences.setHomeButtonPressed(false);
}
}
public static boolean isApplicationSentToBackground(final Context context) {
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> tasks = am.getRunningTasks(1);
if (!tasks.isEmpty()) {
ComponentName topActivity = tasks.get(0).topActivity;
if (!topActivity.getPackageName().equals(context.getPackageName())) {
return true;
}
}
return false;
}