How do I select the activity to start? - android

I am working with android studio and combined three apps together. All the apps have been imported with their manifest file and each of those manifest files has an activity that is the main one that starts first. I have noticed that whichever application I import last, its main activity becomes the main activity of the whole project when its run. How do I change that?

Well it depends on the requirements of your application, you can define an Activity as "MainActivity" by defining the filter:
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
This is an example:
<activity android:name=".activities.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
But you can choose other Activity as MainActivity , for example my application starts with SplashActivity to load all neccesary resources and then finish and start my "MainActivity".
<activity android:name=".activities.SplashActivity"
android:theme="#style/SplashTheme"
android:configChanges="keyboardHidden|orientation|screenSize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
You can keep other Activity as your MainActivity, probably an Activity that make transactions between Fragments, and you never finish it until you need to close completely the application.

Related

What does it mean when two or more activities, each having intent-filter with action=android.intent.action.ACTION_MAIN?

Doc says https://developer.android.com/reference/android/content/Intent.html#ACTION_MAIN is an entry point.
Example code:
<activity android:name="org.A.A"
android:theme="#style/NoTitle"
android:screenOrientation="behind"
android:launchMode="singleTask">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</intent-filter>
</activity>
<activity android:name="org.A.C"
android:theme="#style/NoTitle"
android:launchMode="singleTop"
android:screenOrientation="behind">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</intent-filter>
</activity>
<activity android:name="org.A.B"
android:theme="#style/NoTitle"
android:launchMode="singleTop"
android:screenOrientation="behind">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</intent-filter>
</activity>
1) So using android.intent.action.ACTION_MAIN acts as an entry point to the parent component (parent component I mean the activity or receiver or service)?
2) If yes, Entry point from where, as there is no CATEGORY mentioned.
Android apps are composed of different components. e.g. Activity, Service, BroadcastReceiver, and ContentProvider and each component can act as an entry point of the app.
Let's take activity as an example, you have defined an activity in your application with following action
<intent-filter>
<action android:name="com.yourapp.SOME_ACTION" />
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
and I start an activity in my app with following intent.
Intent intent = new Intent("com.yourapp.SOME_ACTION"); // same action
startActivity(intent);
Now what will happen? System will search for activities with com.yourapp.SOME_ACTION action and if it finds one (in current scenario, it will be activity you have created with com.yourapp.SOME_ACTION in your app), it will start your app (if it is not already started) and will open the activity in your app.
See, now I can enter into your app by using Activity with com.yourapp.SOME_ACTION. Same thing happens in case of other components.
Activity with MAIN action will be entry point of application.If you have one its well and good if more than one then you can have multiple activities through which you can enter into application.
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</intent-filter>
But if you have provided CATEGORY for activity it will create launcher for that entry point.
<category android:name="android.intent.category.LAUNCHER" />
Suppose two activities is having both MAIN action and CATEGORY as launcher two app icon will be created one will have one activity as entry point second will have another one as entry point.
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>

Android - How to change entry point of App without removing shortcuts?

So, in a previous version of my Application, I had an entry point named MainActivity
<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>
This version, I have a new entry point for my application, which then re-directs to MainActivity or another screen programmatically. Here are the two activities:
<activity
android:name=".NewEntryPoint"
android:label="#string/title_activity_second" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
I removed the action.MAIN from main activity and set it to my NewEntryPoint. However, any users that had the application as a shortcut on their homescreen will lose the shortcut. Keeping the Launcher category in MainActivity didn't help either.
Is there a way to change entry point of my App without removing shortcuts?
Thanks!
I believe you could use activity-alias for this, but I would question whether or not it's worth the future maintenance just to avoid users having to add a shortcut to your app again. Something like this:
<activity-alias
android:name=".MainActivity"
android:targetActivity=".NewEntryPoint">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity-alias>

Two main activities in AndroidManifest.xml

I would like to have two main activities in my app. So in my manifest I put:
<activity
android:name="mypackage1.MainActivity"
android:label="#string/title_activity_main">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="mypackage2.MainActivity2"
android:label="#string/title_activity_main2">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Two icons are created in my apps menu. But when I click on each of them the first activity MainActivity is always launched. Is it possible to have two main activities? If so, what's wrong with what I did?
Thanks
The LAUNCHER intent filter is what determines what shows up in the app drawer/launcher. That is why you get two icons shown up.
However, you also set the DEFAULT intent filter, which sets the default Activity for the whole package. Since you set it twice, you get the problem of precedence of the first/latest registered. When you remove the DEFAULT filter, you will be able to start whatever you click on in the launcher.
In short, remove the following line from both Activities:
<category android:name="android.intent.category.DEFAULT" />
Yes, just mark two or more of your <activity>s as LAUNCHER within your manifest.
In addition you have to set the android:taskAffinity attribute on both of your Launcher-Activities which specify the exact package and Activity to be started.
<activity android:label="MyApp" android:name=".MyApp" android:taskAffinity="com.example.MainActivity">
<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" android:taskAffinity="com.example.SettingsActivity" >
<intent-filter>
<action android:name=".Settings"/>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
Use android:documentLaunchMode="intoExisting", to launch a separate task based on the intent's Component name and data URI. Without this (by default), the activity will share all the same activities, as android:documentLaunchMode default to none.
intoExisting: The system searches for a task whose base intent's ComponentName and data URI match those of the launching intent. If the system finds such a task, the system clears the task, and restarts with the root activity receiving a call to onNewIntent(android.content.Intent). If the system does not find such a task, the system creates a new task. source
<activity
android:name=".CameraActivity"
android:exported="true"
android:documentLaunchMode="intoExisting"
android:label="#string/app_1_label">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".ProfilePoseNetActivity"
android:exported="true"
android:documentLaunchMode="intoExisting"
android:label="#string/app_2_label">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
The intention of potroae using task affinities to prevent your 2 activities from sharing the same task. However, it is annoying to have to select a task name, i.e. com.example/SettingsActivity for every task you want to launch separately.

In an Android application,can more than one Main Activity exist

I hope someone will help. In Android manifest file, can we specify more than one activity as the main activity?
Yes you can. But you should define one as default by CATEGORY_DEFAULT. Without default main activity if you have two activities, Android Market do not know what activity to start.
<activity
android:name=".FirstMainActivity"
android:label="First Activity"
android:icon="#drawable/first_icon">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".SecondMainActivity"
android:label="Second Activity"
android:icon="#drawable/second_icon">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
You have to set action=MAIN and category=LAUNCHER to be your entry point showed in launcher.
Yes you can have more than one main activity and you can have multiple launcher activities but if you do so, you will see as many icons in the applications drawer.
If you think you have several entry points in your application then why not?

Issue with launching Android application via Intents

My problem is thus; I am new to programming on the ANDROID platform and have a 'working' application that piggy-backs on the API-Docs example. I wish it to launch three tabs one containing a list of reports, one a form to file a report and the last to show the geo-located reports. It doesn't appear as a separate application, it instead appears as a list to be launched by the API-Docs example. Below is my manifest code...
<activity android:name=".HelloFlamingos">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.SAMPLE_CODE" />
</intent-filter>
</activity>
<activity android:name=".Controls2" android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.TAB" />
</intent-filter>
</activity>
<activity android:name=".List1" android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.TAB" />
</intent-filter>
</activity>
<activity android:name=".ReviewTab" android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.TAB" />
</intent-filter>
</activity>
</application>
<uses-sdk android:minSdkVersion="3" />
HelloFlamingos is the initial page that I wish to be displayed, I realise that the intents for this are wrong, have thought about using category: VIEWS, action: DEFAULT, however is seemingly unwilling to work. Thanks!
In what context is the activity started? If you're looking to have the HelloFlamingos activity the first displayed from the Android OS, you should change its category in the manifest to category.LAUNCHER.
If you're looking to start the activity from elsewhere in your app, create an Intent which matches what you've specified (category.SAMPLE_CODE) and use startActivity or startActivityForResult.

Categories

Resources