How the android app's first activity launched? - android

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>

Related

Android: Add launcher icons programmatically

In the manifest, it is possible to specify multiple activities:
<activity
android:name=".Activity0"
android:label="#string/app_name0">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Activity1"
android:label="#string/app_name1">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
These will then result in two icons, placed on the launcher. I was wondering if the application can register more icons programmatically (based on application settings / user interaction with the app). Note that they would not need to run different activities, a single activity, starting with different intents would also work. Is this possible, or does one have to use widgets?
no need to use widgets - you can just add dummy activities that start your needed activity with the right parameters and then finish - you can also exclude them from recents so the user does not see the activity at all.
You can also install a shortcut by runtime - but then you need permission: INSTALL_SHORTCUT

Android application packaging

I have two android applications- A and B.
I'm trying to package them as a single unit so that upon installation the system installs both apps. However, I could not find any reliable answer till now.
In A's manifest file I added activity tag of B, resulting in error.
Could anyone guide me on how to package two applications as a single unit?
Thanks in advance!
I do not believe this is possible. An APK corresponds to a single AndroidManifest.xml which corresponds to a single Application.
However, if you want mulitple "launchers" or icons, it is possible by exposing multiple Activities by adding IntentFilters to the ones you want (which take the launcher event). However, they are still, technically, a single application.
Update:
Here's how to expose multiple Activities. The main activity would have something similar in the AndroidManifest.xml
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
Copy-paste this inside the other Activities you want to expose. For example:
<activity
android:name="com.example.app.FirstActivity"
android:label="#string/first_app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.app.SecondActivity"
android:label="#string/second_app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

How to change the startup activity in android?

I have two activities namely login and calendar in my Application. Currently my startup activity is "calendar". I want to run the login as first activity not calendar.
The startup activity [Launcher Activity] is declared in the projects' AndroidManifest.xml file
Look for that activity tag in the manifest which looks like this
<activity android:name=".Main"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Look at the attribute android:name. Main is the class which is launched when the app starts. Currently your calendar activity name should be there. Change that to the .classpath of your activity that you want to launch.
That should do it. You may also want to do the hello world application in the tutorials and go through the docs a little to see how Android Applications work.
Add Intent filter to the Activity in which you want startup.
In your case Modify the AndroidManifest.xml file as follows
<activity android:name=".login"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
remove the intent-filter code from calendar Activity tag in manifest and add it to the Activity you wanna load first
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
I mean paste it in the activity you like to run as default.
<activity
android:name="com.example.gridviewimages.AnotherActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Where as
From the docs
category -- Gives additional information about the action to execute. For example,
CATEGORY_LAUNCHER means it should appear in the Launcher as a top-level application, while
CATEGORY_ALTERNATIVE means it should be included in a list of alternative actions the user can
perform on a piece of data.
MAIN means that this activity is the entry point of the application, i.e. when you launch the application, this activity is created.
You want the Application element of the Android Manifest file. You can see details here.
Look at the name attribute, this points to the Application class.

In android every .java file becomes an app on the phone!

I have 6 .java files under one package.In eclipse, after I export the .apk and install the app on the phone or if i run the app on the emulator, there are 6 applications created, one for each .java file! The java files are different screens in my app.So i can open any java file by clicking on the icon in the menu.I only want one of them to be openable through the icon in the menu. So basically, only one icon should be seen in the main menu, which upon clicking opens the first activity in the manifest file!
Any idea whats wrong?
Thanks.
My assumption:
You registered all your Activities in the AndroidManifest (which is right) but as intent-filter you always used
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
That intent-filter tells Android to add the Activity to the launcher. Remove those Intent-filters (except one Activity that's going to be your main entry point) and it should work.
What does your manifest say? It might specify that all activities are shown as seperate apps...
Just check your AndroidManifest.xml file and modify your activity same as below code example:
<activity android:name=".MainActivity"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SecondActivity"
android:label="SecondActivity">
</activity>
So if you mark the above code then you can easily come to know that the below code mention that this activity is going to be act as a launcher activity.
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
Try to mention the intent-filter action as MAIN and Category as Launcher only for your entry activity in your app.
<intent-filter>
<action android:name="android.intent.action.MAIN"></action>
<category android:name="android.intent.category.LAUNCHER"></category>
</intent-filter>.
Remove this for all other activities except the entry point for your app.
Hope this helps.

How do I get multiple icons to launch different activities in one application?

I have an application with two activities and I'd like to be able to have two icons appear in the launcher, each launching the respective activity within the app.
Specifically, I want one icon to launch my main app, and another icon to launch my settings activity. Is this possible?
Here is what I've tried so far:
<activity android:label="MyApp" android:name=".MyApp">
<intent-filter>
<action android:name=".MyApp"/>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:label="Settings" android:name=".Settings">
<intent-filter>
<action android:name=".Settings"/>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
This creates two launcher icons, but they both run my main app instead of the second icon running my settings app. I've tried just having the launcher category but then I don't get an icon so it looks like I need the main action as well.
Is this the right approach or should I be declaring two applications in the manifest instead?
What you need to do is have your settings activity launch in another task. You can do this by specifying its task affinity. This is done with the attribute android:taskAffinity. By default all activities share the same task affinity that defaults to main package specified in the manifest. On your settings activity you can specify android:taskAffinity="your.own.package.SettingsTask" to have the settings activity launch in its own task.
Extra documentation.
You're definitely going in the right direction. This is what I have (truncated, because I have all of my activities in the list while I'm devving for fast access):
<activity android:name=".DeckDrill"
android:label="DeckDrill">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".DeckList"
android:label="DeckList">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
I think what may be happening is interference from your action elements which specify the name of your class. I'm pretty sure that actions and categories need to refer to constants. I don't know how that would result in what you're seeing, but you could try removing them. Other than that, you pretty much have what I have.

Categories

Resources