2016-03-16 25 views
3

我:在我的片段類調用此發送數據片段與FragmentTransaction

@OnClick(R.id.blockedLinkLayout) 
public void onBlockedClick(){ 
    final FragmentTransaction ft = getFragmentManager().beginTransaction(); 
    ft.replace(R.id.content, new SettingsBlockedUsersFragment(), FRAGMENT_TAG); 
    ft.commit(); 
} 

而且它只是一個choosen取代我當前的片段。

而我的問題是,如何使用FragmentTransaction從我的父級片段向我的子片段發送一些數據(例如String值)?

回答

11

討論只是通過他們在一個捆綁的片段參數

在親本片段中:

SettingsBlockedUsersFragment fragment = new SettingsBlockedUsersFragment(); 
Bundle arguments = new Bundle(); 
arguments.putString(string_key , desired_string); 
fragment.setArguments(arguments); 
final FragmentTransaction ft = getFragmentManager().beginTransaction(); 
ft.replace(R.id.content, fragment , FRAGMENT_TAG); 
ft.commit(); 

兒童片段:

Bundle arguments = getArguments(); 
String desired_string = arguments.getString(string_key); 
+0

它的工作原理!如果我想發送例如HashMap oraz ArrayList? – y07k2

+0

你可以使用arguments.putStringArrayList(key,value); 。注意bundle僅限於像String或者int這樣的簡單值。 – Mahfa

+0

因此,我需要將Bundle類型更改爲什麼? – y07k2

1

FragmentTransaction只是轉換片段,它不會「傳遞」任何東西。您需要使用Bundle

SettingsBlockedUsersFragment frag = new SettingsBlockedUsersFragment(); 
Bundle b = new Bundle(); 
// put stuff into bundle... 
b.putString("user", "steve"); 

// Pass the bundle to the Fragment 
frag.setArguments(b); 

// Use Fragment Transaction 
final FragmentTransaction ft = getFragmentManager().beginTransaction(); 
ft.replace(R.id.content, frag, FRAGMENT_TAG); 
ft.commit(); 

然後將片段的OnCreate裏面,你可以做

String user = getArguments().getString("user"); 

其他方式將數據傳遞到一個片段在Best practice for instantiating a new Android Fragment

+0

我想你的意思是說'FragmentTransaction'不是'FragmentTransition' –

0

,你可以在你的片段類中使用工廠方法(如文檔推薦)

public static YourFragment newInstance(String sample) { 
    YourFragment fragment = new YourFragment(); 
    Bundle args = new Bundle(); 
    args.putString(KEY, sample); 
    fragment.setArguments(args); 
    return fragment; 
} 

然後調用YourFragment.newInstance(「字符串,你需要傳遞「);獲取片段的實例並在交易中使用它。

這裏的關鍵實際上是使用setArguments()方法來傳入數據。 然後你可以檢索這些數據在您的片段的onCreate方法這樣

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    if (getArguments() != null) { 
     streamSource = getArguments().getParcelable(KEY); 
    } 
} 
0

在你SettingsBlockedUsersFragment類有如下所示

public class SettingsBlockedUsersFragment extends Fragment { 
    private static final String MY_STRING = "my_string"; 


    public static SettingsBlockedUsersFragment newInstance(String yourStringValue){ 
     SettingsBlockedUsersFragment frag = new SettingsBlockedUsersFragment(); 
     Bundle args = new Bundle(); 
     args.putString(MY_STRING, yourStringValue); 
     frag.setArguments(args); 
     return frag; 
    } 

    @Override 
    protected void onCreate(Bundle savedInstanceState){ 
      Bundle args = getArguments(); 
      if(args != null){ 
       String myString = args.getString(MY_STRING); 
      } 
    } 
} 

在您的片段交易中的靜態方法,你可以通過這樣的ARGS

final FragmentTransaction ft = getFragmentManager().beginTransaction(); 
ft.replace(R.id.content,SettingsBlockedUsersFragment.newInstance("my string"), FRAGMENT_TAG); 
ft.commit(); 
相關問題