Android - 将值从一个片段发送到另一个片段

时间:2017-05-15 09:13:04

标签: android android-fragments bundle fragmenttransaction fragmentmanager

我是Android上的初学者,我已经阅读了有关此问题的几个主题,但我无法在我的应用上使用此功能。

我有两种布局:

  1. activity_home(android:id =“@ + id / home_container”)
  2. activity_signs(android:id =“@ + id / signs_container”)。
  3. 我有两个活动:

    1. HomeActivity,没什么特别的:

      public class HomeActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_home); getSupportFragmentManager().beginTransaction() .replace(R.id.home_container, new HomeFragment()) .commit(); } }

    2. SignsActivity,我想在旋转智能手机时保存我的值:

      public class SignsActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_signs); if (savedInstanceState == null) { SignsFragment signsFragment = new SignsFragment(); signsFragment.setArguments(getIntent().getExtras()); getSupportFragmentManager().beginTransaction() .replace(R.id.signs_container, signsFragment, "SIGNS_FRAGMENT_TAG") .commit(); } else { SignsFragment signsFragment = (SignsFragment)getSupportFragmentManager() .findFragmentByTag("SIGNS_FRAGMENT_TAG"); } } }

    3. 我有两个碎片:

      1. HomeFragment,我想发送数据:

        public class HomeFragment extends Fragment { String language; public HomeFragment() { // Required empty public constructor } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { final View rootView = inflater.inflate(R.layout.activity_home, container, false); language = "french"; Button displayView = (Button) rootView.findViewById(R.id.display); if (displayView != null) { displayView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { EditText signView = (EditText) rootView.findViewById(R.id.sign_number); String signNumber = signView.getText().toString(); if (!signNumber.equals("")) { int number = Integer.parseInt(signNumber); Bundle bundle = new Bundle(); bundle.putString("language", language); bundle.putInt("signNumber", number); FragmentTransaction fragmentTransaction = getActivity().getSupportFragmentManager() .beginTransaction(); SignsFragment signsFragment = new SignsFragment(); signsFragment.setArguments(bundle); fragmentTransaction.replace(R.id.signs_container, signsFragment); fragmentTransaction.addToBackStack(null); fragmentTransaction.commit(); } } }); } return rootView; } }

      2. SignsFragment,我想要接收数据:

        public class SignsFragment extends Fragment { private String language; private int signNumber; private View rootView; public SignsFragment() { // Required empty public constructor } @Override public void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); outState.putString("language", language); outState.putInt("signNumber", signNumber); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { Bundle bundle = getArguments(); if (bundle != null) { language = bundle.getString("language"); signNumber = bundle.getInt("signNumber", 1); } else { if (savedInstanceState != null) { // Restore last state language = savedInstanceState.getString("language"); signNumber = savedInstanceState.getInt("signNumber"); } else { language = "french"; signNumber = 1; } } return inflater.inflate(R.layout.activity_signs, container, false); } }

      3. 当我点击主页上的按钮时应用程序关闭,而我想要检查标志中的数据。任何人都可以帮我纠正错误吗?

        提前感谢您的回答。

1 个答案:

答案 0 :(得分:0)

我有一个片段,其中有两个Tabs,它们也是片段。所以我从一个标签移动到另一个标签并传递数据。这是代码。这完美地工作

Private Sub ShortenRepetitiveCode()

    Dim Ws As Worksheet
    Dim LookupRange As Range
    Dim i As Integer

    Set Ws = ActiveSheet
    Set LookupRange = Worksheets(Ws.Cells(1, "R").Value & " Investments").Range("B58:F81")
    For i = 9 To 19 Step 2              ' specify columns I to S
        Ws.Cells(81, (i + 19)).Value = WorksheetFunction.VLookup(Ws.Cells(65, i).Value, LookupRange, 5, False)
    Next i
End Sub

//在First Fragment中编写以下代码

  // write below code in Second Fragment from where you want to pass the values to nextFragment(i-e FirstFragment)
 // Write below code in OnClickListener of the button in secondFragment
  if (tablayout.getTabAt(1).isSelected()) { // here one is the index number of second fragment i-e current fragment
  LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(getContext());
  Intent i = new Intent("EDIT_TAG_REFRESH");
  i.putExtra("Key1",value1);
  i.putExtra("Key2",value2);
  lbm.sendBroadcast(i);
   }
   tablayout.getTabAt(0).select(); // here 0 is the index number of first fragment i-e to whch fragment it has to move