这看起来与my previous question非常相似,因为它是某种跟进。我对所给出的唯一解决方案并不满意,此外,该解决方案与此问题稍有不同。因此,让我试着再次解释问题...通知打开活动,按回按钮,防止打开回栈活动?
- 通知在启动时创建(使用
BroadcastReceiver
)。 - 我的app主要活动被打开并且主页按钮被按下(活动将被发送到后退堆栈)。
- 我拉下状态栏并按下之前在启动时创建的通知。
- 这将开始一些活动,与主要活动不同。
- 我按下后退按钮,显示主要活动。
这与我以前的问题没什么不同......事情是,“主要活动”只是一个例子。我可以打开应用程序的主要活动,然后通过菜单选项打开有关活动并按下主页按钮。后端堆栈现在是MainActivity
»AboutActivity
。这意味着当在“某些活动”(通过按通知开始)中按下后退按钮时,我们将被带到返回栈的顶部,即关于活动。
基本上想要的是当我在“某些活动”(同样,通过按通知开始)时按下后退按钮时,防止打开任何其他活动。我希望被带到我所在的位置,这可能是桌面或其他应用程序的活动,但不是我的应用程序的MainActivity
和AboutAcitivity
,这不是我所在的位置,那些位于后台堆栈中,在后台“睡眠”。
我已经想出了一个解决方案,但我认为它不是很优雅,我正在寻找更多,更好,更优雅的东西......如果您有任何其他建议,请告诉我。
无论如何,这是我提出的解决方案:
// I use this class for public static (or public static final) members and
// methods
public final class AppHelper {
public static final String KEY_RESUME_FROM_NOTIFICATION = "resumeFromNotification";
private static boolean sResumeFromNotification = false;
public static boolean getResumeFromNotification() {
return sResumeFromNotification;
}
public static void setResumeFromNotification(boolean resumeFromNotification) {
sResumeFromNotification = resumeFromNotification;
}
}
public class MainActivity extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
(...)
}
@Override
protected void onResume() {
super.onResume();
if(AppHelper.getResumeFromNotification()) {
AppHelper.setResumeFromNotification(false);
moveTaskToBack(true);
}
}
}
public class AboutActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
(...)
}
@Override
protected void onResume() {
super.onResume();
if(AppHelper.getResumeFromNotification()) {
AppHelper.setResumeFromNotification(false);
moveTaskToBack(true);
}
}
}
public class SomeActivity extends Activity {
// This will be called when the notification is pressed and the activity is
// not opened yet
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
(...)
extractIntentExtras(intent);
}
// This will be called if the activity is already opened and the
// notification is pressed
@Override
protected void onNewIntent(Intent intent) {
extractIntentExtras(intent);
super.onNewIntent(intent);
}
private void extractIntentExtras(Intent intent) {
Bundle bundleExtras = intent.getExtras();
if(bundleExtras != null) {
// These intent extras are set on the Intent that starts this activity
// when the notification is pressed
AppHelper.setResumeFromNotification(bundleExtras.getBoolean(
AppHelper.KEY_RESUME_FROM_NOTIFICATION));
mRowId = bundleExtras.getLong(AgendaNotesAdapter.KEY_ROW_ID);
populateNoteUpdateFields();
}
}
}
我不知道,但这种方法看起来不是很优雅,我(但它的作品,因为我希望它),我期待作为替代方案或对我提议的解决方案提出强烈意见作为可接受的良好解决方案思考?
你有任何特殊的标志,你用于你的活动? singleTop/singleInstance或类似的东西?我没有看到您提到的活动,因为通知应始终在新任务堆栈中开始您的活动(它们需要FLAG_ACTIVITY_NEW_TASK标志)。 –
我在'MainActivity'(清单中)有'singleTop',没有别的。我一直对FLAG_ACTIVITY_NEW_TASK标志感到困惑。无论标志是否设置,通知开始的活动都可以工作。我应该使用它吗? –
从我所了解的情况来看,应该始终为通知中的活动设置。在你的情况下,你一定希望它被设置,因为你不希望你的通知活动成为你正常任务堆栈的一部分。 –