我的应用程序包含MainActivity并使用全屏片段来显示内容.我正在尝试在应用程序重新创建期间实现以下功能(当应用程序已经在后台运行了很长时间,被系统杀死然后它被带到了前台)
如果用户手动重新创建应用程序(通过从最近的应用程序列表中选择应用程序),则应重新创建主活动,然后重新创建全屏的最后一个片段.没问题,这是标准行为.
如果由于用户触摸推送通知而重新创建应用程序,则应重新创建主活动,但不应重新创建全屏中的最后一个片段.而是应该创建和显示新的片段.为什么?推送通知包含有关应显示哪种片段的信息
我的方法依赖于检查我在构建启动Main Activity的通知时所添加的意图额外内容,为了简洁起见,部分代码没有显示,但我总是在意图中加入一些额外内容,总是
MainActivity.java
@Override
protected void onCreate(Bundle savedInstanceState) {
//debug
System.out.println("MainActivity.onCreate() extras:"+getIntent().getExtras());
if (savedInstanceState!=null && getIntent().getExtras()!=null){
//case 2. passing null will prevent Fragment recreation
super.onCreate(null)
}
else{
//case 1.
super.onCreate(savedInstanceState)
}
...
}
@Override
public void onResume(){
//debug
System.out.println("MainActivity.onResume() extras:"+getIntent().getExtras());
...
}
Run Code Online (Sandbox Code Playgroud)
现在,如果应用程序根本没有运行并且通知到达,我会得到以下输出:
MainActivity.onCreate() extras: Bundle[{android:viewHierarchyState=Bundle[mParcelledData....
MainActivity.onResume() extras: Bundle[{android:viewHierarchyState=Bundle[mParcelledData....
Run Code Online (Sandbox Code Playgroud)
当用户手动重新创建应用程序时,我得到以下输出:
MainActivity.onCreate() extras: null
MainActivity.onResume() extras: null
Run Code Online (Sandbox Code Playgroud)
通过通知重新创建应用程序时
MainActivity.onCreate() extras: null
MainActivity.onResume() extras: Bundle[{android:viewHierarchyState=Bundle[mParcelledData....
Run Code Online (Sandbox Code Playgroud)
最后一种情况不是我所期望的,不知何故通知意图附加只有在调用super.onCreate()之后才可用
关于如何实现1.和2.的任何想法?
巨人编辑: 这是通知代码,如您所见,有一个启动屏幕活动将意图转发给主要活动
GcmIntentService.java
@Override protected void onHandleIntent(Intent intent) { …Run Code Online (Sandbox Code Playgroud) notifications android android-intent activity-lifecycle android-fragments
好的,在这里忍受我
我的应用程序由闪屏活动(A)和主要活动(B)组成.当应用程序启动时,(A)显示一点,然后启动(B).之后(A)结束.这在"正常"条件下工作正常.这是启动的代码(B)
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
Intent mainIntent = new Intent(A.this, B.class);
startActivity(mainIntent);
finish();
}
}, SPLASH_DELAY);
Run Code Online (Sandbox Code Playgroud)
当通知到达时,用户点击它.我是通过PendingIntent开始的(A):
Intent mIntent = new Intent(this, A.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, mIntent, PendingIntent.FLAG_CANCEL_CURRENT);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this).setSmallIcon()... //build the whole notification
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(0, mBuilder.build());
Run Code Online (Sandbox Code Playgroud)
这开始于(A)然后是(B)并且一切都很好.
然而...
一旦应用程序在屏幕上显示并且第二个通知到达(A)不再启动,也不会在(B)中收到任何回调
阅读http://developer.android.com/guide/components/tasks-and-back-stack.html#ActivityState上的文档我得出结论,我应该使用FLAG_ACTIVITY_NEW_TASK设置启动(A)以便它启动一个新任务只有在(A)尚未运行),我也应该开始(B)与标志FLAG_ACTIVITY_SINGLE_TOP(这样我就可以得到一个回调B.onNewIntent() ,因为B将被运行).所以我做了
...
mainIntent.setFlags(FLAG_ACTIVITY_NEW_TASK);
....
mIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
....
Run Code Online (Sandbox Code Playgroud)
但是,不,不.这种行为似乎根本没有改变.
我错过了文档中的内容吗?在我看来,每次在我的情况下FLAG_ACTIVITY_NEW_TASK应该开始(A),因为到第二个通知到达时.(A)已经完成,但它什么也没做.
你能给我一些关于如何获得回调的信息,这样我就能向用户显示正确的信息吗?
谢谢
android ×2