How to start an activity in an android Application from the Library

后端 未结 4 601
不思量自难忘°
不思量自难忘° 2021-02-11 07:22

I have an Android application in Android Studio. And I\'ve added a library into the application. The button, view, and activities are defined in the library. When I click on the

相关标签:
4条回答
  • 2021-02-11 07:52

    Include the activity in AndroidManifest.xml.

    To access an activity from any other project the easiest way is to pass the whole class name (including package, e.g; "com.myproject.MainActivitiy")

    Calling from your library :

     Intent intent= new Intent("com.myproject.MainActivitiy");
     startActivity(intent);
    

    And in your project manifest declare it like this

               <activity
                android:name="com.myproject.MainActivitiy"
                android:label="@string/app_name" >
                <intent-filter>
                    <action android:name="com.myproject.MainActivitiy" />
                    <category android:name="android.intent.category.DEFAULT" />
                </intent-filter>
            </activity>
    
    0 讨论(0)
  • 2021-02-11 07:57

    In this case use Implicit Intent

    Inside library Activity TESTActivity :

    Intent intent = new Intent();
    intent.setAction("com.myapp.myimplicit_action");
    startActivity(intent);
    

    and here is my manifest file declaration for some activity say 'ImplicitActivity' with the same action

    <activity  android:name=".ImplicitActivity">
       <intent-filter>
       <action android:name="com.myapp.myimplicit_action" />
       <category android:name="android.intent.category.DEFAULT" />
       </intent-filter>
    </activity>
    
    0 讨论(0)
  • 2021-02-11 08:03

    The normal way for doing this is to do this:

    Intent intent = new Intent();
    intent.setComponent(new ComponentName("com.my.package","com.my.package.activity.ActivityName");
    startActivity(intent);
    

    This is an explicit reference to an activity within your library. You should ensure that when starting this Activity that you catch the ActivityNotFoundException as this can happen when the Activity does not exist in the system.

    Ideally when building this Intent you should insure that you can resolve it by using PackageManager APIs.

    However you should try to avoid hardcoding packages, but when it comes to a library, sometimes you don't have a choice.

    Also one thing to note is that within the library you need to ensure that the Activity is exported so that you can access it outside of your application.

    android:exported

    Whether or not the activity can be launched by components of other applications — "true" if it can be, and "false" if not. If "false", the activity can be launched only by components of the same application or applications with the same user ID. The default value depends on whether the activity contains intent filters. The absence of any filters means that the activity can be invoked only by specifying its exact class name. This implies that the activity is intended only for application-internal use (since others would not know the class name). So in this case, the default value is "false". On the other hand, the presence of at least one filter implies that the activity is intended for external use, so the default value is "true".

    This attribute is not the only way to limit an activity's exposure to other applications. You can also use a permission to limit the external entities that can invoke the activity (see the permission attribute).

    Ref

    http://developer.android.com/guide/topics/manifest/activity-element.html

    0 讨论(0)
  • 2021-02-11 08:12

    In this case, Intent can be used by providing the full class name including the package.

    Let us suppose your current Activity class is MainActivity.java with package name com.app.myproject.

    And you want to navigate to another Activity with class named Activity.java that is in another package com.app.external.

    Include com.app.external.Activity.java in the manifest of your current project/library.

     <activity
            android:name="com.app.external.Activity"
            android:label="@string/title_activity_login"
            android:screenOrientation="portrait">
     </activity>
    

    And your Intent should be like this -

    Intent intent = new Intent(MainActivity.this, com.app.external.Activity.class);
    
    startActivity(intent);
    
    0 讨论(0)
提交回复
热议问题