Start Activity of Main project from my library pro

2019-06-02 00:10发布

I have 2 projects. One is my Main Project(A) , and another is a Library Project(B). I want to start an activity which is present in A from an activity which is located in B. How do I do that ?

I Tried startActivity(getApplicationContext(),B.class); ,but

B.class

is not resolved.

How Can I let my library project start an activity of my main project ?

2条回答
地球回转人心会变
2楼-- · 2019-06-02 00:30

You can add custom Action in intent-filter of you activity and start that activity by specifying action

<activity android:name="my.package.MyActivity">
    <intent-filter>
        <action android:name="my.package.action.MY_ACTION"/>
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="my.package"/>
    </intent-filter>
</activity>

start activity with this code:

final Intent intent = new Intent("my.package.action.MY_ACTION");
intent.addCategory(getActivity().getPackageName());
startActivity(getActivity(), intent);
查看更多
我欲成王,谁敢阻挡
3楼-- · 2019-06-02 00:51

You shouldn't need to use an intent filter. The code in Activity A can use

ComponentName cn = new ComponentName(this, "my.package.MyActivity.B");
Intent intent = new Intent().setComponent(cn);

startActivity(this, intent);

to specify the activity B should be started.

查看更多
登录 后发表回答