Start Activity Using Custom Action

Start Activity Using Custom Action

I think you are creating your intent wrong. Try like this:

String CUSTOM_ACTION = "com.example.foo.bar.YOUR_ACTION";

//Intent i = new Intent(this, FeedBackActivity.class); // <--- You might need to do it this way.
Intent i = new Intent();
i.setAction(CUSTOM_ACTION);

startActivity(i);

Use custom intent action to launch activity only in specific application context

I figured out a way to achieve what I wanted.

Basically I do not hard code the package in my library, rather I start the login activity from the library using:

private static final String INTENT_ACTION_PATTERN = "%s.LOGIN";
public void startLoginActivity() {
Intent intent = new Intent();
String intentAction = String.format(INTENT_ACTION_PATTERN, mContext.getPackageName());
intent.setAction(intentAction);
mContext.startActivity(intent);
}

And I will just use a convention that I will always define the custom intent filter to match the package of my application:

<activity
android:name=".view.LoginActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="com.example.myapp.LOGIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>

Intent: Custom Action On Android

In the LogInActivity, you can retrieve the action of the caller intent.

String filteredAction = getIntent().getAction();

Now if anyone launched your LoginActivity with LoginActivity.LOGIN_ACTION then you will get com.sid.ugho.action.LOGIN_ACTION in filteredAction. But if the LoginActivity was not launched with action rather with in any other way, then you would get null in filteredAction.

Action bar on Action back (Navigation Up) to custom activity?

Alright make sure u do declare activities as below -

<activity
android:name="com.example.myfirstapp.DisplayMessageActivity"
android:parentActivityName="com.example.myfirstapp.MainActivity" >
<!-- The meta-data element is needed for versions lower than 4.1 -->
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.myfirstapp.MainActivity" />
</activity>

Now in every activity add below code block-

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// Respond to the action bar's Up/Home button
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}

Update
add a New class file to check login or not else use home as default.. and replace your new class as launcher in Manifest

How to open Activity via intent-filter and what is the use of intent-filter?

Activity can even be launched via IntentFilter
try this out

Basically when you install your app, Android system will register the activity with corresponding action, when you declare your activity with custom action, Android system stores the activity with the respective activity. When you launch the intent with your custom action. The system will find the receiving activity and launch it it there is only one activity matching it, if there are more than one Activity receiving that action, System will ask the user to choose which activity to complete the action.

declare activity in manifest as

       <activity
android:name=".YourActivity" >
<intent-filter>
<action android:name="your.custom.ACTION" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>

then you can start this activity by just calling

startActivity(new Intent("your.custom.ACTION"));

Start Activity with action, but no category

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.

How to start android activity from eclipse with custom Intent

If your still using eclipse you probably need need to create a simple ant script with a custom task to execute the test. ADB shell has a command to start activities where you can also specify extra's

am [start|instrument]

am start [-a <action>] [-d ]
[-t <mime_type>] [-c <category> [-c <category>] ...]
[-e <extra_key> <extra_value>
[-e <extra_key> <extra_value> ...]
[-n <component>] [-D] [<uri>]

am instrument [-e <arg_name> <arg_value>] [-p <prof_file>] [-w] <component>

You would pass them like this:

am start -a android.intent.action.VIEW -c android.intent.category.DEFAULT -e foo bar -e bert ernie -n org.package.name/.MyCustomActivity

P.S. don't forget the dot before the activity.

This can be translated to an ant target which you should put in the ant script.

<target name="run">
<exec executable="adb">
<arg value="shell"/>
<arg value="am"/>
<arg value="start"/>
<arg value="-a"/>
<arg value="android.intent.action.MAIN"/>
<arg value="-e"/>
<arg value="extra_key extra_value"/>
<arg value="-n"/>
<arg value="{package.name}/{activity}"/>
</exec>
</target>

which you can execute like this: ant debug install run

How to run ant files from eclipse see:

  • Eclipse Help ant
  • Eclipse Help Application launcher


Related Topics



Leave a reply



Submit