简体   繁体   English

不是从意图开始活动

[英]Start an Activity not from an Intent

How can I start an Activity without using an Intent? 如何在不使用Intent的情况下启动活动? The only rule I have got is 我唯一的规则是

if( var == true ) startActivity();

but startActivity(); 但是startActivity(); needs an Intent as a parameter. 需要一个Intent作为参数。

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 . 这是使用Intent导航到第二个Activity (另一页)的方法。

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 . 另外,不要忘记为每个Activity调整AndroidManifest.xml

<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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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