2013-08-26 19 views
2

目前我已經潛入片段世界:http://developer.android.com/guide/components/fragments.html#CommunicatingWithActivityAndroid的 - 從活動傳達給片段它承載

據我瞭解,通過創建片段的偵聽器,然後在其中承載片段是活動實現它從片段到活動的一種很好的方式,但是如何從活動返回到片段?另一位聽衆?也許我並不完全理解聽衆正在做什麼。任何有關這個主題的幫助解釋如何從活動到片段進行交流將不勝感激!

P.S.我目前正在將我製作的活動(B)轉換爲片段。在開始活動B之前,我使用活動A做了一些intent.putExtra(「value」),所以這就是我想要取代的東西......可能根本沒有幫助你,但我想我會試着把它以透視我在做什麼。

回答

1

我可能已經找到解決方案,哈哈。我會做一些檢查,以確保它能正常工作並在稍後確認。

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
    Bundle savedInstanceState) { 

     Bundle b = getActivity().getIntent().getExtras(); 
     wid = b.getString("wid"); 
     rid = b.getString("rid"); 
     View view = inflater.inflate(R.layout.categoryfragment, container, false); 
return view; 
} 
1

就像你做,當你創建一個活動,你可以傳遞一個BundleFragment

有一個關於如何在Fragment class reference上做到這一點的例子。

/** 
* Create a new instance of DetailsFragment, initialized to 
* show the text at 'index'. 
*/ 
public static DetailsFragment newInstance(int index) { 
    DetailsFragment f = new DetailsFragment(); 

    // Supply index input as an argument. 
    Bundle args = new Bundle(); 
    args.putInt("index", index); 
    f.setArguments(args); 

    return f; 
} 

使用得到Bundle回來。