Two Launcher Activities

Two launcher activities

You need to specify which activity is the default one by adding the following line to your intent-filter:

<category android:name="android.intent.category.DEFAULT"/>

Insert this in the default activity and keep the rest. Then it should work.

You may also want to add a different icon to your 2nd activity with the attribute android:icon="@drawable/myothericon"

How to properly define 2 main launcher activities in Android

so it has less priority, and the operating system takes into account the first launcher activity as the main one

There is no concept of "main one". How a launcher chooses to order its launchable activities is up to the developers of the launcher.

I would like to know if there is a way to define which one is the main one with more importance over the other

No, sorry.

I couldn't find any info about multiple launcher activities in Google policies, but is there any policy or guidelines to follow when defining more than one?

Questions about policies for app distribution channels are considered to be off-topic for Stack Overflow. FWIW, I am not aware of any restrictions here, and Google used to distribute apps with multiple launchable activities (notably Google Maps). I do not know if any of Google's current apps offer multiple launcher activities.

Is it possible to have more than one launcher Activity?

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>

Two launcher activities creating two APKs

Use <intent-filter> only once in manifest. like this

<activity android:name=".LaucherActivity" android:theme="@style/SplashTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<activity
android:name=".MainActivity"
android:screenOrientation="portrait"
android:theme="@style/AppTheme.TransparentTheme">

</activity>

can we make two launcher activities and launch one based on some condition?

I will recommend the best way around. Whenever you are stuck on such things, Splash screen will help you.

Let me explain how:-

Just make splash screen as Launcher activity. If you don't want that to display it for much longer, just have the handler run for 1-2 seconds. Now look at the code below.

SplashScreen.java

public class SplashScreen extends AppCompatActivity {

private String email;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

setContentView(R.layout.activity_splash);

//SharedPreference to Store API Result
SharedPreferences pref = getApplicationContext().getSharedPreferences("CachedResponse", 0);
SharedPreferences.Editor editor = pref.edit();
editor.apply();

email = pref.getString("login", null);

int SPLASH_TIME_OUT = 3000;

if (email != null) {

//It means User is already Logged in so I will take the user to Select_College Screen

new Handler().postDelayed(new Runnable() {

@Override
public void run() {

Intent intent = new Intent(SplashScreen.this, Select_College.class);
intent.putExtra("Email", email);
startActivity(intent);
finish();

}

}, SPLASH_TIME_OUT);

} else {

//It means User is not Logged in so I will take the user to Login Screen

new Handler().postDelayed(new Runnable() {

@Override
public void run() {

Intent intent = new Intent(SplashScreen.this, Login.class);
startActivity(intent);
finish();

}

}, SPLASH_TIME_OUT);

}

}
}

Login.java

public class Login extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_doctor_login);

//SharedPreference to Store API Result
pref = getApplicationContext().getSharedPreferences("CachedResponse", 0);

Login();

}

private void login() {

//If login is successfull, before moving to next activity, store something in sharedpreference with name login. It can be email or just a string as "true"

SharedPreferences.Editor editor = pref.edit();
editor.putString("login", email);
editor.apply();

Intent intent = new Intent(DoctorLogin.this, Select_Collage.class);
intent.putExtra("Email", email);
startActivity(intent);
finish();

}
}

Select_Collage.java

public class Select_Collage extends AppCompatActivity {

private SharedPreferences pref;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_doctor_message);

//SharedPreference to Store API Result
pref = getApplicationContext().getSharedPreferences("CachedResponse", 0);

//Somewhere on Signout button click, delete the login sharedpreference
signOut();

}

private void signOut() {

SharedPreferences.Editor editor = pref.edit();
editor.remove("login");
editor.apply();

Intent intent = new Intent(Select_Collage.this, Login.class);
startActivity(intent);
finish();

}
}

So that's how you can solve this problem. Keep Coding :)

Android multiple launcher Activities?

Use android:taskAffinity maybe resolve your case.



Related Topics



Leave a reply



Submit