write a program of linking activities using intents in android.
Loading
write a program of linking activities using intents in android.
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Tuhin PaulRank #7 · 71.9K reputationPosted Feb 16, 2023, 5:57 AM EST
We will start with the Algorithm to list down the steps needed and then we will sit down to implement:
Algorithm/Step by step guide:
1. Create a new activity that you want to link to from your current activity.
2. Add an intent filter to the new activity in the AndroidManifest.xml file, so that it can be launched from your current activity.
3. In your current activity, create an Intent object that specifies the new activity to launch.
4. Add any necessary data to the intent using extra key-value pairs.
5. Call startActivity() on the intent to launch the new activity.
6. In the new activity, retrieve any data passed in the intent using getIntent() and extract the extra data.
Now we will start the Implementation:
1. Create a new activity that you want to link to from your current activity. For example, let's call it "NewActivity".
2. Add an intent filter to the new activity in the AndroidManifest.xml file, so that it can be launched from your current activity. Add the following code snippet to the "NewActivity" activity block in the manifest file:
3. In your current activity, create an Intent object that specifies the new activity to launch. For example, add the following code snippet to the button click listener of the button that should link to the "NewActivity":
Intent intent = new Intent("com.example.app.NEW_ACTIVITY");4. Add any necessary data to the intent using extra key-value pairs. For example, to pass a string value to the new activity, add the following code:
intent.putExtra("myString", "Hello World");5. Call startActivity() on the intent to launch the new activity. Add the following code snippet to launch the new activity:
6. In the new activity, retrieve any data passed in the intent using getIntent() and extract the extra data. For example, to retrieve the string value passed in step 4, add the following code to the onCreate() method of the "NewActivity":
String myString = getIntent().getStringExtra("myString");Code for Unit Testing:
To unit test this implementation, you can create a test case that launches the current activity and simulates a button click to launch the new activity. You can then use assertions to verify that the new activity was launched successfully and that any data passed through the intent was correctly received.
Example for the test case implementation:
public class MainActivityTest extends ActivityInstrumentationTestCase2 { public MainActivityTest() { super(MainActivity.class); } public void testLaunchNewActivity() { MainActivity activity = getActivity(); Button button = (Button) activity.findViewById(R.id.myButton); assertNotNull(button); // Simulate button click TouchUtils.clickView(this, button); // Verify that the new activity was launched Intent intent = getStartedActivityIntent(); assertNotNull(intent); assertEquals("com.example.app.NEW_ACTIVITY", intent.getAction()); // Verify that any data passed through the intent was correctly received String myString = intent.getStringExtra("myString"); assertEquals("Hello World", myString); } }This test case launches the MainActivity, finds the button with id "myButton", simulates a button click, and verifies that the new activity was launched with the correct action and any data passed through the intent was correctly received.