Question


I'm reading an android guide and I've arrived on a point where it explains the tag and it says that typing <action android:name="android.intent.action.MAIN"/>

<category android:name="android.intent.category.LAUNCHER">

I make my activity be the main activity. My question is the following: is it possible to make the same thing in my source code just like in java typing something (public void main(...)) in such a way that my compiler understands that is my main activity? The second question is quite general, is it possible to make everything done in xml just using my source file?

Was it helpful?

Solution

Intent filters

Intent filters are used by the system to try to find installed application components that can receive an intent that the system is trying to resolve, i.e. when the Intent doesn't specify a particular component class to target. For example, if you did this...

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivity(intent);

The system has no explicit indication what Activity (or Service, or BroadcastReceiver) to start. It has to find some components that are capable of resolving the Intent, and it uses the declared <intent-filter>s to do this. Typically an app that is capable of capturing an image (like the Camera app) will declare their activity handles this action.

When you click the launcher icon of your app, the launcher basically creates an Intent like this:

Intent mainIntent = new Intent(Intent.ACTION_MAIN);
mainIntent.setCategory(Intent.CATEGORY_LAUNCHER);
mainIntent.setPackage(/* your package */);
startActivity(mainIntent);

The result is the system tries to find a component in that package with an <intent-filter> that specifies <action android:name="android.intent.action.MAIN" /> and <category android:name="android.intent.category.LAUNCHER">.

In short, it has absolutely nothing to do with the presence of the typical public static void main() in Java.

XML

When the system loads an XML file at runtime (like a layout file), it is actually going to create Java objects based on the information it finds by parsing the file. Using layouts as an example, all the different XML tags (e.g. <ImageView>) have corresponding java classes (android.widget.ImageView), and most of the properties you can set with XML attributes can also be set using the public methods on those Views.

If you were so inclined, you could build your Activity's layout entirely in Java by instantiating them yourself and using the set[Property]() methods, which is essentially what the system is doing for you:

public void onCreate(Bundle saved) {
    super.onCreate(saved);

    LinearLayout layout = new LinearLayout(this);
    layout.setPadding(20, 20, 20, 20);
    layout.setOrientation(LinearLayout.VERTICAL);

    TextView text = new TextView(this);
    text.setTextSize(18);
    text.setTextColor(Color.MAGENTA);
    text.setText(R.string.hello_world);

    layout.addView(text, LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
    setContentView(layout);
}

Similar things happen with XML drawables, although creating them in Java is quite a bit different.

In short, many of the things you can do in XML you can also do in Java, but there are downsides to it though (you lose the automatic resource handling that the OS provides, for instance).

OTHER TIPS

1、No

2、The Android.manitest File is a description file that tell the system what your application property. this can not be replaced by the Java code.

3、some resources files can be replaced by your Java code, but I think it's not a good idea to use the java code to replace the xml file. It's designed with MVC

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top