[zz] onNewIntent

ref: http://my.oschina.net/xsjayz/blog/138447

一、onNewIntent()

IntentActivity中重写下列方法:onCreate onStart onRestart  onResume  onPause onStop onDestroy  onNewIntent


  

如果IntentActivity处于任务栈的顶端,也就是说之前打开过的Activity,现在处于onPause、onStop 状态的话,其他应用再发送Intent的话,执行顺序为:
onNewIntent,onRestart,onStart,onResume。

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" ></ activity >


launchModesingleTask的时候,通过Intent启到一个Activity,如果系统已经存在一个实例,系统就会将请求发送到这个实例上,但这个时候,系统就不会再调用通常情况下我们处理请求数据的onCreate方法,而是调用onNewIntent方法,如下所示:

?
1
2
3
4
5
6
7
8
9
protected void onNewIntent(Intent intent) {
 
   super .onNewIntent(intent);
 
   setIntent(intent); //must store the new intent unless getIntent() will return the old one
 
   processExtraData();
 
}

不要忘记,系统可能会随时杀掉后台运行的 Activity ,如果这一切发生,那么系统就会调用 onCreate 方法,而不调用 onNewIntent 方法,一个好的解决方法就是在 onCreate onNewIntent 方法中调用同一个处理数据的方法,如下所示:
?
1
2
3
4
5
6
7
8
9
public void onCreate(Bundle savedInstanceState) {
 
   super .onCreate(savedInstanceState);
 
   setContentView(R.layout.main);
 
   processExtraData();
 
}
?
1
2
3
4
5
6
7
8
9
protected void onNewIntent(Intent intent) {
 
   super .onNewIntent(intent);
 
    setIntent(intent); //must store the new intent unless getIntent() will return the old one
 
   processExtraData()
 
}
?
1
2
3
4
5
6
7
private void processExtraData(){
 
   Intent intent = getIntent();
 
   //use the data received here
 
}

 

二、onNewIntent()的setIntent()和getIntent()

?
1
2
3
4
5
6
7
8
9
@Override
protected void onNewIntent(Intent intent) {
     super .onNewIntent(intent);
 
     // setIntent(intent);
 
     int data = getIntent().getIntExtra( "HAHA" , 0 );
     // int data = intent.getIntExtra("HAHA", 0);
}

如果没有调用setIntent(intent),则getIntent()获取的数据将不是你所期望的。但是使用intent.getInXxx,貌似可以获得正确的结果。

注意这句话:
Note that getIntent() still returns the original Intent. You can use setIntent(Intent) to update it to this new Intent.

所以最好是调用setIntent(intent),这样在使用getIntent()的时候就不会有问题了。



=======================



普通的Activity之间的跳转,如:新打开一个Activity,此时的执行顺序是0nCreat-onStart-onResume..,此时使用Intent 传递数据没有问题,在下个Activity 中直接getIntent().

但是如果是从A-B-A,且A 设置为singleTask 模式,则在B-A 时,实际执行的是onNewIntent- onResart-onStart-onResume.此时从B传递过来的intent,需要在onNewIntent() 中赋值给A的intent, setIntent(B传过来的intent).这样你在getIntent() 时获取的才是B传回来的intent.

  当系统在低内存时,可能会杀掉A。此时,B-A 就是正常的0nCreat-onStart-onResume,此时的A的getIntent() 就是最新的从B传回来的,不经过onNewIntent().所以从B返回的数据操作不能只是放在onNewIntent中,放在onStart 之后会比较安全。当然你也可以在Oncreat()和onNewIntent() 中都做处理。




=======================


Activity的onNewIntent()方法何时会被调用

前提:ActivityA已经启动过,处于当前应用的Activity堆栈中;

当ActivityA的LaunchMode为SingleTop时,如果ActivityA在栈顶,且现在要再启动ActivityA,这时会调用onNewIntent()方法

当ActivityA的LaunchMode为SingleInstance,SingleTask时,如果已经ActivityA已经在堆栈中,那么此时会调用onNewIntent()方法

当ActivityA的LaunchMode为Standard时,由于每次启动ActivityA都是启动新的实例,和原来启动的没关系,所以不会调用原来ActivityA的onNewIntent方法


================================

http://blog.csdn.net/linghu_java/article/details/17266603




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Ace Stream中文版是新一代的创新多媒体平台,其中包括针对普通互联网用户以及多媒体市场专业人员的不同产品和解决方案。 Ace Stream平台为互联网在线广播/节目提供了出色的视听质量,是存储和传送音频和视频内容的最有效系统。Ace Stream用户观看的视频质量等级比YouTube视频质量高10倍(以1080p格式提供),比OTT提供的VoD和Live Stream所有其他现有服务的广播/节目多出许多倍(Over The Top)服务运营商,它们不使用P2P技术。使用单播的现有直播服务或标准CDN运营商可以为相同数量的观众提供具有相同高级质量的直播,而普通Ace Stream用户可以使用家庭互联网频道从他的个人电脑提供! Ace Player HD支持的格式也是非常的全面: P2P 文件 ( *.acelive *.acestream *.acemedia *.torrent *.tslive ) 媒体文件 ( *.3g2 *.3gp *.3gp2 *.3gpp *.amv *.asf *.avi *.bin *.px *.drc *.dv *f4v *.flv *.gxf *.iso *.m1v *.m2v *.m2t *.m2ts *.m4v *.mkv *.mov *.mp2 *.mp2v *.mp4 *.mp4v *.mpa *.mpe *.mpeg *.mpeg1 *.mpeg2 *.mpeg4 *.mpg *.mpv2 *.mts *.mtv *.mxf *.mxg *.nsv *.nuv *.ogg *.ogm *.ogv *.ogx *.ps *.rec *.rm *.rmvb *.tod *.ts *.tts *.vob *.vro *.webm *.wm *.wmv *.3ga *.669 *.a52 *.aac *.ac3 *.adt *.adts *.aif *.aifc *.aiff *.amr *.aob *.ape *.awb *.caf *.cda *.dts *.flac *.it *.m4a *.m4p *.mid *.mka *.mlp *.mod *.mp1 *.mp2 *.mp3 *.mpc *.mpga *.oga *.ogg *.oma *.opus *.qcp *.ra *.rmi *.s3m *.spx *.thd *.tta *.voc *.vqf *.w64 *.wav *.wma *.wv *.xa *.xm *.asx *.b4s *.cue *.ifo *.m3u *.m3u8 *.pls *.ram *.rar *.sdp *.vlc *.xspf *.wvx *.zip *.conf ) 视频文件 ( *.3g2 *.3gp *.3gp2 *.3gpp *.amv *.asf *.avi *.bin *.px *.drc *.dv *f4v *.flv *.gxf *.iso *.m1v *.m2v *.m2t *.m2ts *.m4v *.mkv *.mov *.mp2 *.mp2v *.mp4 *.mp4v *.mpa *.mpe *.mpeg *.mpeg1 *.mpeg2 *.mpeg4 *.mpg *.mpv2 *.mts *.mtv *.mxf *.mxg *.nsv *.nuv *.ogg *.ogm *.ogv *.ogx *.ps *.rec *.rm *.rmvb *.tod *.ts *.tts *.vob *.vro *.webm *.wm *.wmv ) 音频文件 ( *.3ga *.669 *.a52 *.aac *.ac3 *.adt *.adts *.aif *.aifc *.aiff *.amr *.aob *.ape *.awb *.caf *.cda *.dts *.flac *.it *.m4a *.m4p *.mid *.mka *.mlp *.mod *.mp1 *.mp2 *.mp3 *.mpc *.mpga *.oga *.ogg *.oma *.opus *.qcp *.ra *.rmi *.s3m *.spx *.thd *.tta *.voc *.vqf *.w64 *.wav *.wma *.wv *.xa *.xm ) 播放列表文件 ( *.asx *.b4s *.cue *.ifo *.m3u *.m3u8 *.pls *.ram *.rar *.sdp *.vlc *.xspf *.wvx *.zip *.conf )
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值