Referencing new Activity in Manifest

From what I understood watching the Android tutorial, to reference a new class you need to reference it in the manifest.

Say, I had a class AndroidX and I need to reference it. What I have to do is;

startActivity(new Intent("thepackage.RANDOMNAME")) ;

and reference it android:name="thepackage.RANDOMNAME"

I’d think “RANDOMNAME” would be a new folder, but it isn’t and I have no idea how it is referenced.

I haven’t even seen a file or folder.

What’s happening?

Answer

<application android:label="@string/app_name" android:icon="@drawable/icon">
    <activity android:name=".MainActivity"
              android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity android:name=".AndroidX"></activity>

And to call it with an intent, you’d need:

startActivity(new Intent(getApplicationContext(), AndroidX.class");

Most people call it like:

Intent i = new Intent(this, AndroidX.class);
startActivity(i);

I don’t know if it’d be a problem the other way or not, but I think this is more readable. Feel free to ask anything you need in the comments.

Leave a Reply

Your email address will not be published. Required fields are marked *