onNewIntent()的使用
来源:互联网 发布:网络分层结构五层 编辑:程序博客网 时间:2024/06/07 05:17
onNewIntent()的使用
1.用途:
当一个activity只在任务栈中存在一个时,比如app的首要一般只需要创建一次.也就是该activity的启动模式是singleTask.若果另外一个activity启动该activity(该activity已经存在)想给该activity传递数据,那么就用到了onNewIntent()方法.
2.直接上代码说明
两个activity分别是A和B互相切换,其中A的启动模式是singleTask,B默认.
布局就是每个activity都有一个按钮,不再贴出.
public class ActivityA extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_a); System.out.println("A执行了onCreate");}public void button(View view){ startActivity(new Intent(this,ActivityB.class));}@Overrideprotected void onNewIntent(Intent intent) { super.onNewIntent(intent); System.out.println("A执行了onNewIntent"); String test = intent.getStringExtra("test"); System.out.println("传过来的数据是:"+test);}@Overrideprotected void onDestroy() { super.onDestroy(); System.out.println("A执行了onDestroy");}@Overrideprotected void onStart() { super.onStart(); System.out.println("A执行了onStart");}@Overrideprotected void onResume() { super.onResume(); System.out.println("A执行了onResume");}@Overrideprotected void onStop() { super.onStop(); System.out.println("A执行了onStop");}@Overrideprotected void onRestart() { super.onRestart(); System.out.println("A执行了onRestart");}}
另一个:
public class ActivityB extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_b); System.out.println("创建了B");}public void button(View view){ Intent intent = new Intent(this,ActivityA.class); intent.putExtra("test","测试数据"); startActivity(intent);}@Overrideprotected void onDestroy() { super.onDestroy(); System.out.println("销毁了B");}}
说明:
程序一开始运行,日志打印:
02-22 17:08:22.417 2297-2297/com.dgtech.qidongmoshidemo I/System.out: A执行了onCreate02-22 17:08:22.417 2297-2297/com.dgtech.qidongmoshidemo I/System.out: A执行了onStart02-22 17:08:22.417 2297-2297/com.dgtech.qidongmoshidemo I/System.out: A执行了onResume
然后点击按钮进入B:
02-22 17:08:24.737 2297-2297/com.dgtech.qidongmoshidemo I/System.out: 创建了B02-22 17:08:25.187 2297-2297/com.dgtech.qidongmoshidemo I/System.out: A执行了onStop
然后B再点击按钮进入A:
02-22 17:08:30.727 2297-2297/com.dgtech.qidongmoshidemo I/System.out: A执行了onNewIntent02-22 17:08:30.727 2297-2297/com.dgtech.qidongmoshidemo I/System.out: 传过来的数据是:测试数据02-22 17:08:30.727 2297-2297/com.dgtech.qidongmoshidemo I/System.out: A执行了onRestart02-22 17:08:30.727 2297-2297/com.dgtech.qidongmoshidemo I/System.out: A执行了onStart02-22 17:08:30.727 2297-2297/com.dgtech.qidongmoshidemo I/System.out: A执行了onResume
从日志可以清楚的看到onNewIntent方法在什么时候执行.
注意:只有onNewIntent所属的activity已经创建才会执行onNewIntent.比如上面,只有A已经存在才会执行onNewIntent.
假如A在创建之后由于各种原因被回收,那么就不会执行onNewIntent,而是去执行onCreate方法.
0 0
- Android:onNewIntent的使用
- 关于onNewIntent的使用
- 关于onNewIntent的使用
- onnewintent的使用时机
- 关于onNewIntent的使用
- 关于onNewIntent的使用
- 关于onNewIntent的使用
- onNewIntent()的使用
- onNewIntent()的使用
- Activity生命周期 onNewIntent的使用
- Activity中onNewIntent的使用
- Activity生命周期 onNewIntent的使用
- onNewIntent使用遇到的坑
- 关于onNewIntent的使用&PendingIntent flag赋值
- Android:onNewIntent()的使用及注意事项
- Activity传值之onNewIntent的使用
- android onNewIntent使用详解
- Android onNewIntent()使用
- Linux 下安装 numpy 和 scipy
- java异常体系____异常处理机制
- fmdb的购物车
- Linux可执行文件的启动及命令行参数和环境变量的传递
- MySQL索引原理及慢查询优化
- onNewIntent()的使用
- [整理]C#反射(Reflection)详解
- According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by de
- 交换机开发(二)—— 三层交换机报文转发过程
- 洛谷 P1605 迷宫
- java再复习——注解的定义与使用
- mysql sql语句随记
- Java利用Jxl向数据库上传下载Excel文件
- ubuntu安装cuda循环登录