JDS
JDS

Reputation: 16968

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?

Upvotes: 2

Views: 9936

Answers (3)

vipw
vipw

Reputation: 7645

To start an activity you should use startActivity() instead of startService().

You'll also have to make sure the testActivity is listed in your android manifest.

Upvotes: 4

ccheneson
ccheneson

Reputation: 49410

You want to use startActivity instead of startService

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

Upvotes: 14

Nanne
Nanne

Reputation: 64399

If the activity is still running in the background, it gets called and only the onResume() gets called, not the onCreate();

check the lifecycle here: http://developer.android.com/guide/topics/fundamentals/activities.html#Lifecycle

Upvotes: 3

Related Questions