How to explicitly and implicitly call an Activity on Android?

-1

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

If I want to call an Activity, an internal screen of my app and do a standard processing, such as calling the camera or gps function, which context should I use, explicit or implicit calling?

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

    
asked by anonymous 19.05.2016 / 16:19

2 answers

6

1 - Implicitly:

No AndroidManifest.xml declare intent filter to its 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 your Activity :

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

2 - Explicitly.

You use the same way as above but in the intent constructor you need to pass the parameters to activity current and the next activity.

Intent i=new Intent (this,MainActivity.class);
startActivity(i);
    
23.05.2016 / 02:44
3

To call your Activity explicitly, you will use Intent :

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

Próximo button on activity_nome.xml screen:

<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 File NomeActivity.java , like this:

public void next(View view) {

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

}

See above, imagining that you have a nearby button ( método next ), such as on a login screen, by clicking on it you will be sent to another via the Explicit Invocation.

  • Explicit intentions specify the component to start with by name (the fully qualified class name). Typically, 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, starting a new activity in response to a user action or start a service to download a background.

To invoke your Activity implicitly:

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

<?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 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 method next , in File NomeActivity.java , modify it as follows:

 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. For example, if you want to view the user a location on a map, you can use an implicit intent to request another capable application to display a location specified on the map.

When invoking explicitly:

  

Fixed call of a simple activity, used by default.

When invoking implicitly:

  

When you want to call a activity you know it can be   replaced by another activity (flexibility) or some feature   native android or third party service.

See the Android Developer's Guide: link

    
19.05.2016 / 16:19