简体   繁体   中英

Android - How do i launch a new class and its activity?

I have two activity as below in same project. How do i launch the MainActivity from ServicesDemo? I have used the Intent but it does not launch MainActivity.

Mainfest i have only one:

<activity 
    android:name=".ServicesDemo" android:label="@string/app_name">

When the project launch it start this one:

public class ServicesDemo extends Activity implements OnClickListener {
  public void onClick(View src) {
    switch (src.getId()) {
      case R.id.buttonpicture:
        Intent i = new Intent(getBaseContext(), MainActivity.class);
        startActivity(i);
        break;
    }
  }

}

ServiceDemo needs to launch this also:

public class MainActivity extends Activity  implements OnClickListener {
}

EDIT:

Main fest: multiple activity listed

<activity 
    android:name=".ServicesDemo" android:label="@string/app_name">        
  <intent-filter>
    <action android:name="android.intent.action.MAIN" />           
    <category android:name="android.intent.category.HOME" />     
    <category android:name="android.intent.category.LAUNCHER" />
  </intent-filter>

</activity>

<activity android:name=".MainActivity" android:label="@string/app_name">
</activity> 

Called activity which is needed:

Intent i = new Intent(getBaseContext(), MainActivity.class);
startActivity(i);

Add the activity you want to launch into the Manifest:

<activity android:name=".MainActivity" android:label="@string/app_name" />

Then you can launch it with an intent:

startActivity(new Intent(this, MainActivity.class));
startActivity(new Intent("your.package.MainActivity"));
Intent i = new Intent(this, MainActivity.class);
startActivity(i);
finish();

then you must declare your activity in the manifest

<activity 
    android:name=".MainActivity" android:label="@string/app_name">

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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