2016-07-30 28 views
1

我試圖將值作爲參數從一個片段發送到另一個使用bundle但捆綁不是不捆綁。我試圖獲得下一個片段的價值,但值是然後我調試,並得到該值不捆綁保存。附上截圖。Android bundle.putLong(「id」,id)不起作用

這裏是代碼

switch(v.getId()) 
    { 
     case R.id.store_gift_promo: 
     { 
      Fragment fragment = new GiftPromotion(); 
      Bundle bundle = new Bundle(); 
      bundle.putLong("id",id); 
      fragment.setArguments(bundle); 
      FragmentManager fragmentManager = ((FragmentActivity)mActivity).getSupportFragmentManager(); 
      FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 
      fragmentTransaction.replace(R.id.container_body, fragment); 
      fragmentTransaction.commit(); 
      break; 
     } 


    } 

請幫我解決這個問題。

enter image description here

+0

把你的代碼... – Intimate

+0

這很有趣。我能想到的唯一解釋是在第3行和第4行之間改變了'id'的值?涉及多線程或時間旅行的可能性? – Shaishav

+0

@Shaishav我不知道我也很驚訝! –

回答

0

嘗試在這樣的另一種方式:

GiftPromotion gp = GiftPromotion.newInstance(id); 
在GiftPromotion

:在檢索中的onCreate

public static GiftPromotion newInstance(long id) { 
    GiftPromotion f = new GiftPromotion(); 
    Bundle args = new Bundle(); 
    args.putLong("longValue", id); 
    f.setArguments(args); 
    return f; 
} 

片段:

long lId = getArguments().getLong("longValue", 0L); 
相關問題