JDS JDS - 14 days ago 5
Android Question

Android intent not starting new activity?

I'm trying to do something rather simple here, just launch a new activity from my main one. Here's the code:

public class mainActivity extends Activity {

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);


Intent i = new Intent(mainActivity.this, testActivity.class);
startService(i);
}


}


///////////////// next file /////////////////


public class testActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);

Toast.makeText(this, "testActivity started", Toast.LENGTH_SHORT).show();

}


}


///////////////// manifest section ///////////////////


<application android:icon="@drawable/icon" android:label="@string/app_name">
<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=".testActivity" />


But I never see the Toast from testActivity - what gives?

Answer

You want to use startActivity instead of startService

Intent i = new Intent(mainActivity.this, testActivity.class);  
startActivity(i);
Comments