Welcoming a new face — #anonim_fw0 just made a spark with 5 #NFTs in the #NFToa ecosystem ⚡🔥
— NFToa (@nftoa_) September 2, 2025
Fresh energy like this keeps the community alive and growing. Thank you for joining the journey #eCash $XEC #NFTCollectors #NFT_support #CryptoTrading #CryptoNews #CryptoHelp pic.twitter.com/G2j1a8JW3y
The core of an application is actually 3, namely activity, service and broadcast. Intent itself is used to call activity, call service or do broadcast.
Figure 8.1. (a) First Activity view, (b) Second Activity view.
In this section we will understand the intent from the simplest, which is calling another activity. The scenario is, we have 2 layouts and 2 activities that are paired with each other. In the first activity, a button is provided, if clicked then it moves to the second activity. In this second activity, a button is also provided, if clicked then the second activity will be destroyed (closed) then the first activity reappears. Look at the following figure 8.1.
Well, can't wait, right??? Let's get started...
1. Make a new project first
2. We create the first layout using main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#000000"
android:text="Hehe.. ini activity-1" />
<Button android:text="Lanjut"
android:id="@+id/Button01"
android:layout_width=" fill_parent"
android:textSize="18px"
android:layout_height="55px">
</Button>
</LinearLayout>3. We create a new XML and name it main2.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#000000"
android:text="Trus yg ini activity-2" />
<Button android:text="Kembali"
android:id="@+id/Button02"
android:layout_width=" fill_parent "
android:textSize="18px"
android:layout_height="55px">4. Now we code for the first activity CallActivity.java
package cnt.CallActivity;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class CallActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button next = (Button) findViewById(R.id.Button01);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View bebek) {
Intent myIntent = new
Intent(bebek.getContext(), CallActivity2.class);
startActivityForResult(myIntent, 0);
}
});
}
}5. Create a new class named CallActivity2.java. Right click package>new>class.
package cnt.CallActivity;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class CallActivity2 extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
Button prev = (Button) findViewById(R.id.Button02);
prev.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent intent = new Intent();
setResult(RESULT_OK, intent);
finish();
}
});
}
}6. Now pay attention to AndroidManifest.xml, first register the second activity here.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="cnt.CallActivity"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="7" />
<application android:icon="@drawable/icon"
android:label="@string/app_name">
<activity android:name=".CallActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category
android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="CallActivity2">
<intent-filter>
<action android:name="android.intent.action.MAIN2" />
</intent-filter>
</activity>
</application>
</manifest>7. Done! Please run it.
Program Discussion
We start from the first activity callActivity.java, as usual we first synchronize the object to the widget in the xml. We synchronize the button in line 11, while line 12 plays a role in activating the button. Lines 15-16 are the intent declaration while line 17 activates the intent to call the second activity using the startActivityForResult() method. With this method, the new activity will appear above the previous activity.
Now let's discuss the second activity. Line 19 is the intent declaration, while setResult() on line 20 is the answer to the startActivityForResult() call on the first activity. Finish() on line 21 is the method to end the activity. With the completion of this second activity, what appears on the screen is the previous activity.
