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