android onIntent 是什么东西
2015-05-14 15:34
429 查看
在Android应用程序开发的时候,从一个Activity启动另一个Activity并传递一些数据到新的Activity上非常简单,但是当您需要让后台运行的Activity回到前台并传递一些数据可能就会存在一点点小问题。
首先,在默认情况下,当您通过Intent启到一个Activity的时候,就算已经存在一个相同的正在运行的Activity,系统都会创建一个新的Activity实例并显示出来。为了不让Activity实例化多次,我们需要通过在AndroidManifest.xml配置activity的加载方式(launchMode)以实现单任务模式,如下所示:
1 <activity android:label="@string/app_name" android:launchmode="singleTask"android:name="Activity1">
2 </activity>
launchMode为singleTask的时候,通过Intent启到一个Activity,如果系统已经存在一个实例,系统就会将请求发送到这个实例上,但这个时候,系统就不会再调用通常情况下我们处理请求数据的onCreate方法,而是调用onNewIntent方法,如下所示:
1 protected void onNewIntent(Intent intent) {
2 super.onNewIntent(intent);
3 setIntent(intent);//must store the new intent unless getIntent() will return the old one
4 processExtraData();
5 }
不要忘记,系统可能会随时杀掉后台运行的Activity,如果这一切发生,那么系统就会调用onCreate方法,而不调用onNewIntent方法,一个好的解决方法就是在onCreate和onNewIntent方法中调用同一个处理数据的方法,如下所示:
01 public void onCreate(Bundle savedInstanceState) {
02 super.onCreate(savedInstanceState);
03 setContentView(R.layout.main);
04 processExtraData();
05 }
06
07 protected void onNewIntent(Intent intent) {
08 super.onNewIntent(intent);
09 setIntent(intent);//must store the new intent unless getIntent() will return the old one
10 processExtraData()
11 }
12
13 private void processExtraData(){
14 Intent intent = getIntent();
15 //use the data received here
16 }
转:http://blog.csdn.net/tyj1982/article/details/6848879
首先,在默认情况下,当您通过Intent启到一个Activity的时候,就算已经存在一个相同的正在运行的Activity,系统都会创建一个新的Activity实例并显示出来。为了不让Activity实例化多次,我们需要通过在AndroidManifest.xml配置activity的加载方式(launchMode)以实现单任务模式,如下所示:
1 <activity android:label="@string/app_name" android:launchmode="singleTask"android:name="Activity1">
2 </activity>
launchMode为singleTask的时候,通过Intent启到一个Activity,如果系统已经存在一个实例,系统就会将请求发送到这个实例上,但这个时候,系统就不会再调用通常情况下我们处理请求数据的onCreate方法,而是调用onNewIntent方法,如下所示:
1 protected void onNewIntent(Intent intent) {
2 super.onNewIntent(intent);
3 setIntent(intent);//must store the new intent unless getIntent() will return the old one
4 processExtraData();
5 }
不要忘记,系统可能会随时杀掉后台运行的Activity,如果这一切发生,那么系统就会调用onCreate方法,而不调用onNewIntent方法,一个好的解决方法就是在onCreate和onNewIntent方法中调用同一个处理数据的方法,如下所示:
01 public void onCreate(Bundle savedInstanceState) {
02 super.onCreate(savedInstanceState);
03 setContentView(R.layout.main);
04 processExtraData();
05 }
06
07 protected void onNewIntent(Intent intent) {
08 super.onNewIntent(intent);
09 setIntent(intent);//must store the new intent unless getIntent() will return the old one
10 processExtraData()
11 }
12
13 private void processExtraData(){
14 Intent intent = getIntent();
15 //use the data received here
16 }
转:http://blog.csdn.net/tyj1982/article/details/6848879
相关文章推荐
- 合生元到底是什么东西?
- WebServices中的SOAP究竟是什么东西?与http,xml,webservices的本质关系是什么?
- 三四、vue中slot是什么东西?
- @property里能写的东西有什么用
- property中的getter=isXXXX 是什么东西
- [华硕UL] 求助关于AI RECOVERY RECOVERY, 求助 每次开机都会提示有AI RECOVERY REMIND 让做备份,不知道这个有什么用,是备份系统的隐藏分区还是别的东西
- Caffe到底训练出了个什么东西:caffemodel解析
- uDig是个什么东西
- 有关MAC FIR v5.1的一点东西,也许除了我没有人能看懂写得是什么……
- XML是什么东西
- 什么东西都很容易忘记,大杂烩开始了——也许都不是原创,但真的需要加些东西帮助记忆了
- JavaScript的解析器是个什么东西
- [扫盲] XAMPP是什么东西?
- 哈希值 是什么?哈希值是什么东西啊?具体怎么识别?怎么用?
- 一句话:afx是什么东西?
- 世界上好玩的东西很多,不是什么东西都可以弄精通 的,但吃饭的家伙还真得精通。厨师不能靠意念炒菜、司机不能靠意念开车,你也不能靠意念写程序,人不能活 在想象之中,电脑帮我们写程序的科幻时代还没到来。学不好,是你没刻苦努力,与你的才智无关。
- 淘宝上买了一件东西发生了什么
- 乱七八糟的什么都有,初学时的一点笔记类的东西
- Android 7.0来的真快,来看看又来了些什么出人意料的东西(墙外搬运)
- 由alert出来的对象是[object object],就感觉不知所措,而进一步思考,对象究竟是什么?即变成语言(如js、java、c等任意语言)中的变量,究竟是什么东西?