The app crashes when I click the button that uses startActivity.
I want to ask the user if they wish to use the app as their default launcher.
In Java, I have this function in MainActivity.java
:
public void promptSetDefaultLauncher(View view) {
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
try {
startActivity(intent);
} catch (ActivityNotFoundException e) {
Log.e("LauncherError", "Home activity not found", e);
Toast.makeText(this, "Home activity not found", Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Log.e("LauncherError", "Unexpected error", e);
Toast.makeText(this, "Unexpected error", Toast.LENGTH_SHORT).show();
}
}
It is linked to this button in the activity_main.xml
:
<Button
android:id="@+id/BTN_DefaultLauncher"
android:layout_width="match_parent"
android:layout_height="83dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="132dp"
android:text="@string/set_as_default"
android:onClick="promptSetDefaultLauncher"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent" />
Here’s the AndroidManifest.xml
:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<queries android:name="android.permission.QUERY_ALL_PACKAGES"/>
<queries>
<intent>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent>
</queries>
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Plainhome"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.HOME" />
</intent-filter>
</activity>
</application>
</manifest>
I tried modifying AndroidManifest.xml, modifying its call, calling it in other functions…
WhoAmI is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.