1.關(guān)于singleTask模式無法收到intent傳值問題的解決
原文? https://blog.csdn.net/harryweasley/article/details/46557827
感謝作者
如果將activity的launchMode設(shè)置為singleTask,會發(fā)現(xiàn)收不到intent傳遞過來的值趴俘,這時候就需要使用到onNewIntent方法,在里面接收intent。
launchMode為singleTask的時候班挖,通過Intent啟到一個Activity,如果系統(tǒng)已經(jīng)存在一個實例蛋辈,系統(tǒng)就會將請求發(fā)送到這個實例上避凝,但這個時候社痛,系統(tǒng)就不會再調(diào)用通常情況下我們處理請求數(shù)據(jù)的onCreate方法坠七,而是調(diào)用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 }
同時,系統(tǒng)可能會隨時殺掉后臺運行的activity彪置,那么系統(tǒng)就會調(diào)用onCreate方法离例,而不調(diào)用onNewIntent方法,因此需要在onCreate和onNewIntent方法中調(diào)用同一個處理數(shù)據(jù)的方法悉稠,如下所示:
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 }
2.Activity以singleTask模式啟動宫蛆,intent傳值的解決辦法
這樣在與它關(guān)聯(lián)的Fragment中,就可以調(diào)用
原文?https://blog.csdn.net/harryweasley/article/details/46557827
感謝作者
@Override
publicvoidonResume(){
super.onResume();
// 第一次進(jìn)入這個頁面的猛,下面的方法是不會執(zhí)行的耀盗,因為ringName是null
String ringName = getActivity().getIntent().getStringExtra("ringName");
if(ringName !=null) {
newSound.setText(ringName);
Log.e("tag", ringName +"要保存的值");
SharedPreferenceUtil.setString(getActivity(),
SharedPreferenceUtil.RINGTONE_NAME, ringName);
}
}
注意,這里Fragment調(diào)用的時候卦尊,一定要在onResume方法中叛拷。
https://blog.csdn.net/harryweasley/article/details/46557827