How to call explicitly and implicitly an Activity on Android?

Asked

Viewed 3,642 times

-1

I would like to know how and when to call explicitly and implicitly an Activity(activity)?

If I want to call an Activity(activity), an internal screen of my app and do a standard processing, how to call the function of the camera or gps, what context should I use, explicit or implicit call?

If I want to call a native feature of Android, or third (from another application), what context should I use, explicit or implicit call?

  • 6

    I did not vote against your question, but it seems to me that it is not very elaborate and without context. It’s like you asked anyway just so you could post the answer. I think that if [Edit] and elaborate better, contextualize the use, and who knows how to detail a real need for use, can be better received. The only precaution, now that the question already has more than one answer, is to edit without changing the main doubt, not to invalidate the third party posts. Here are some tips to elaborate questions: [Ask]

2 answers

6

1 - Implicitly:

In the AndroidManifest.xml declare a intent filter for your activity:

<activity
    android:name="com.example.counter.MainActivity"
    android:label="@string/app_name" >
    <intent-filter>
        <action android:name="com.example.counter.MainAction" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
<activity>

Then you call yours Activity:

Intent i=new Intent ("com.example.counter.MainAction");
startActivity(i);

2 - Explicitly.

You use the same way as the previous one but in the intent you need to pass the parameters to activity current and the next Activity.

Intent i=new Intent (this,MainActivity.class);
startActivity(i);

3

To call your Activity explicitly, you will make use of the Intent:

Intent is a message object that can be used to request an action from another application component.

Knob Próximo onscreen activity_nome.xml:

<Button
        android:id="@+id/btn_prox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_below="@+id/edt_nome"
        android:layout_marginTop="10dp"
        android:text="@string/btn_prox"
        style="?android:attr/buttonBarButtonStyle"
        android:onClick="next" />

Method next in the Archive NomeActivity.java, in that way:

public void next(View view) {

Intent intent = new Intent(this, EmailActivity.class);
startActivity(intent); 

}

See above, imagining you have a button next(método next), as in a login screen, when you click on it you will be sent to another of it through the Explicit Invocation.

  • Explicit intentions specify the component to start with the name (the fully qualified class name). Normally, a explicit intention to start a component in the application itself because you know the class name of the activity or service you want start. For example, start a new activity in response to a user action or start a service to download a file in background.

To call your Activity implicitly:

Modify your Activity .EmailActivity in your file AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="br.com.intents">

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme">

        <activity android:name=".NomeActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".EmailActivity" />

    </application>

</manifest>

Modify by adding the intent-filter:

                <activity android:name=".EmailActivity">

                <intent-filter>
                <action android:name="br.com.intents.android.intent.action.EMAIL" />
                <category android:name="android.intent.category.DEFAULT" />
                </intent-filter>

                </activity>

In the method next, in the Archive NomeActivity.java, modify it like this:

 public void next(View view) {

    Intent intent = new Intent("br.com.intents.android.intent.action.EMAIL");
    startActivity(intent); 

    }
  • Implicit intentions do not name any specific component, but declare a general action to be taken, which allows a component from another application treat it. For example: if you want to display to user a location on a map, can use an implicit intention to request another capable application to display a location specified on the map.

When explicitly invoke:

Fixed call from an Activity, simple, used by default.

When implicitly invoke:

When you want to call one activity who knows she can be replaced by another activity(flexibility) or some resource native android or third party service.

See the Android Developer guide: https://developer.android.com/guide/components/intents-filters.html

Browser other questions tagged

You are not signed in. Login or sign up in order to post.