【发布时间】:2011-10-03 21:29:13
【问题描述】:
这看起来与my previous question 非常相似,因为它是某种跟进。我对给出的唯一解决方案不太满意;此外,解决方案是针对与此问题略有不同的问题。所以让我再试着解释一下这个问题......
- 在启动时会创建一个通知(带有
BroadcastReceiver)。 - 我的应用程序主 Activity 已打开并按下主页按钮(该 Activity 将被发送到后台堆栈)。
- 我拉下状态栏并按下之前在启动时创建的通知。
- 这将启动一些与主要活动不同的活动。
- 我按下后退按钮并显示主要活动。
这与我之前的问题没有太大不同......问题是,“主要活动”只是一个例子。我本可以打开应用程序主活动,然后通过菜单选项打开关于活动并按下主页按钮。后台堆栈现在将是 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标志感到困惑。无论是否设置了该标志,通知启动的活动都有效。我应该使用它还是不使用它? -
据我了解,应该始终为通知中的活动设置它。在您的情况下,您肯定希望设置它,因为您不希望通知活动成为正常任务堆栈的一部分。
-
这也是我所理解的,但我没有看到任何不同的行为......:/
-
这种方法对你有用吗? stackoverflow.com/questions/4628246/…
标签: android android-activity notifications android-intent back-button