I'm developing two applications, AppA and AppB, and I want to start AppB from AppA.
In AppA, I am using
Intent initIntent = getPackageManager().getLaunchIntentForPackage("com.example.appB.ActivityB");
in AppB, I am adding an intent filter to the manifest file :
<activity
android:name="com.example.appB.ActivityB"
android:label="#string/title_activity_init" >
<intent-filter>
<action android:name="com.example.appB.ActivityB" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
But I got a java.lang.NullPointerException on the intent...
Any ideas would be welcome.
Your intent filter isn't complete for this API:
The current implementation looks first for a main activity in the category CATEGORY_INFO, and next for a main activity in the category CATEGORY_LAUNCHER. Returns null if neither are found.
So you need to modify Activity B's intent filter to include:
<action android:name="android.intent.action.MAIN" />
You are trying to specify an Activity in the Intent, even though you are creating a Intent meant to launch a package; this is not neccesary. Simply remove the Activity from your Intent like so:
Intent initIntent = getPackageManager().getLaunchIntentForPackage("com.example.appB");
And then just make sure that you have the following two lines in whichever Activity you wish to start when the application is launched:
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
They should be already be in there for your main Activity, although it looks like you were missing the android.intent.action.MAIN intent filter in your ActivityB.
Related
I'm trying to call main activity using implicit intents. I give both action and category in intent but before starting the activity android system gives me a list of applications to select from for opening the activity.
Code snippet I am using to call the main activity follows:
protected void initiateActivity(int requestCode, String value, String oper) {
Intent i = new Intent("android.intent.action.MAIN");
i.addCategory("android.intent.category.LAUNCHER");
i.putExtra("VALUE", value);
i.putExtra("OPER", oper);
startActivityForResult(i, requestCode);
}
It seems to me that every app in system will be having same action, category combo, hence android is giving me that list of apps to select from. What changes can I make to my Main Activity so that this issue is not seen?
Looks like you might need to separate out with intent-filters. It looks there is a good explanation in this post:
How can I start MAIN activity with the help of <intent-filter>?
Which recommends adding the filters such as these below or you will be calling the launcher:
<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>
I know I need to use to enable an activity to receive an Intent like this (not main activity).
<activity android:name=".MyApp_2ndActivity">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
However, I found it can be triggered too if "intent-filter" is removed, like this.
<activity android:name=".MyApp_2ndActivity">
</activity>
I am wondering what difference is in these 2 formats?
See here: http://developer.android.com/guide/components/intents-filters.html
The difference is the second one can only be started using an explicit Intent -- one which names the component it wants to start. The first one can be started by an implicit Intent -- one which does not specify the exact component but contains information for the system to find an appropriate match for. The intent filters are used by the system for resolving such intents.
Difference is the when we use this code:
<activity android:name=".MyApp_2ndActivity">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
This will be the first activity triggered when you starts your application.It doesnot need any explicit intents
and when we use this code:
<activity android:name=".MyApp_2ndActivity">
</activity>
the activity will be started with the use of Explicit intent
Intent filters are used, for example, when the activity starts from a specific event on the device. Your main activity has specific intent filters. If you want your application start when NFC tag is scanned, you can specify that via intent filters.
You can read more here for example.
http://developer.android.com/guide/components/intents-filters.html
when we created a helloworld application using ADT, The "MainActivity" will be loaded, Because every program has a entry, such as the "main" function, For android apps, We can declare many activities in the file called "AndroidManifest.xml", So I wanna know how this activity launched by android framework? which is android apps "main" entry ?
Manifest tells android which activity to launch. Actually, when you click app icon, OS consults with application's manifest file and looks for the launcher activity. You can declare any activity as your launcher by writing this inside your activity tag in manifest.
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
Manifest always acts as interface between your application and OS. It provides all the information about your app to OS like what are the permissions, what activities, what receivers you are using in your app including your LAUNCHER ACTIVITY.
Only one activity should have the "main" action and "launcher" category...
So in the AndroidManifest.xml file, you should essentially have only one:
action android:name="android.intent.action.MAIN"
category android:name="android.intent.category.DEFAULT"
Activities will very often need to support the CATEGORY_DEFAULT so that they can be found by Context.startActivity(). So, CATEGORY_DEFAULT can appear number of times.
Android does not grab whichever one appears first in the manifest but it starts with activity having CATEGORY_LAUNCHER.
CATEGORY_LAUNCHER : The activity can be the initial activity of a task and is listed in the top-level application launcher.
because of this
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
the activity declared with intent filter
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
will be launched first.
<activity
android:name="com.example.hello.HelloActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
I have an android project with several packages. The structure of the packages in this case is com.siva.restorative is the package that contains the activity I want to run.
My activities are declared in my manifest as
<activity android:theme="#style/YtdTheme" android:name="com.siva.restorativecare.RestorativeCare">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
The mainScreen activity displays fine, since it is inside the com.WAPP package. But when I try to run the setLocationActivity, I get the unable to find explicit class error. Here is how I have the intent parameters:
Intent i = new Intent(this,SecondActivity.class);
I guess first you should change the action string in manifest as:
<action android:name="com.siva.restorativecare.RestorativeCare" />
and then start running your activity as:
Intent i = new Intent("com.siva.restorativecare.RestorativeCare");
startActivity(i);
I am trying to launch an activity by specifying only its action(custom) defined in its intent filter from an activity in other application. The activity to be launched is the main activity of its application thus have android.intent.action.MAIN & android.intent.category.LAUNCHER set as action and category in its intent filter. Now according to android doc on Intent and Intent Filter, i do not need to specify DEFAULT category at all in this case. But doing the same i am i am unable to launch the activity.
LogCat says, Activity could not be found...
Am i misinterpreting the text or is there something else missing...?
Code, used for calling the activity
Intent i=new Intent();
i.setAction("android.AddressBookV4.firstActivity");
startActivity(i);
Definition of Activity being called in the manifest file
<activity android:name=".AddressBook"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.AddressBookV4.firstActivity" />
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
You need to add the LAUNCHER category to your Intent, since the <intent-filter> says that it matches the following Intents (in pseudocode):
((action IS "android.AddressBookV4.firstActivity")
OR (action IS "android.intent.action.MAIN"))
AND (category IS "android.intent.category.LAUNCHER")