android之onNewIntent()用法

2023-05-16

onNewIntent()用法

知识点:

1、intent的显示和隐式启动;

2、activity对intent的action的相应;

3、onNewIntent()和singleTask(栈唯一模式)的结合使用;

4、使用onNewIntent()需要注意坑;


在activity中,其实还有一个方法onNewIntent(),可以重写,这个方法在结合启动模式,有了很大的用处。关于启动模式,请看这里:点击打开链接

1、其他应用发Intent,执行下列方法:

Uri uri = Uri.parse("philn://blog.163.com");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);   
亦可:intent.setAction(Intent.ACTION_VIEW);
然后:startActivity(intent);

2、接收Intent声明:

<activity android:name=".IntentActivity" android:launchMode="singleTask"
                  android:label="@string/testname">
             <intent-filter>
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />
            </intent-filter>
</activity>

3、我们这里结合启动模式的singleTask模式来讲。singleTask模式:就是栈唯一原则。即是在同一个应用程序中,launchmode设置为singleTask的activity,在这个应用程序中,有且只有一个实例。

设置栈唯一原则设置。我们需要通过在AndroidManifest.xml配置activity的加载方式(launchMode)来设置栈唯一原则模式,如下所示:

<activity android:label="@string/app_name" android:launchmode=“singleTask"android:name="Activity1"></activity>

所以,我们可以知道:如果之前创建了目标Activity的实例,此时这个实例处于onPause、onStop 状态的话,再接收到intent的话,她的执行顺序为:

onNewIntent,onRestart,onStart,onResume。


activity栈唯一原则下,通过Intent启到一个Activity,如果系统已经存在一个实例,系统就会将请求发送到这个实例上,但这个时候,系统就不会再创建一个新的实例,不会调用onCreate方法,而是调用onNewIntent方法,如下所示:

protected void onNewIntent(Intent intent) {
  super.onNewIntent(intent);
  setIntent(intent);//must store the new intent unless getIntent() will return the old one
}

我们需要知道,在内存吃紧的情况下,系统可能会kill掉后台运行的 Activity ,如果不巧要启动的那个activity实例被系统kill了,那么系统就会调用 onCreate 方法,而不调用 onNewIntent 方法。这里有个解决方法就是在 onCreate onNewIntent 方法中调用同一个处理数据的方法,如下所示:

public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  getNewIntent();
}

protected void onNewIntent(Intent intent) {
  super.onNewIntent(intent);
   setIntent(intent);//must store the new intent unless getIntent() will return the old one
  getNewIntent();
}

private void getNewIntent(){
  Intent intent = getIntent(); //use the data received here
}

注意onNewIntent()中的陷阱:

有时候,我们在多次启动同一个栈唯一模式下的activity时,在onNewIntent()里面的getIntent()得到的intent感觉都是第一次的那个数据。对,这里就是这个陷阱。因为它就是会返回第一个intent的数据。就是这么坑。


原因就是我们没有在onNewIntent()里面设置setIntent(),将最新的intent设置给这个activity实例。

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    setIntent(intent);//设置新的intent
    int data = getIntent().getIntExtra("tanksu", 0);//此时的到的数据就是正确的了
}

在这里,如果你没有调用setIntent()方法的话,则getIntent()获取的intent都只会是最初那个intent(Note that getIntent() still returns the original Intent. You can use setIntent(Intent) to update it to this new Intent.)。





本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

android之onNewIntent()用法 的相关文章

  • SugarORM 错误或缺少数据库

    我尝试让 Android 项目与 SugarORM 一起使用 但是 我遇到了以下错误 引起原因 android database sqlite SQLiteException 没有这样的表 DOCUMENT 代码1 编译时 INSERT O
  • 蓝牙适配器.getDefaultAdapter();返回空值

    我开始开发一个应用程序通过蓝牙与arduino设备进行通信 我正在初始化 bt 适配器 BluetoothAdapter btAdapter BluetoothAdapter getDefaultAdapter 问题是 btAdapter
  • 带有垂直选项卡的 Android 布局 - 设计类似于 hello 短信应用程序

    Can somebody please help me out in designing vertical tabs like in the given pic taken from hello sms app 我不要求提供代码片段 所以请
  • 如何向 ListView 添加粘性标题?

    我有一个 listView 所以我想添加一个粘性标头 以便它粘在 listView 的顶部 当 listView 中开始不同的类别时 不同的标头会取代它 就像联系人一样 其中 a 为粘性标头位于顶部 直到 b 出现 有没有图书馆可以做 我正
  • 旋转对话框的自定义主题

    我想自定义当我单击 spinnermode dialog 中的微调器时出现的对话框的外观 我想使用对话框片段 但没有找到文档 是否可以 不会 外观是固定的 给人一种操作系统的感觉 您必须创建自己的微调器才能执行此操作 这并不容易 但如果微调
  • 字符串生成器最大长度

    我想知道字符串生成器 或字符串缓冲区 的最大容量是多少 我的 Android 应用程序中有一个静态变量 它应该保存日志字符串 它最多可以容纳 130 行和大约 10000 个字符 我如果我附加更多内容 但它只是不显示 没有错误 没有例外 所
  • 如何调试使用maven构建的android应用程序

    我目前正在尝试从 Eclipse 调试我的设备上的 Android 应用程序 设备已添加 我可以在控制台和 Eclipse 中看到它 控制台 Windows adb devices List of devices attached 0019
  • Eddystone Beacon 中广播的 MAC ID 会改变吗?

    我将描述我的设置 我制作了一个模拟 Eddystone 信标的 Android 应用程序 我能够使用 PlayStore 中的 Beacon Toy 应用程序检测手机上的 Eddystone 信标 但问题是 自上次检查以来 显示的 MAC
  • 如何对齐文本和图标可组合项,以便即使文本溢出后它们也能保持在一起?

    我有一个文本和一个图标可组合项 我希望图标粘在可组合项的右侧 这是我的代码 Row verticalAlignment Alignment CenterVertically horizontalArrangement Arrangement
  • 如何在谷歌地图中使用latlng字符串数组绘制多边形

    在我的应用程序中 我有包含 imagview 的 recyclerview 并且该 imageview 通过使用我存储在 sqlite 中的坐标包含静态地图图像 当我单击该图像时 我将该字符串数组格式的坐标传递给其他地图活动 然后使用该字符
  • 如何知道 TTS 何时完成?

    我正在 Android 上实现交互式语音响应应用程序 我想知道如何确定何时tts speak 函数已完成通话 因此我可以调用我的语音识别器函数 public class TTSActivity extends Activity implem
  • Android 时间选择器在分钟滚动时自动更改小时

    例如 当我在 TimePicker 上滚动分钟时 在将分钟滚动到 59 后 小时会自动滚动到 7 因此新时间将为 07 59 同样的方式 如果我有 07 59 并且我将分钟滚动到 00 小时将自动滚动到 8 所以时间将是 08 00 此逻辑
  • WebView 未绘制,WARN/webcore(5336):第一次布局后无法获取 viewWidth

    我的应用程序有一个视图 可以使用以下代码以编程方式添加到活动中 RelativeLayout LayoutParams layoutParams new RelativeLayout LayoutParams 480 75 Relative
  • 当我转到下一个活动并再次返回时,如何恢复活动的值?

    我希望当用户返回我的第一个活动时恢复编辑文本的值 请帮帮我 提前致谢 这是我的第一个活动代码 用于在编辑文本中获取用户值 public class IntentActivity extends Activity EditText ed1 e
  • 将 XML 从网站解析到 Android 设备

    我正在启动一个 Android 应用程序 它将解析来自网络的 XML 我创建了一些 Android 应用程序 但它们从未涉及解析 XML 我想知道是否有人对最佳方法有任何建议 这是一个例子 try URL url new URL your
  • 如何发现另一个应用程序的意图

    我正在尝试构建一个应用程序来接收来自 StumbleUpon 应用程序的共享 此时 我可以接收浏览器的 共享网址 但是当从 StumbleUpon 共享时 我的应用程序不会显示在列表中 我想我可能没有在清单中注册正确的意图 有什么方法可以找
  • 如何使用特定选项卡启动活动?

    我已经浏览了许多示例 问题和教程 但我从未见过使用特定选项卡启动活动 启动新意图 我知道可以使用 setCurrentTab切换到选项卡 但这只能从父活动选项卡内部完成 从另一个活动启动一个活动中包含的特定选项卡怎么样 是否可以 如果是这样
  • 找出该月第一个星期日/星期一等的日期

    我想在java中检测每个月第一周 第二周的第一个星期日 星期一的日期 我怎样才能实现它 我已经检查了 java 中的 Calendar 类和 Date 类 但无法找到解决方案 所以请帮助我解决这个问题 Calendar calendar C
  • Oreo:应用程序未运行时不会触发警报

    我有相对简单的设置 应该在一天中的特定时间触发警报并向用户显示通知 这是相关代码 设置闹钟 long inTime expirationTime Calendar getInstance getTimeInMillis 10000 Inte
  • 什么是 Android DecorView?

    http developer android com reference android view Window html getDecorView http developer android com reference android

随机推荐