【Android】 Activity启动模式singleTask的数据传输 onNewIntent方法

来源:互联网 发布:java中实现md5加密 编辑:程序博客网 时间:2024/06/09 23:39

很容易的就会有Activity启动模式(含四种启动模式、或者这样)的设置需求,比较简单的方式是:

在xml中设置activity的启动模式

<activity android:name="Act1" android:launchMode="singleTask"></activity>

这样会有一个问题,当Activity1已经创建时,在其他Activity中使用startActivity(intent)方式来回到Activity时,想在onResume中获取intent的Extras,发现并不能截取到数据,在网络上找到的解决办法是,在Activity中重写onNewIntent(Intent intent)方法,具体方法如下:

    @Override    protected void onNewIntent(Intent intent) {        super.onNewIntent(intent);        Bundle bundle = intent.getExtras();        if(bundle != null) DebugU.sop(bundle.getString(CData.GAME_IDS),"=-=-=-");        DebugU.sop("Activity onNewIntent");    }

(PS:如果你使用的是Fragment方式,需要多加一次处理啦,因为Fragment并没有这一方法)
启动方式:

    Intent intent = new Intent(ActivitySelectGame.this,ActivityMoments.class);    intent.putExtra(CData.GAME_IDS,"123456");    startActivity(intent);
0 0
原创粉丝点击