Intent constructor syntax

2019-07-31 15:40发布

I am just starting to develop Android (being a .Net developer)

I am following the code from a book, and to start a new 'form' (screen) they show this code

Intent i = new Intent("net.learn2develop.ACTIVITY2");  

The class definition is this:

package net.learn2develop.Activities;

//imports removed

public class Activity2 extends Activity {

My question is: i presume that the string in the constructor in the Intent is the classname. But why is it 'net.learn2develop' and not 'net.learn2develop.Activities' and why is the classname all caps?

标签: android
2条回答
ゆ 、 Hurt°
2楼-- · 2019-07-31 15:45

Take a look at documentation: Intent(String action).

So this is actually action name, not class name. They usually look very similar to unprepared eye. Take a look at AndroidManifest.xml and search for net.learn2develop.ACTIVITY2 string. You should find somehting like:

   <activity android:name=".Activity2" >
        <intent-filter >
            <action android:name="android.intent.action.ACTIVITY2" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
查看更多
我欲成王,谁敢阻挡
3楼-- · 2019-07-31 15:46

No need to use the class name as caps. In fact use it exactly the way you have declared it . As android uses java as language and java is a case sensitive language.

For example to start the new acivity:

Intent i=new Intnet("net.learn2develop.Activity2"); //assuming that your package name is net.learn2develop
startActivity(i);

or

Intent i=new Intnet(Activity1.this,Activity2.class) //assuming that your current class name is Actvity1 
startActivity(i);

Finally dont forget to declare the new Activity in manifest file.

查看更多
登录 后发表回答