An introduction to the difference between Android explicitly and Activity implicitly

  • 2020-06-07 05:14:24
  • OfStack

Some time ago Determined to insist on writing blog, but found that their accumulation is really not much, so the vacation bubble bubble library, read some valuable articles. Harvest a lot, today's article to share the main, common learning.

Why write explicit startup versus implicit startup Activity? This comes from an interview in which I was asked by an Baidu engineer, but then I felt that My answer was not good enough, so I cut the crap and got to the point.

For example, there are roughly two ways to start Acitivity: explicitly and implicitly. The following are introduced respectively:

A: Start explicitly

For starters, this is the most common, and the following code explains what an explicit boot is.


Intent intent=new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);

B: Implicit startup

The difference with implicit startup is that we don't need to pass the parameters like Intent(ES21en.this, SecondActivity.class) and then Start another Activity. We need to add the filter intentfilter to intent.


<activity 
android:name="com.example.android.tst.SecondActivity" 
android:label="@string/title_activity_main" > 
<intent-filter> 
<action android:name="com.example.android.tst.SecondActivity"/> 
<category android:name="android.intent.category.DEFAULT" /> 
</intent-filter> 
</activity>

In this way, we only need to start another Activity using the following method:


Intent intent=new Intent("com.example.android.tst.SecondActivity");
startActivity(intent);

This will make one instance clearer. Because not used to webpage edit blog, and not familiar with the attachment upload, the project is not uploaded, more hands the most important!


Related articles: