In the previous post we have learnt how to navigate from one
activity to other, in real time scenario you generally need to pass data
between activities. In this post we will get to know how to do the same.
Scenario – Have a screen called Main where in you will have
to enter your name. When you press the button you will be taken to another
screen where in Hello <name> will be displayed.
Logic –
1) Create an activity with a Label having “Enter your Name”,
a editable text box in which you can write your name and a button which will
make the application to go to another Screen
2) Create an activity which has a label saying you are in
Activity 2 ‘In Activity2’, a textview which will prefix ‘Hello’ to what ever you
have entered in the textbox of the previous activity. Finally add a button
which will end the flow and will return back to the main screen.
So now let us add the code to the logic mentioned above
assuming you have created a new project for this, add a new java class in src à <your package name>
à <your java class>.
1) Main Activity –
a) Creation of Layout – Go to res à layout àmain.xml file. Add
Linear Layout into the UI, add the Label with the text “Enter your Name”. Drag
Editable Text box into the layout and place it below the Label. Below this drag
a Button and edit the text to “Say Hello”. So the layout will look like in the
snapshot given below.
b) Creation of the activity -Now go to the src à <package you
created> à
<java file>. In this add the code which will perform the activities when
the button is clicked.
In this create a button as shown below
Button act1= (Button) findViewById(R.id.button1);
Then add the functionality which will instruct the app to
move to other screen and pass the name to other screen as shown below.
public void onClick(View view) {
Create an intent and point it to the other activity’s class
Intent activity1=new
Intent(view.getContext(),subActivity.class);
Since you have to send data of the editable text box to
other screen include the code to read it in this listener
EditText ET=(EditText) findViewById(R.id.editText1);
String Name="Hello, "+ET.getText().toString();
Since we need to pass extra data into the sub activity, use
the function <intent>.putextra(<keyname>,key) as show below.
activity1.putExtra("Name", Name);
Then start the new activity using the function startActivityForResult(<intent
name>,<intent request code>)
startActivityForResult(activity1,1);
The activity will look like the snapshot shown below.
2) Sub Activity –
Now that we have configured the main activity, let us code
the subactivity.
a) subactivity.xml – Create a new xml file called
subactivity.xml in res àlayout.
Add linear layout to the UI. Add a label which has the text ‘In
Activity2’. Add a TextView in which the message “Hello <message from
main activity>” will be displayed. Then add a button which will finish the
activity and return back to the main screen. The layout will finally look like
the snapshot shown below.
b) Creation of the activity – Now go to srcà<your package name>à<your java class>
and extend activity so that you can use all properties of an activity here.
Create a function onCreate function which will perform the desired activities.
In this function set the contentview to the layout which you have just defined
as shown below.
setContentView(R.layout.subactivity);
Now you have to write a code which will accept the data from
the previous activity.
//getintent is used to get the previous intent
Intent mainAct=getIntent();
//once you get the previous intent, you can access the key
which holds the data and use it as shown below.
String
VariableName=mainAct.getExtras().getString("Name");
TextView helloText=(TextView) findViewById(R.id.textView3);
helloText.setText(VariableName);
Then create a Button which will finish the activity and take
you back to the initial screen using the following code
Intent subAct=new Intent();
setResult(RESULT_OK,subAct);
finish();
The subactivity java file will look as in the snapshot show
below.
3) Modification to manifest file – In the manifest file add
the new activity with the following code.
<activity
android:name=".subActivity"></activity>
The manifest file will look as shown in the snapshot shown
below.
Now we are done with the coding part, lets see if it
actually works. Right click on the project and run it. The initial screen will
be as shown in the picture below.
Enter Android in the textbox and click on
the button. It should take you to the other screen where in you will have a
text saying ‘Hello, Android’ as
shown below.
Once you click on the button here it should take you back to the
initial screen.
So we have learnt how to pass data between different activities. Well this is a simple one there are many complex scenarios which will be posted later :). Till then Adios!!!!!!!!
No comments:
Post a Comment