guo
guo

Reputation: 10211

How to run a certain activity in Android Studio?

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?

Upvotes: 24

Views: 47575

Answers (7)

Asad Lodhi
Asad Lodhi

Reputation: 51

For my example, the specific activity was called Activity2 and the project was called ScreenSizes

1- Open the Android Manifest: app>manifests>AndroidManifest.xml

2- Change the activity section for the specific activity to include android:exported="true" like this:

<activity android:name=".Activity2"
      android:exported="true">
</activity>

3- Open the java class of the specific activity: app>java>com.example.(your app name)>(specific activity)

in my case it was: app>java>com.example.screensizes>Activity2

4- Right click anywhere in the blank/white area of the Java file and select the option Run '(activity name)'

in my case it was: Run 'Activity2'

Upvotes: 0

Gayan Prasad
Gayan Prasad

Reputation: 1

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

Upvotes: 0

swati kapoor
swati kapoor

Reputation: 131

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

Upvotes: 1

Harry Sandal
Harry Sandal

Reputation: 51

<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

Upvotes: 0

Pravin Divraniya
Pravin Divraniya

Reputation: 4374

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

Upvotes: 5

njzk2
njzk2

Reputation: 39397

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.

Upvotes: 37

Muhammed Refaat
Muhammed Refaat

Reputation: 9103

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);
}

Upvotes: 1

Related Questions