Click here to Skip to main content
Click here to Skip to main content

Starting (Launching) other Activity and passing Data to it

, 11 Jul 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
This article explains how to start a new activity in your Application and how to pass data to it to work on.

Introduction

This article explains the trouble/problem beginners in the Android developers have like Launching a new activity. It also explains how you can do it and how you can pass on the Data to it to work on.

Background

When I started the Android development back in 2013, I had the very similar problem. Until now I have seen many people ask for the same problem. So I have created a simple tutorial for beginners to understand what is the basic idea behind this problem/solution.

Using the code

To start this, create the simple Android Application inside the Eclipse software. Edit the activity_main.xml file and created a New Button there with the message of Launch Activity. After this, created a new Android Activity and called it SecondActivity. Then, instead of doing all the hard coding, I'd let the Eclipse handle everything for me. 

Use the following code to work with the Activity thing. 

First of all, edit the Button and add the Click handler to it, its a bit easy way of handling all the Events, otherwise you can also use the onClickListener etc.

android:onClick="launchActivity"

After this, create the Activity handler inside the MainActivity.java class file as this

public void launchActivity(View view) {
    Intent intent = new Intent(this, SecondActivity.class);
    intent.putExtra(MESSAGE, "Love for all, Hatred for none!");
    startActivity(intent);
}

Notice the usage of the Class View inside the method. It is required by the method to be passed on as a parameter to work on. But we're not using it here. I have created an Intent to launch the Activity. Actually when you want to do anything, you create a new Intent, Intent means that your activity intents to do something. In this case, it needs to launch a new activity.

Second parameter for the Intent instance is the Class for the Activity to be launched. I had the SecondActivity, so I used it. You can have any other Activity that you want to use. 

intent.putExtra is the method, that is used to attach the data with the intent. This way, intent is used to pass on data to the next activity too. I have used a Key Value pair. 

MESSAGE variable that was a constant must be created in the Application, in order for the Application to know that it exists, you can set it to be an Empty String, or put a message in it. 

public static final String MESSAGE = "Love for all, Hatred for none!";

Note that, it should not be inside any method. But in the Class itself as a Global variable.

After that, you should open up the second activity that you want to get launched and get the data there. 

To get the data in the Activity, use this code.

Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.MESSAGE);
TextView textView = (TextView) findViewById(R.id.messageToShow);
textView.setText("Data sent with the intent was: " + message);

 

History

First version of this post.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Afzaal Ahmad Zeeshan
Student De Codes
Pakistan Pakistan
What should I tell you? Why not contact me and ask for yourself!
Follow on   Twitter   Google+   LinkedIn

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web03 | 2.8.141029.1 | Last Updated 11 Jul 2014
Article Copyright 2014 by Afzaal Ahmad Zeeshan
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid