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?
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.
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>
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
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'
Related
I have created a new project in android studio and when i am trying to run it ,it is saying default activity not found.i have checked my manifest file this does not contain any problem.
there is a Red X Mark android studio application while running
Exit Android Studio.
Go to path > C:\Users\YOUR_WINDOW_USER_NAME\.AndroidStudio3.3\system
Remove /caches folder.
Make sure
you have defined Launcher activity in Manifest file for
one Activity
you have set the default activity from Configurations
you have set the selected activity from Configurations.
There is a work around to make it work that worked for me.
1- Select Edit Configurations from the drop down next to run button.
2- Under launch options > Launch select Specified activity.
3- Another field "Activity" will appear select your default activity.
4- Click OK and sync project with gradle files and it should work.
two Method for solving this problem
Method 1.make sure that default activity is define in your manifest
Method 2. even default activity is define but not solved than go to your manifest file click on merged Manifest at bottom left side of your manifest file and you will see the side drawer menu of your manifest file you will see the error and from there you can solved your error by click on suggestion.
Check the manifest file and under the activity tags make sure it has:
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".Activity_name Here"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
Make sure that the line containing the action android:name= "android.intent.action.MAIN"
is included in the activity tag of the application.
Also make sure that activity name is corresponding to the android:name of the activity.
Try to check this code..
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
Check whether your Launcher activity intent filter has this
tools:node="remove"
If yes, remove it and error will be resolved. This piece of code will be used when we are having flavors in our project structure, in-order to remove a Launcher activity.
Else, Two launcher icons will be installed.
i have resolved this issue by changing the location of android studio from program files to program filesx86
When i download my app from store.
its installed on main screen, and on apps menu ( when i click the circle with 6 dots on it )
when i click from main screen it opens an app, and when i click from the other place..it opens a second app as well.
i need one app running only..
how can this be fixed?
my manifest
<application
android:name="dfsfsdp"
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".StartupActivity"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
I think you aren't running multiples instances (if you are running multiple instances if fail of the mobile), or you see two applications open in the menu what you use to close the running apps (the recent applications menu)?
If you only see one maybe your are restarting the same instance. Look at your StartupActivity or post here to try help you.
If this don't help you, take a look at to read about the lifecycle of an Activity:
http://developer.android.com/training/basics/activity-lifecycle/index.html
Best regards.
I have an activity, which is launched every time user wants to unlock a phone (MainActivity).
I wish to add another activity to the app, which will launch every time user clicks an icon of an app, and will contain settings for the first activity. What is the correct way to set it in AndroidManifest.xml?
Currently my AndroidManifest file looks like this:
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<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>
<receiver android:name=".BootCompletedReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<service android:name=".LockService"></service>
</application>
Define your activity in manifest like following:
<application>
...
<activity android:name=".YourNewActivity"></activity>
...
</application>
P.S:I assume that you activity is directly under the outermost package. if there are sub packages then you might need to use .subpackagename.YourNewActivity.
Now in your MainActivity, define a button inside who's onClickListener, you can start your second activity YourNewActivity using `Intents'. You might want to see this
How to start new activity on button click . Hope this helps.
You can't tie an activity to a button click in the UI inside the manifest file itself. Just add a normal <activity> and then ask for that activity to be called when you click the button.
The whole purpose of activities is that they can be re-used when the user opens the application again. You could create one activity and create a fragment everytime you open your app. Fragments do not have to be declared in your manifest. Your activity keeps track of the data. You are trying to add something dynamicaly (a unknown amount of activities) in a static xml-file (your manifest).
Just create a new fragment in your activity's onResume method.
http://www.vogella.com/articles/AndroidFragments/article.html
I'm having a problem with my app after changing the default activity in the manifest. This is the manifest after i changed it. As far as i can see it's syntactically correct.
<application android:icon="#drawable/icon" android:label="#string/app_name">
<uses-library android:name="com.google.android.maps" />
<activity android:name=".loginActivity"
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 android:name="CouncilPlannerActivity"></activity>
<activity android:name="MainTabActivity"></activity>
<activity android:name="MapTabActivity" android:theme="#android:style/Theme.NoTitleBar"></activity>
<activity android:name="NodeFormActivity"></activity>
<activity android:name="viewNewsActivity"></activity>
</application>
The problem is when i deploy the app to my device it works fine first time. However, when i close the app with the home button it refuses to open again. Clicking the icon in the devices app list doesn't do anything.
If i change the default activity to the one it was at originally it works fine. Is this a bug or is there another reference to a default activity that i'm missing?
I'm developing on Android 2.2 if that makes a difference.
I just noticed the logcat spits out an error when i try to open the app : "Permission Denied: checkComponentPermission() reqUID10064"
You probably need to post the loginActivity in question here so we can see if there are any problems in the activity. Otherwise double check that the loginActivity is in the same package as the other activities, if its not you need to change the ".loginActivity" part of the manifest to its relative location to the main package, aka "somename.loginActivity"
I think the problem is in loginActivity class. May be you check for already loggined user and finish activity in this case?
I am using Win 7.0, Eclipse and android SDK. I want to add new activity in AndroidManifest.xml Application tab as it is shown in this tutorial Android Development – Adding Screens & Button Handlers
I add an Activity name to my manifest but it does not automatically turn it into a link. e.g. I cannot click the "Name"(It is not a hyperlink as shown in the article), thus I cannot create my class.
Can You Help me? what is the problem ?
1.Go to the Androidmanifest.xml file and add the activity inside the tag
if your activity name is secondAct.
2.Create a class named secondAct.
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".Project1Activity"
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 android:name=".secondAct"></activity>
<activity android:name=".third"></activity>
</application>
3 . if you are using a button for going to next activity, use the following code in secondAct.java
Button fbtn=(Button)findViewById(R.id.sbtn);
fbtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent sec=new Intent(secondAct.this,com.asish.third.class);
startActivity(sec);
}
});
Go to the small tab underneath that says AndroidManifest.xml and shows you the XML code for it. It should look like this:
<application android:label="#string/app_name" android:icon="#drawable/icon">
<activity android:name=".ApplicationName"
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 android:name=".AnotherActivity"></activity>
</application>
Okay, click on ADD, then select the top box that says "Create a new element at the top level, in Application" and then you should get a box with linkable NAME*.
You need to create the class first, then point to that class in your manifest... just putting the class name in the manifest is not enough. It will not automatically create it for you.
Also, it is easier to create the class first because then Eclipse will autocomplete the class name/path for you.
EDIT: AH HAH! I see what link you are talking about...
Yeah, you need to actually create the class first for that to appear.