Why my activity has not started up? - android

I am using implicit way to start an Activity. But when it runs, LogCat shows me can not find the Activity.
here is my declare of <intent-filter>:
<activity android:name=".GroupEditActivity">
<intent-filter>
<action android:name="android.intent.action.EDIT"></action>
<data mimeType="vnd.android.cursor.item/group"></data>
<category android:name="android.intent.category.DEFAULT"></category>
</intent-filter>
</activity>
Here is the code to start Activity:
Intent intent = new Intent(Intent.ACTION_EDIT);
intent.setType(Groups.CONTENT_ITEM_TYPE);
startActivity(intent);
Reference said if I set the type and action the same as Intent-filter, them my Activity can start up. But when I only set action in Intent, it still can start the Activity which Intent-filter contains mimetype. So I totally lost.
here is what i find in official website:
Thanks
logcat details:
android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.EDITtyp=vnd.android.cursor.item/group }
And I can sure the type and action in java and xml are the same.
I solve the problem myself. The error is cause by <data mimeType. I forgot to add android here. So if change to <data android:mimeType, then everything works fine. But why it doesn't report a exception before, does mimeType is also legal?

Are you launching an activity separate from the main activity? In this case you need to call the name of that activity. If you're trying to launch your main activity, it doesn't need to call an intent.
Intent intent = new Intent(this, MyOtherActivity.class);
startActivity(intent);

Related

Unable to start activity through intent: ActivityNotFoundException

I have this Activity defined in my AndroidManifest.xml:
<activity
android:name=".Splash"
android:label="#string/app_name"
android:noHistory="true" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
When I am trying to call it in this way:
Intent splashActivity = new Intent("android.intent.action.Splash");
mainAppContext.startActivity(splashActivity);
I get:
android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.Splash }
And when I try to start it in this way:
Intent splashActivity = new Intent("android.intent.action.MAIN ");
mainAppContext.startActivity(splashActivity);
It open a Complete action using dialog with many options there like play store etc.
Please help in starting this Activity.
From some other Activity:
startActivity(new Intent(this, Splash.class));
Intent splashActivity = new Intent("android.intent.action.Splash");
No-no-no! It should to
Intent splashActivity = new Intent(context, Splash.class);
If you use action - it seem that you want activity for some type of action, for example, view webpage on browser.
In your situation you have to use concrete activity and set it to intent
<action android:name="android.intent.action.MAIN" />
this action is not for your app, it is needed to android's launcher, that find activity in your application to launch it
Official documentation about intent mechanism
Where are you trying to launch this activity from? The following lines imply that Splash is your main Activity:
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
Are you trying to launch the Splash Activity from Splash itself? This would be problematic. If you are launching from another Activity then you can use the following to launch Splash:
Intent intent = new Intent();
intent.setClass(YourCurrentActivity.this, Splash.class);
startActivity(intent);
Activity not found exception is occurred when u are trying to use or start activity that is not declared into the Mainfeast.xml file.
And When you want to start any activity then you need to start that activity by using intent. but in android "android.intent.action.Splash" Intent Action exist. And you have declared the following intent filter
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
"android.intent.action.MAIN" this is the action name that indicate the this is the activity to be started first. and android send this intent and an activity that is registered with this intent action is the first activity to be started first.
So to start splash activity you doesn't need any intent. You just need to simply run this project. but If u want to Start Another activity from splash actiivty then u need to do following things.
Intent intent = new Intent(Context, ActivityName.class);
startActivity(intent);
or
startActivityForResult(intent, requestCode);
and remember that You have to declare that activity into the mainfeast.xml

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?

Failing to start an activity with an implicit intent

I am a little confused why the implicit intent call is failing. When trying to start an intent I keep getting the following error:
android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=content://org.chrisolsen.crossfit.providers.WorkoutProvider/workouts typ=vnd.android.cursor.dir/vnd.chrisolsen.crossfit.workout }
AndroidManifest
<activity android:name=".activities.WorkoutsActivity"
android:label="#string/title_workouts" >
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<data android:mimeType="vnd.android.cursor.dir/vnd.chrisolsen.crossfit.workout"/>
</intent-filter>
</activity>
<provider
android:name=".providers.WorkoutProvider"
android:authorities="org.chrisolsen.crossfit.providers.WorkoutProvider" />
Calling activity (dashboard)
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(WorkoutProvider.CONTENT_URI, "vnd.android.cursor.dir/vnd.chrisolsen.crossfit.workout");
startActivity(intent);
Called activity (workouts). It doesn't make it here
Uri uri = getIntent().getData();
...
It seems like it should be simple, but I am confused to why it says there is no activity found.
Any ideas?
In order to be started with implicit intents, An activity must declare
<category android:name="android.intent.category.DEFAULT" />
Also, make sure you are using startActivity instead of sendBroadcast. There is a difference between these methods. A broadcast will not be received by an activity's intent filter. You must use a BroadcastReceiver for that.
Note that, although the Intent class is used for sending and receiving these broadcasts, the Intent broadcast mechanism here is completely separate from Intents that are used to start Activities with Context.startActivity(). There is no way for a BroadcastReceiver to see or capture Intents used with startActivity(); likewise, when you broadcast an Intent, you will never find or start an Activity. These two operations are semantically very different: starting an Activity with an Intent is a foreground operation that modifies what the user is currently interacting with; broadcasting an Intent is a background operation that the user is not normally aware of.
Source: http://developer.android.com/reference/android/content/BroadcastReceiver.html
Android docs: sendBroadcast
Android docs: startActivity

How to export an activity so other apps can call it?

Well I searched a lot, but I didn't find a precise answer how to export an Activity, so an app can start it with startActivityforResult.
How do I achieve that? Do I have to change the Manifest in some ways?
As an alternate to Dalmas' answer, you can actually export an Activity without creating an <intent-filter> (along with the hassle of coming up with a custom action).
In the Manifest edit your Activity tag like so:
<activity
android:name=".SomeActivity"
....
android:exported="true" />
The important part is android:exported="true", this export tag determines "whether or not the activity can be launched by components of other applications". If your <activity> contains an <intent-filter> then this tag is set to true automatically, if it does not then it is set to false by default.
Then to launch the Activity do this:
Intent i = new Intent();
i.setComponent(new ComponentName("package name", "fully-qualified name of activity"));
startActivity(i);
Of course with this method you will need to know the exact name of the Activity you are trying to launch.
You need to declare an intent-filter in your Manifest (I took the following example from Barcode Scanner) :
<activity android:name="...">
<intent-filter>
<action android:name="com.google.zxing.client.android.SCAN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
Then create an intent with the same action string :
Intent intent = new Intent("com.google.zxing.client.android.SCAN");
startActivityForResult(intent, code);
Android should start your activity (or it will show a drop-down box if there are multiple apps sharing the same action string).

android check if an activity was started from an action or from another activity?

In my manifest file, I have an activity declaration which looks something like this:
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
This activity is obviously launched on application start. User can navigate from this activity to another activity and from that one to another etc.
In another activity, I start MainActivity by using 'startActivity' method, ie. something like:
Intent intent = new Intent(AnotherActivity.this, MainActivity.class);
startActivity(intent);
In my MainActivity (in onCreate() method maybe), can I determine whether an activity was started from a action of from another activity? Is there something like "launcher listener"? I would like to avoid putting any extra content in the intent.
Can I simply put String s = getIntent().getAction(); in onCreate method and check whether it is has a value of MAIN?
Well the stock android launcher does send Intent.ACTION_MAIN as as the action. However, you can't be sure that some other launcher will have the same behaviour. Your best bet will be to pass some extra data with the Intent.

Categories

Resources