开发者

Android intent not starting new activity?

开发者 https://www.devze.com 2023-03-12 19:18 出处:网络
I\'m trying to do something rather simple here, just launch a new activity from my main one. Here\'s the code:

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

开发者_StackOverflow中文版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?


You want to use startActivity instead of startService

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


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消