How to run a certain activity in Android Studio?-ThrowExceptions

Exception or error:

For instance, I have a few activities within one app, and in order to see a certain activity’s UI or whatever, I need to run a certain activity that is not the launcher of the app.

One stupid way is to build a “door” for that activity in the launcher and go inside the activity from the door. However, Is there any better way to run a certain activity alone?

How to solve:

Very easy. Start by exporting the activity you need to run:

Add android:exported="true" in the Activity declaration in the Manifest. This is because am is an external application, and you need to export Activities to allow external application to start them.

Go to “Edit Configurations…” in the “Run” menu.

In the left pane, select your application. In the right pane, in the “General” tab, in the “Launch Options” section, there is a “Launch:” dropdown.

Select “Specified Activity”, and enter the name of your activity as it appears in your Manifest.

You can create as many Configurations as you like, and name them according however you like, for example to indicate which activity is being started.

###

I am using Android Studio stable version 2.1.2 and there is one shortcut to do so. Just open the activity class you wish to run and right click on coding area, There is options to run and debug the particular activity as shown in below screen shot.

For windows use shortcut ctrl+shift+F10 and for mac use ctrl+shift+R. I have tested this in emulator and its working fine, didn’t test in actual device.Works only for activity class and don’t forget to put cursor in coding area by clicking on it. Also I am not aware whether this option available in older Android studio versions less than 2.1.2.

enter image description here

###

As mentioned in this answer, you can easily achieve that by giving the activity an action name in the manifest.xml of the app:

<activity android:name="Activity3" ... >
    <intent-filter>
      <action android:name="com.company.package.FOO"/>
      <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</activity>

then create the following intent from anywhere in order to run this activity specifically:

startActivity(new Intent("com.company.package.FOO"));

After your clarification that the activity has to be run firstly when running the app instead of the launcher, you can achieve that by not setting the content of the launcher activity and instead create an intent that runs the wanted activity:

MainActivity:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //setContentView(R.layout.activity_layout); // remove this line

    Intent intent = new Intent(ThisActivity.this, WantedActivity.class);
    intent.putExtra("EXIT", false);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(intent);
}

###

Add exported true Manifest declaration of that activity.

Go to that activity, right click anywhere, go will get certain option with a ‘Run XYZ Activity’ option too. just run it

###

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

enter image description here
enter image description here

###

First you need to have two or more activities in your app to start with. Let’s say you want to go to a certain activity in your app to display first. May be for testing purposes or any other. Let’s see how it can be done,
First you need to find the AndroidManifest.xml file. Its there under the manifests folder.
According to this first dispaly activity is MainActivity

Lets’s say I want to make the home activity display first. So what I have to do is simply cut the intent-filter…/intent-filter and paste it within home activity. Like this

First Displaying Acivity is MainActivity according to this,

<application
        android:allowBackup="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=".home">
        </activity>
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

When we want to make the home acivity display first simplay change it as this,

<application
        android:allowBackup="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=".home">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".MainActivity">
        </activity>
    </application>

This should work. Hope this will help

Leave a Reply

Your email address will not be published. Required fields are marked *