How to start an activity using a custom intent? - android

I have a very simple application (an example from a textbook) that consists of 2 activities: The first activity UsingIntentActivity has a button. When this button is clicked it must lead to the second activity called SecondActivity which will show a text on the screen.
I can achieve this using startActivity(new Intent(this, SecondActivity.class));
However in the textbook where I met this example another form of the same method is used:
startActivity(new Intent("net.dreamingpixel.SecondActivity"));
And in the Manifest File a matching custom intent is created (as I understood):
<activity
android:name=".UsingIntentActivity"
android:label="#string/title_activity_using_intent" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".SecondActivity"
android:label="Second Activity" >
<intent-filter>
<action android:name="net.dreamingpixel.SecondActivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
So there is an intent-filter and its category is set to DEFAULT. But when I try to run the app like this and click the button of the UsingIntentActivity the app crashes. In the log cat I can see the following messages:
FATAL EXCEPTION: main
java.lang.IllegalStateException: Could not execute the method of the activity
And then it points to the call of the startActivity method that I wrote at the beginning of this post.
Did anyone have a similar problem? Did I make a mistake somewhere?
UPDATE: Also I noticed that there is a warning in the Manifest file on the line where I open the second activity tag. It says: Exported activity does not require permission
Maybe this warning has to do something with my problem..

As you send that you have created second activity in manifest file as per
startActivity(new Intent("net.dreamingpixel.SecondActivity"));
Here net.dreamingpixel.SecondActivity means, here you need to provide the activity name with the package you created in your project...
In manifest at the top you will find package name. You need to use that package name with your activity...
Here as per above code..
net.dreamingpixel ----- is a package
SecondActivity ----- is an Activity in that package.

Related

Change Android App starting activity

I'm trying to change the start up activity.
I created an activity when the app loads but I want to add a screen before that, I'm not sure where to change the Android manifest to load a certain layout/activity when the app starts.
The startup activity [Launcher Activity] is declared in the projects' AndroidManifest.xml file
Look for that activity tag in the manifest which looks like this
<activity android:name=".Main"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Look at the attribute android:name. Main is the class which is launched when the app starts. Currently your calendar activity name should be there. Change that to the .classpath of your activity that you want to launch.
That should do it. You may also want to do the hello world application in the tutorials and go through the docs a little to see how Android Applications work.
From here
Just create another activity and set it as Launcher Activity and after a timer (if that's your achieve) just call the other activity!

How to change the main activity shown on the emulator to another activity?

I am having a problem with setting the main activity that is supposed to be shown on the emulator. Initially I already have an main activity (my first page) that will be shown when I first start up my emulator. But then now I wish to change the first activity that will be shown first when I run my application. I have changed the name of the activity in my android manifest however the first activity that runs is my previous activity instead of the new one that I have created. I have no idea why the new activity won't show although I have changed the android manifest Anyone has an idea on solving my problem? Thanks in advance. Any help would be greatly appreciated.
Specify the activity which you want to be launched initially with the intent filter in your Android Manifest.xml
<activity
android:name=".newActivity"
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=".FirstAvtivity" />
In the Android Studio Run -> Edits Configuration menu, you can change the Launch activity manually.

Newly added Activity designated as Main is not always "Main" Activity shown

Allow me to explain my predicament.
I have been building an app for a while now where it would immediately enter my MainActivity.class. I had this Activity declared in the AndroidManifest.xml file as such:
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
Then I realized that I wanted to add a Login Activity to my app so a NewMainActivity.class was required. I've been able to successfully implement this new Activity. And I changed the <intent-filter> of the old MainActivity.class to the following:
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
However this is where my problem arises.
When I press the back key on the Login Page (NewMainActivity.class) it goes through the onPause(), onStop(), and onDestroy() methods. When I start the App again the Login Page (NewMainActivity.class) is not shown, the old MainActivity.class is shown instead.
Can anyone guess what I'm still missing/doing wrong? It seems to me like it should always start on the Login Page everytime now. Let me know if a code sample would help.
Delete from your old MainActivity.class the intent-filter that you've defined:
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
Update: I've understood your question incorrectly at first. The intent-filter I've proposed to delete does nothing except exporting your activity. This means that any application can call the activity of your MainActivity.class So, in any case it should be deleted.
Now about the login activity. As I understand your scenario is the following. When you start your application it at first starts login activity. Then you enter your credentials and the main activity is appeared if the login process has been successful.
Thus, you have two activities in the stack: login and your main class. When you then push home button and run your application once again it starts from the last activity in the stack i.e. Main activity.
Thus, the problem is not in your intent-filters. You should finish your activities. In this case you'll always start from the login activity.
If at first Yury's suggestion does not work, and you did something similar to what I did, which was copy and pasting your workspace between multiple computers and loading the project to eclipse each time, then simply start a new project and copy the actual text within all your classes to fresh new classes in the new project.

How to start another Activity when you have the Activity name String but not the Activity.Class?

I'm running into a problem starting other activities from mine. I know this must be being done elsewhere as there are so many launcher apps out there which much use package manager to start specific activities...
I can get an Acitivity name I would like to start from the package manager, but how can I somehow parse this and turn it into an intent? Baring in mind I can't access the class... Also I would like to start that specific activity and not launch the MAIN intent from the package...
I'm sure someone must be doing this somewhere... It's kind of the point in activities isn't it?
Assuming you have following in your AndroidManifest.xml
<!-- The askUser dialog activity -->
<activity android:theme="#android:style/Theme.Dialog"
android:name="my.app.AskUserActivity"
android:excludeFromRecents="true"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="my.app.intents.AskUserConfirmConnect"/>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
Then you can call this Activity by name, like this:
Intent dlgIntent = null;
dlgIntent = new Intent("my.app.intents.AskUserConfirmConnect");
dlgIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
dlgIntent.addFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
context.startActivity(dlgIntent);
You can use setClassName(String, String) on the Intent to avoid needing the other class.

Start Activity with action, but no category

I am trying to start an activity defined in another apk, in its AndroidManifest.xml, it defines an activity and with an action, but no category defined.
The format is like
<activity name="...">
<intent-filter>
<action android:name="action name">
<intent-filter>
</activity>
My code is following
Intent i = new Intent("action name");
startActivity(i);
However my apk crashed with uncaught ActivityNotFound exception, the logs read
No Activity found to handle intent ... "
Any thoughts?
Thanx a lot.
Looking at the Intent documentation, it says Note also the DEFAULT category supplied here: this is required for the Context.startActivity method to resolve your activity when its component name is not explicitly specified. If the activity's IntentFilter definition does not include that category then you can't start it with startActivity. Try using the setClassName method, and pass it the package class and the activity class you're trying to launch.
you cannot have empty category when you use startActivity(...).
add a default category and this will do the job:
<intent-filter>
<action android:name="action name" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
You need to define the activity you are starting in your Manifest. Make sure you have provided same <intent-action (and name of the activity) that has the activity in the other apk you want to start.
android: how do i open another app from my app?

Categories

Resources