gpt4 book ai didi

android - Activity 重新创建 Intent 附加项为空

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:04:12 29 4
gpt4 key购买 nike

我的应用程序包含一个 MainActivity 并使用全屏 fragment 来显示内容。我正在尝试在重新创建应用程序期间实现以下目标(当应用程序在后台运行了很长时间,被系统杀死,然后它被带到前台)

  1. 如果用户手动重新创建应用程序(通过从最近的应用程序列表中选择应用程序),则应重新创建主 Activity ,然后重新创建全屏的最后一个 fragment 。没问题,这是标准行为。

  2. 如果由于用户触摸推送通知而重新创建应用程序,主 Activity 应重新创建,但全屏模式下的最后一个 fragment 不应重新创建。 应该创建并显示一个新的 fragment 。为什么?推送通知包含有关应显示哪种 fragment 的信息

我的方法依赖于检查我在构建启动主 Activity 的通知时放置的额外 Intent ,为了简洁起见,这部分代码没有显示,但我总是在 Intent 中放置一些额外部分,总是

主 Activity .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());
...
}

现在,如果应用程序根本没有运行并且收到通知,我会得到以下输出:

MainActivity.onCreate() extras: Bundle[{android:viewHierarchyState=Bundle[mParcelledData....
MainActivity.onResume() extras: Bundle[{android:viewHierarchyState=Bundle[mParcelledData....

当用户手动重新创建应用程序时,我得到以下输出:

MainActivity.onCreate() extras: null
MainActivity.onResume() extras: null

当通过通知重新创建应用时

MainActivity.onCreate() extras: null
MainActivity.onResume() extras: Bundle[{android:viewHierarchyState=Bundle[mParcelledData....

这最后一个案例不是我所期望的,不知何故通知 Intent 附加功能仅在调用 super.onCreate() 后可用

关于如何实现 1. 和 2. 的任何想法?

巨型编辑:这是通知代码,如您所见,有一个将 Intent 传递给主 Activity 的启动画面 Activity

GcmIntentService.java

@Override protected void onHandleIntent(Intent intent) {
Bundle extras = intent.getExtras();
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
String messageType = gcm.getMessageType(intent);
if (!extras.isEmpty()) {
if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType)) {
sendNotification(extras.getString(KEY_CATEGORY), extras.getString(KEY_CONTENT));
}
}
GcmBroadcastReceiver.completeWakefulIntent(intent);
}

private void sendNotification(String category, String content) {
category = evaluateCategory(category);
mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
Intent mIntent = new Intent(this, SplashActivity.class);
mIntent.putExtra(KEY_CATEGORY, Integer.valueOf(category));
mIntent.putExtra(KEY_NEW, true);
mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent contentIntent = PendingIntent.getActivity(this, Integer.valueOf(category), mIntent, PendingIntent.FLAG_CANCEL_CURRENT);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.logo_actionbar)
//customize notification

mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(Integer.valueOf(category), mBuilder.build());

SplashActivity.java

@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent mIntent = getIntent();
if (mIntent.getExtras()!=null){
category = mIntent.getExtras().getInt(GcmIntentService.KEY_CATEGORY);
isNew = mIntent.getExtras().getBoolean(GcmIntentService.KEY_NEW);
}
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
Intent mainIntent = new Intent(SplashActivity.this, MainActivity.class);
mainIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP|Intent.FLAG_ACTIVITY_CLEAR_TOP);
if (category!=null ){
mainIntent.putExtra(GcmIntentService.KEY_CATEGORY, category);
mainIntent.putExtra(GcmIntentService.KEY_NEW, isNew);
}
startActivity(mainIntent);
overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
finish();
}
}, SPLASH_DELAY);
}

最佳答案

覆盖 Activity 提供的 onNewIntent(Intent intent) 方法怎么样?

  1. 读取 savedInstanceState 可以为 null,getIntent() 可以包含额外的 GCM。

  2. According to Android Developers:

protected void onNewIntent (Intent intent)

This is called for activities that set launchMode to "singleTop" intheir package, or if a client used the FLAG_ACTIVITY_SINGLE_TOP flagwhen calling startActivity(Intent). In either case, when the activityis re-launched while at the top of the activity stack instead of a newinstance of the activity being started, onNewIntent() will be calledon the existing instance with the Intent that was used to re-launchit.

An activity will always be paused before receiving a new intent, soyou can count on onResume() being called after this method.

Note that getIntent() still returns the original Intent. You can usesetIntent(Intent) to update it to this new Intent.

关于android - Activity 重新创建 Intent 附加项为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25516928/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com