I can not go to the next activity, using the intention

advertisements

I can't start a 'New Activity' when each item is clicked, I'm using a condition such as if-else,

if (item.equals("Aerosol")) {
  startActivity(new Intent(Search.this, Aerosol.class));
  startActivity(intent);

I use this there is nothing wrong but I can't go to the next class.


In order to start an intent, declare the Intent in the AndroidManifest as this:

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name">
    <activity
        android:name=".MainActivity"
        android:label="@string/title_activity_main" >
        <intent-filter>
            <action android:name="com.example.package.EXCLASS" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
</application

where .MainActivity is the name of the class, com.example.package is your package name, EXCLASS is whatever you would like it to be but remember what you named it.

For every new activity you would like to start, you should redeclare a new <activity> /> in the AndroidManifest, with the same pacakage name, a new word instead of EXCLASS, and the android:name=".MainActivity" should change to be an exact copy of the class name with a decimal placed in the front. This should be placed after the <application> /> declaration so it looks like this:

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <activity
        android:name=".StartingActivity"
        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=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="com.example.package.EXCLASS" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
</application>

In your class, MainActivity in my example, you would declare startActivity(new Intent("com.example.package.EXCLASS")); where com.example.package is your package name, and EXCLASS is whatever your declared it to be in the AndroidManifest.

The ending result would be:

private void select(String item) {
    try {
        if (item.equals("Aerosol")) {
            startActivity(new Intent("com.example.package.CLASSONE"));
        }
        if (item.equals("Aluminium")) {
            startActivity(new Intent("com.example.package.CLASSTWO"));
        }

Hope this helped!