如何使用 Android fragmentmanager 传递变量
How to pass variables with Android fragmentmanager
我有以下简单代码可以在内容框架中从一个片段切换到另一个片段。有没有简单的方法在下面的代码中传递变量?
FragmentManager fm = getActivity().getFragmentManager();
fm.beginTransaction().replace(R.id.content_frame, new TransactionDetailsFragment()).commit();
如何为 TransactionDetailsFragment
创建参数化构造函数?
fm.beginTransaction().replace(R.id.content_frame, new TransactionDetailsFragment(YOUR_PARAMS)).commit();
当您创建 new TransactionDetailsFragment(YOUR_PARAMS)
作为 FragmentTransaction
的参数时,我认为使用构造函数是一个不错的选择。
您可以使用捆绑包:
FragmentManager fm = getActivity().getFragmentManager();
Bundle arguments = new Bundle();
arguments.putInt("VALUE1", 0);
arguments.putInt("VALUE2", 100);
MyFragment myFragment = new Fragment();
fragment.setArguments(arguments);
fm.beginTransaction().replace(R.id.content_frame, myFragment).commit();
然后,您检索如下:
public class MyFragment extends Fragment {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle bundle = this.getArguments();
if (bundle != null) {
int value1 = bundle.getInt("VALUE1", -1);
int value2 = bundle.getInt("VALUE2", -1);
}
}
}
或者您可以使用 newInstance 方法 - 在您的 Fragment class 中创建一个方法,例如:
public static TransactionDetailsFragment newInstance(String param) {
TransactionDetailsFragment frag = new TransactionDetailsFragment();
Bundle bund = new Bundle();
bund.putString("paramkey", param); // you use key to later grab the value
frag.setArguments(bund);
return frag;
}
因此,要创建您的片段,您需要:
TransactionDetailsFragment.newInstance("PASSING VALUE");
(这是用来代替你的 new TransactionDetailsFragment()
)
然后例如在同一片段的 onCreate/onCreateView 中,您将获得如下值:
String value = getArguments().getString("paramkey");
我有以下简单代码可以在内容框架中从一个片段切换到另一个片段。有没有简单的方法在下面的代码中传递变量?
FragmentManager fm = getActivity().getFragmentManager();
fm.beginTransaction().replace(R.id.content_frame, new TransactionDetailsFragment()).commit();
如何为 TransactionDetailsFragment
创建参数化构造函数?
fm.beginTransaction().replace(R.id.content_frame, new TransactionDetailsFragment(YOUR_PARAMS)).commit();
当您创建 new TransactionDetailsFragment(YOUR_PARAMS)
作为 FragmentTransaction
的参数时,我认为使用构造函数是一个不错的选择。
您可以使用捆绑包:
FragmentManager fm = getActivity().getFragmentManager();
Bundle arguments = new Bundle();
arguments.putInt("VALUE1", 0);
arguments.putInt("VALUE2", 100);
MyFragment myFragment = new Fragment();
fragment.setArguments(arguments);
fm.beginTransaction().replace(R.id.content_frame, myFragment).commit();
然后,您检索如下:
public class MyFragment extends Fragment {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle bundle = this.getArguments();
if (bundle != null) {
int value1 = bundle.getInt("VALUE1", -1);
int value2 = bundle.getInt("VALUE2", -1);
}
}
}
或者您可以使用 newInstance 方法 - 在您的 Fragment class 中创建一个方法,例如:
public static TransactionDetailsFragment newInstance(String param) {
TransactionDetailsFragment frag = new TransactionDetailsFragment();
Bundle bund = new Bundle();
bund.putString("paramkey", param); // you use key to later grab the value
frag.setArguments(bund);
return frag;
}
因此,要创建您的片段,您需要:
TransactionDetailsFragment.newInstance("PASSING VALUE");
(这是用来代替你的 new TransactionDetailsFragment()
)
然后例如在同一片段的 onCreate/onCreateView 中,您将获得如下值:
String value = getArguments().getString("paramkey");