简体   繁体   中英

Start an Activity not from an Intent

How can I start an Activity without using an Intent? The only rule I have got is

if( var == true ) startActivity();

but startActivity(); needs an Intent as a parameter.

Just create a new intent for the activity you want to start. depending on where you are you will need the app context thought.

Intent i = new Intent(getApplicationContext(), YourActivity.class);
startActivity(i);

Here's how to navigate to a second Activity (another page) using an Intent .

public void onClick(View v)
{
    Intent intent = new Intent(this, SecondActivity.class);
    startActivity(intent);
}

Also, don't forget to adjust the AndroidManifest.xml for each Activity .

<application android:label="@string/app_name" android:icon="@drawable/ic_launcher">
        <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="SecondActivity"
                  android:label="@string/second_label">
            <intent-filter>
                <action android:name="android.intent.action.SECOND" /> //should be namespace of your company I guess
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
    </application>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM