[英]SavedInstanceStateNull between Fragments is null
我有一個片段,顯示一個按鈕列表,每個按鈕都有自己的ID。 當用戶單擊該按鈕時,我試圖將當前片段替換為另一個片段。 在我替換片段之后,我正在向bundle添加參數。 從我從調試模式可以看出,該部分100%正常工作。 但是,當我們到達新片段時,“savedInstanceState”狀態將始終為null。
我已經閱讀了幾篇關於此的帖子,我感到困惑,因為我試圖設置捆綁並以與從活動轉到第一個片段時所做的相同的方式替換片段,但它仍然無法正常工作。
MyActivity.java(這個工作正在設置所有值):
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
MyFirstFragment fragment = new MyFirstFragment();
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.frame,fragment);
fragmentTransaction.commit();
Intent i = getIntent();
value = i.getExtras().getString("key");
Bundle bundle = new Bundle();
bundle.putString("key",value);
fragment.setArguments(bundle);
}
MyFirstFragment.java(也正在工作):
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_first, container, false);
final String value = getArguments().getString("key");
myButton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
String gid = Integer.toString(v.getId());
MySecondFragment secondfragment = new MySecondFragment();
Bundle secondFragmentBundle = new Bundle();
secondFragmentBundle.putString("key",value);
secondFragmentBundle.putString("id",id);
secondfragment.setArguments(secondFragmentBundle);
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.frame,secondfragment);
fragmentTransaction.commit();
}
});
MySecondFragment(為savedInstanceState和所有bundle agruments獲取null!):
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_second, container, false);
value = getArguments().getString("key");
id = getArguments().getString("id");
}
在將片段附加到活動后,您無法在片段上設置參數。
您的問題是由片段創建的混淆引起的。 您在問題中提供的內容不是將參數發送到另一個Fragment類型的正確方法。
在創建Fragment時,將參數傳遞給Fragment的事實上的標准方法是使用靜態工廠方法 ,通常稱為newInstance()。 您應該以這種方式設置參數,並且應該在提交FragmentTransaction之前這樣做 - 但是,從技術上講,您可以在提交事務后立即執行此操作而不會產生任何不良影響。 這是因為當提交片段時,它不會立即附加/創建,而是在主線程上排隊。
以下是您將如何做到這一點:
在Fragment類中編寫一個名為new instance的方法。 請確保為系統保留一個公共默認(無參數)構造函數來處理后台事物。
public MyFirstFragment extends Fragment {
public MyFirstFragment(){
//constructor - don't use this to create a new fragment directly
}
public static MyFirstFragment newInstance(String argument1, String argument2) {
// This is where you set the Fragment arguments:
MyFirstFragment instance = new MyFirstFragment();
Bundle arguments = new Bundle();
// add whatever arguments you need to the bundle
arguments.putString("ARGUMENT_1_KEY", argument1);
arguments.putString("ARGUMENT_2_KEY", argument2);
//Set the arguments on the new fragment instance
instance.setArguments(arguments);
return instance;
}
// recover the values in onCreate:
@Override
public void onCreate(Bundle savedInstanceState) {
// an easy way to check if this is a new instance is to null-check the saved state Bundle
if (savedInstanceState == null) {
String argument1 = getArguments().getString("ARGUMENT_1_KEY");
String argument2 = getArguments().getString("ARGUMENT_2_KEY");
} else {
// restore whatever you need from savedInstanceState bundle
}
}
關鍵點 - 參數傳遞給newInstance()方法(只要允許類型,這些方法可以是您想要的任何方法)。 newInstance()方法強制執行參數類型,並且還在片段已附加到活動后阻止設置它們。
在此之后,在Activity.onCreate()方法中:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String argument1 = "some information";
String argument2 = "other information";
MyFirstFragment fragment = MyFirstFragment.newInstance(argument1, argument2);
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.frame,fragment);
fragmentTransaction.commit();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.