Android的"单顶"发射方式和onNewIntent方法 [英] Android "single top" launch mode and onNewIntent method

查看:131
本文介绍了Android的"单顶"发射方式和onNewIntent方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读了Android文档中,通过设置我的活动的launchMode属性singleTop或添加 FLAG_ACTIVITY_SINGLE_TOP 标志,以我的意图,即调用 startActivity (意向)将重用某个活动实例,并给我在 onNewIntent 回调的意图。我没有这两个东西, onNewIntent 从来没有火灾和的onCreate 触发每次。该文档还表示, this.getIntent()返回最先传递给活动是首次创建时的意图。在的onCreate 我打电话 getIntent ,我每一次(我创建的意图得到一个新的在另一个活动对象,并增加一个额外的给它......这额外应该每次都相同的,如果它被退回我有同样的意图对象)。所有这一切使我相信,我的行为是不是像个单顶,我不明白为什么。

要添加的情况下,一些背景,我只是缺少一个必要步骤,这是我的活动申报清单中的code我使用的启动活动。活动本身并不做任何事情值得一提的问候是:

在AndroidManifest.xml中:

 <活动
        机器人:ArtistActivityNAME =
        机器人:标签=艺术家
        机器人:launchMode =singleTop>
    < /活性GT;
 

在我的呼唤活动:

 意向书我=新意图();
    i.putExtra(EXTRA_KEY_ARTIST,ID);
    i.setClass(这一点,ArtistActivity.class);
    i.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
    startActivity(ⅰ);
 

解决方案

你检查的onDestroy()叫呢?这可能是为什么的onCreate()被调用每一个时间,而不是 onNewIntent(),这只会如果活动被称为已经存在的。

例如,如果您通过后退按钮它就会被默认情况下摧毁了留下你的活动。但是,如果你去了就活动堆栈到较高的其他活动,并从那里打电话给你的 ArtistActivity.class 再次将跳过的onCreate()键,直接进入 onNewIntent(),因为该活动已创建因为你把它定义为 singleTop Android将不会创建一个新的实例,但拿一个已经躺在附近。

我做什么,看看发生了什么事情我实施虚拟功能为每个活动的所有不同状态,所以我总是现在这是怎么回事:

  @覆盖
公共无效的onDestroy(){
    Log.i(TAG的onDestroy());
    super.onDestroy();
}
 

一样的 onRestart() ONSTART() onResume()的onPause()的onDestroy()

如果上面的(后退按钮)是不是你的问题,实施这些假人至少会帮你调试它会好一点。

I read in the Android documentation that by setting my Activity's launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would reuse a single Activity instance and give me the Intent in the onNewIntent callback. I did both of these things, and onNewIntent never fires and onCreate fires every time. The docs also say that this.getIntent() returns the intent that was first passed to the Activity when it was first created. In onCreate I'm calling getIntent and I'm getting a new one every time (I'm creating the intent object in another activity and adding an extra to it...this extra should be the same every time if it was returning me the same intent object). All this leads me to believe that my activity is not acting like a "single top", and I don't understand why.

To add some background in case I'm simply missing a required step, here's my Activity declaration in the manifest and the code I'm using to launch the activity. The Activity itself doesn't do anything worth mentioning in regards to this:

in AndroidManifest.xml:

    <activity
        android:name=".ArtistActivity"
        android:label="Artist"
        android:launchMode="singleTop">
    </activity>

in my calling Activity:

    	Intent i = new Intent();
    	i.putExtra(EXTRA_KEY_ARTIST, id);
    	i.setClass(this, ArtistActivity.class);
    	i.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
    	startActivity(i);

解决方案

Did you check if onDestroy() was called as well? That's probably why onCreate() gets invoked every time instead of onNewIntent(), which would only be called if the activity is already existing.

For example if you leave your activity via the BACK-button it gets destroyed by default. But if you go up higher on the activity stack into other activities and from there call your ArtistActivity.class again it will skip onCreate() and go directly to onNewIntent(), because the activity has already been created and since you defined it as singleTop Android won't create a new instance of it, but take the one that is already lying around.

What I do to see what's going on I implement dummy functions for all the different states of each activity so I always now what's going on:

@Override
public void onDestroy() {
    Log.i(TAG, "onDestroy()");
    super.onDestroy();
}

Same for onRestart(), onStart(), onResume(), onPause(), onDestroy()

If the above (BACK-button) wasn't your problem, implementing these dummies will at least help you debugging it a bit better.

这篇关于Android的&QUOT;单顶&QUOT;发射方式和onNewIntent方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆