0

This is a simple project. I am using 2 activities.

  1. MainActivity
  2. NewActivity

I made 3 aliases of MainActivity

  • RED
  • BLUE
  • GREEN

I want to change icon and label of my launcher according to activity-alias I made. For this I made 3 buttons in New Activity. MainActivity simply calls NewActivity. But the app just crashing generating error that

Here is manifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.app_alias_check">

    <application
        android:allowBackup="true"
        android:enabled="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <activity
            android:name=".NewActivity"
            android:label="@string/app_name" />

        <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-alias
            android:name=".RED"
            android:enabled="true"
            android:icon="@drawable/red"
            android:label="Red app"
            android:targetActivity=".MainActivity">

        </activity-alias>

        <activity-alias
            android:name=".BLUE"
            android:enabled="false"
            android:icon="@drawable/blue"
            android:label="Blue app"
            android:targetActivity=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity-alias>

        <activity-alias
            android:name=".GREEN"
            android:enabled="false"
            android:icon="@drawable/green"
            android:label="Green app"
            android:targetActivity=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity-alias>


    </application>

</manifest>

Here is MainActivity.java:

package com.example.app_alias_check;

import android.content.Intent;
import android.os.Bundle;
import android.os.PersistableBundle;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

class MainActivity extends AppCompatActivity {
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState, @Nullable PersistableBundle persistentState) {
        super.onCreate(savedInstanceState, persistentState);
        Intent intent = new Intent(this, NewActivity.class);
        startActivity(intent);
    }
}

Here is NewActivity.java:

package com.example.app_alias_check;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ComponentName;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class NewActivity extends AppCompatActivity implements View.OnClickListener {

     private String []iconColour = {"RED", "GREEN",  "BLUE"};
    private Button bRed, bGreen, bBlue;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new);
        bRed = findViewById(R.id.btn_red);
        bGreen = findViewById(R.id.btn_green);
        bBlue = findViewById(R.id.btn_blue);
        bRed.setOnClickListener(this);
        bGreen.setOnClickListener(this);
        bBlue.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_red:
                setIcon("Red");
                break;
            case R.id.btn_green:
                setIcon("Green");
                break;
            case R.id.btn_blue:
                setIcon("Blue");
                break;
        }
    }

    private void setIcon(String targetColour) {
        int action;
        for (String value : iconColour) {
            if (value.equals(targetColour))
                action = PackageManager.COMPONENT_ENABLED_STATE_ENABLED;
            else
                action = PackageManager.COMPONENT_ENABLED_STATE_DISABLED;

            ComponentName componentName = new ComponentName(BuildConfig.APPLICATION_ID, BuildConfig.APPLICATION_ID + value);
            getPackageManager().setComponentEnabledSetting(
                    componentName,
                    action, PackageManager.DONT_KILL_APP);
        }
    }


}

Here is activity_new.xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".NewActivity">

    <Button
        android:id="@+id/btn_red"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Red"
        app:layout_constraintBottom_toTopOf="@+id/btn_green"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.501"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/btn_blue"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Blue"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.501"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/btn_green" />

    <Button
        android:id="@+id/btn_green"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Green"
        app:layout_constraintBottom_toTopOf="@+id/btn_blue"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.501"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/btn_red" />

</androidx.constraintlayout.widget.ConstraintLayout>

Here is the error log:

    --------- beginning of crash
07-17 20:06:49.420 3047-3047/com.example.app_alias_check E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.app_alias_check, PID: 3047
    java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.app_alias_check/com.example.app_alias_check.MainActivity}: java.lang.IllegalAccessException: class com.example.app_alias_check.MainActivity is not accessible from class android.app.Instrumentation
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2209)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
        at android.app.ActivityThread.access$800(ActivityThread.java:144)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5221)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
     Caused by: java.lang.IllegalAccessException: class com.example.app_alias_check.MainActivity is not accessible from class android.app.Instrumentation
        at java.lang.Class.newInstance(Class.java:1557)
        at android.app.Instrumentation.newActivity(Instrumentation.java:1065)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2199)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360) 
        at android.app.ActivityThread.access$800(ActivityThread.java:144) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:135) 
        at android.app.ActivityThread.main(ActivityThread.java:5221) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at java.lang.reflect.Method.invoke(Method.java:372) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 

I don't know whether it is allowed to make multiple aliases of an activity or only one alias is allowed?

gpl
  • 1,380
  • 2
  • 8
  • 24

1 Answers1

0

Multiple <activity-alias> are allowed.

You need to declare MainActivity as a public class. Add the public keyword to

class MainActivity extends AppCompatActivity {
David Wasser
  • 93,459
  • 16
  • 209
  • 274
  • OK I declared it public and everything is running fine but why my shortcuts on home screen get deleted whenever I switch ``? I want them to be preserved and change according to the new `` – gpl Jul 19 '20 at 06:10
  • If you disable an `` then it will not show on the HOME screen. Isn't that what you are trying to do? If not, please explain in more detail what you are trying to do. – David Wasser Jul 19 '20 at 15:25
  • I want to change my app icon and label. For that here I created 3 ``. `NewActivity` has 3 buttons for it, RED, GREEN, BLUE which change launcher activity. But when I disable current `` its shortcuts removed from home screen. I can switch between them easily but my app shortcuts getting removed from homescreen – gpl Jul 19 '20 at 16:41
  • Then you will need to put them on the homescreen yourself. I'm pretty sure that the shortcut on the home screen is done by the installer when the app is installed. If you then disable that, it will be removed from the home screen. You'll need to put them on the home screen yourself. See https://stackoverflow.com/questions/1103027/how-to-change-an-application-icon-programmatically-in-android for example – David Wasser Jul 19 '20 at 21:26
  • I think it is impossible to place them correctly on home-screen because we never know where user place that shortcut on which home-screen? I s there any way to programmatically change icon and label of `` at run-time? – gpl Jul 20 '20 at 06:08
  • Please see the linked question. I don't really know. – David Wasser Jul 20 '20 at 09:34