Get the App
SLTechnology News&Howtos  ›  Development  › 

How to realize the Communication between activity and fragment in Android

Shulou Source: shulou.com Published: 2022-05-31 23:51:55 09月21日 Update

This article mainly explains "how to realize the communication between activity and fragment in Android". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to realize the communication between activity and fragment in Android.

I. composition of APP

MainActivity+ bottom navigation bar

Second, the purpose of communication

The member method test () in MainActivity is judged by the test () method in fragment before the next operation can be performed, such as interface jump.

Example:

Public class MainActivity extends Activity {public boolean mBaiDuSDK_is_Ok = false; public boolean ismBaiDuSDK_is_Ok () {return mBaiDuSDK_is_Ok;} public void setmBaiDuSDK_is_Ok (boolean mBaiDuSDK_is_Ok) {this.mBaiDuSDK_is_Ok = mBaiDuSDK_is_Ok;}}

As in the above sample code, you need to judge the value of mBaiDuSDK_is_Ok in fragment and perform the interface jump.

III. Solutions

Public class Test1Fragment extends Fragment implements OnClickListener {private Activity mActivity; @ Override public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {mActivity = getActivity (); / / when the interface is created, define parent Activity view = inflater.inflate (R.layout.fragment_test1, container, false); return view } @ Override public void onViewCreated (View view, Bundle savedInstanceState) {/ / a test button in the fragment_test1 layout Button test_button= (Button) view.findViewById (R.id. Test_button); test_button setOnClickListener (this); / / set button listening event super.onViewCreated (view, savedInstanceState);} @ Override public void onClick (View view) {/ / TODO Auto-generated method stub switch (view.getId ()) {/ / Test button listening event case R.id.test_button: / / mActivity is the parent Activity Intent intent = new Intent (mActivity, SecondActivity.class) Log.i ("BAIDUSDK", "verify:" + ((MainActivity) mActivity) .ismBaiDuSDK_is_Ok ()); / / the most critical step is that fragment calls the member method of the parent activity to communicate with if (MainActivity) mActivity) .ismBaiDuSDK_is_Ok ()) {startActivity (intent) } else {Log.e ("TEST", "Please make sure mBaiDuSDK_is_Ok is normal!") ;} break;}} at this point, I believe you have a deeper understanding of "how to realize the communication between activity and fragment in Android". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: Communication method button interface event content member learning testing monitoring practical deeper code representative key interest such as above practical practical layout Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information OPPO Reno Microsoft Linux Shulou Technology