How can I start MAIN activity with the help of ?

后端 未结 3 681
时光说笑
时光说笑 2020-12-07 03:14

When I declare my main activity in this maner:



        
相关标签:
3条回答
  • 2020-12-07 03:46

    the name attribute in the action tag is the name of the action, not the name of your activity. remove the line,

      <action android:name="com.package.name.MyActivity"/>
    

    since the intent filter tag is under your activity's tag, the system already understands that it's applied to that activity.

    0 讨论(0)
  • 2020-12-07 03:51

    Your class name isn't com.package.name.MyActivity, it's com.package.name.general.MyActivity.

    An easy way to avoid this mistake is to create your new Intent using a class instead of a package name. You can use auto-complete to fill in the right class.

    You say you don't want to create the Intent by specifying the class. I wonder why not -- I think it's a good way to go.

    0 讨论(0)
  • 2020-12-07 03:57

    Try to specify two intent filters:

    <activity android:name=".MyActivity"
              android:configChanges="orientation|keyboardHidden"
              android:windowSoftInputMode="stateHidden"
              android:screenOrientation="portrait">
        <intent-filter>
              <action android:name="android.intent.action.MAIN"/>
              <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        <intent-filter>
              <action android:name="com.package.name.MyAction"/>
              <category android:name="android.intent.category.DEFAULT"/>
        </intent-filter>
    </activity>
    

    Then you can start the activity using the action name:

    Intent intent = new Intent("com.package.name.MyAction");
    context.startActivity(intent);
    

    or the class name:

    Intent intent = new Intent(context, MyActivity.class);
    context.startActivity(intent);
    
    0 讨论(0)
提交回复
热议问题