In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces the relevant knowledge of how to encapsulate Intent in Android, the content is detailed and easy to understand, the operation is simple and fast, and it has a certain reference value. I believe you will have something to gain after reading this Android article on how to package Intent. Let's take a look.
Detailed explanation of an example of Android Intent encapsulation
What is Intent:
Intent is to coordinate the communication and interaction between applications and components. You can launch Activity, Service, Broadcasts through Intent. It is also possible to call third-party components across programs. For example: start the dialing interface, music playback, etc.
Component starts ActivitystartActicity () ServicestartService (), bindService () BroadcastssendBroadcast ()
Use Intent:
Chestnut: add a click button to an Activity-> Click to start the second Activity (two parameters of type String are required)
Click the code for the first Activity button:
Public void onclick (View view) {Intent intent=new Intent (this,Main2Activity.class); intent.putExtra ("text1", "xiaoming"); intent.putExtra ("text2", "Chinese"); startActivity (intent);}
The second Activity accepts the Intent data code:
Protected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.activity_main2); ButterKnife.bind (this); initView ();} private void initView () {String text1 = getIntent (). GetStringExtra ("text1"); String text2 = getIntent (). GetStringExtra ("text2"); mTextView.setText (text1); mTextView2.setText (text2);}
This is a common use of Intent, but it has a big drawback. If you have more and more projects, you may cause the program to crash if you miss one parameter in starting the second Activity. So how to avoid this situation:
Parameters are required for the second Activity, so why not let him set the parameters. According to its specifications.
Give the instance of Intent to the second Activity. It needs the encapsulation of this method more. That is, the principle of opening and closing
Another way of thinking is code implementation:
Click the code for the first Activity button:
Public void onclick (View view) {/ / calls the second Activity encapsulated Intent Intent intent = Main2Activity.newIntent (MainActivity.this, "hsis", "wodhis"); startActivity (intent);}
Code for the second Activity:
Public class Main2Activity extends AppCompatActivity {private static final String INTENT_TEXT1= "intent_text1"; private static final String INTENT_TEXT2= "intent_text2"; private String text1,text2; @ butterknife.Bind (R.id.textView) TextView mTextView; @ butterknife.Bind (R.id.textView2) TextView mTextView2; @ Override protected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.activity_main2); ButterKnife.bind (this); initIntent (); initView () } public static Intent newIntent (Activity activity,String te, String te2) {Intent intent=new Intent (activity,Main2Activity.class); intent.putExtra (INTENT_TEXT1,te); intent.putExtra (INTENT_TEXT2,te2); return intent;} private void initIntent () {text1=getIntent (). GetStringExtra (INTENT_TEXT1); text2=getIntent (). GetStringExtra (INTENT_TEXT2);} private void initView () {mTextView.setText (text1); mTextView2.setText (text2) }} this is the end of the article on "how to encapsulate Intent in Android". Thank you for reading! I believe that everyone has a certain understanding of the knowledge of "how to encapsulate Intent in Android". If you want to learn more knowledge, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.