How to determine which activity begins when i launch application? - android

I want know how i can determine which activity begins when i launch application ?
I have two Activity. MainActivity and Activity2. Which is the way i can code my application start every time in MainActivity ?
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Activity2"
android:label="#string/app_name"
android:screenOrientation="portrait"/>
<activity

Check out your AndroidManifest.xml file. In there you have each of your activities listed, then you can simply specify which one you'd like to be the default startup by adding this "intent-filter" shown below.
<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>

Related

Unable to assign activities in androidmanifest.xml as required for correct activity sequence

Intro - Sequence
Welcome Activity (only once per user first time open)
No internet connectivity (Check at time of launch of application)
Main Activity (Launch of application)
Current androidmanifest.xml sequence -
<activity
android:name="com.example.myapp.no_internet_alert"
android:screenOrientation="portrait">
<intent-filter>
<action android:name=".MainActivity"/>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity
android:name="com.example.myapp.WelcomeActivity"
android:screenOrientation="portrait">
</activity>
Is this what you're trying to do?
<activity
android:name="com.example.myapp.no_internet_alert"
android:screenOrientation="portrait"/>
<activity
android:name=".MainActivity"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.myapp.WelcomeActivity"
android:screenOrientation="portrait"/>

Trouble understanding how to set my login activity to the launcher activity in the Android manifest

I want to change my manifest so that the Login Activity is the first activity to start on the phone. The login activity checks the sharedpreferences to see if a user has login info saved. If they do it logs them in. Im pretty sure I need an intent-filter in login activity with <category android:name="android.intent.category.LAUNCHER" /> but I dont get what I should put for the action.
Heres my manifest
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<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=".DisplayMessageActivity"
android:label="#string/title_activity_display_message"
android:parentActivityName=".MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.incubatorcle.dinahealth.MainActivity" />
</activity>
<activity
android:name=".UserOnboardActivity"
android:label="#string/title_activity_user_onboard" >
</activity>
<activity
android:name=".LoginActivity"
android:label="#string/title_activity_login" >
</activity>
</application>
You need:
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
ACTION_MAIN action indicates this is the main entry point and does
not expect any intent data.
CATEGORY_LAUNCHER category indicates
that this activity's icon should be placed in the system's app
launcher.
Your MainActivity need to look like this:
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.yourpacgagename.MAINACTIVITY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
Your Login activity need to look like this:
<activity
android:name=".LoginActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
As your question is answered by #wariaten
Here is another way to do the login check
Add a third activity (where you check the SharedPreferences and launch the LoginActivity or the HomeActivity) and set it as the launcher as suggested by #wariaten

Android app with 2 launcher icons with separate taskAffinity

I have app with 2 launcher icons and 2 different Activities. Manifest file is as like below:
....
<activity
android:name=".MyActivity"
android:label="#string/app_name"
android:icon="#mipmap/ic_launcher"
android:taskAffinity="my.package.com.MyActivity">
<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=".MySettings"
android:label="#string/settings"
android:icon="#mipmap/ic_launcher"
android:taskAffinity="my.package.com.MySettings">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
....
I want MyActivity to be default screen to be opened after installing, so i used .category.DEFAULT.
action.MAIN and category.LAUNCHER are to show two separate launcher icons (1 for MyActivity, 1 for MySettings).
Problem: When i open MyActivity, MySettings screen is also gets opened. I want each launcher icon to open its corresponding Activity only. I have used taskAffinity for each Activity to solve problem but it did not work. I think to make taskAffinity work, Activity must be started with flag Intent.FLAG_ACTIVITY_NEW_TASK which i can not in my case (both are launch Activities).
I have also tried to use android:launchMode="singleTask" but it did not work either.
How to make each launcher icon to open only its Activity ?
UPDATE: Sorry, i realized MyActivity does not open MySettings, it seemed so though. MyActivity did not have UI, so it showed blank screen which is similar to MySettings. taskAffinity works correct. I have added android:theme="#android:style/Theme.NoDisplay" to MyActivity to hide UI.
Just use two different images for your apps to launch.
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<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=".MySettings"
android:label="settings"
android:icon="#drawable/ic_launcher2"
android:taskAffinity="my.package.com.MySettings">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
I solved that way:
<activity
android:name="andynaz.android.hyperfcalc.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="andynaz.android.hyperfcalc.MainActivity2"
android:label="#string/app_name"
android:icon="#mipmap/ic_launcher2">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
I did not use the taskAffinity, but I indicated the fully specified name of Activity classes (other than using 2 different images).

App widgets removed in home launcher if manifest activity intent-filter changed to different activity

I have an app with widgets. I created a new activity for a splash screen to show for a second and declared the new activity in my manifest with the intent-filter tags. I changed this:
<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>
to this:
<activity
android:name=".SplashScreen"
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=".MainActivity"
android:label="#string/app_name" >
</activity>
When I launched my app to the emulator, my widgets disappear from the home launcher and I have to re-add them. I want to add a splash screen to my app but I don't want my users to have to re-add their widgets, if possible. Any suggestions?

How to specify which activity starts on app launch?

I have an app with 3 different activities in it. When I launch the app one of the activities always starts first. But I want a differnt activity to start before the one that is currnetly starting first.
How would I change this to make a differnet activity start first?
Open your AnroidManifest.xml file and set the Launching Activity using the intent-filter tag as follows,
<activity android:name=".LaunchingActivity"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
You need to add an intent filter to the activity you want to start on application launch in your app's AndroidManifest.xml:
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
You need to make changes in AndroidManifest.xml file...
<application
android:icon="#drawable/image"
android:label="#string/app_name" >
<activity
android:label="#string/app_name"
android:name="define the activity which you want to start first here" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".FirstActivity" >
</activity>
<activity android:name=".SecondActivity" >
</activity>
<activity android:name=".ThirdActivity" >
</activity>
</application>
Hope this will help you....
In your AndroidManifest.xml put the following:
<activity android:label="#string/app_name"
android:name=".TestActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
The intent-filter inside the activity tells Android which Activity to launch.

Categories

Resources