You are on page 1of 5

LAB 4

ACTIVITIES
List of Experiments

 Create layout for the two activities of the application

 Override all the methods in activity life cycle and observe

 Create second Activity

 Create a new method onSendButton in MainActivity.java, inside it create an intent and


and start the new activity using intent.

 Send data from one activity to another

 Receive reply from second activity to the first activity

 Change the label of the app bar in second screen.

What you should already KNOW From the previous practicals,

 You should be able to: Create layouts

 Create and call methods from xml

What you will LEARN

 Understand the activity lifecycle, and when activities are created, pause, stop, and are
destroyed.

 Understand the lifecycle callback methods associated with activity changes.

App Overview:

Create an App that can send a message from one screen to another using intents. Also the app
should be able to receive reply of an intent.

Task 1 Create layout for the two activities of the application


Task 2 Override all methods of Activity Life Cycle

Note: onCreateMethod is already overrided by default when you create a new activity.

Hint: Press Alt+Insert to find the methods that can be overridden.

To print a message use Log Cat:


Log.d(”MainActivity”, "onStart");

Task 3: Create second Activity and set its layout

3.1. Create a new Activity named SecondActivity


3.3.Change the code so that moves the screen to the SecondActivity.

Task 4: Create a new method onSendButton in MainActivity.java, inside it create an intent and and
start the new activity using intent.

4.1. Create object of intent using current view, and explicitly describe the next java file to run.
Hint:
Intent in=new Intent(view.getContext(),SecondActivity.class);
startActivity(in);
4.4.Associate this function to the send Button in the xml file.
Hint:
<Button

android:layout_width="110dp"

android:layout_height="59dp"

android:text="Send"

app:layout_constraintEnd_toEndOf="parent"

app:layout_constraintStart_toStartOf="parent"

app:layout_constraintTop_toBottomOf="@id/Editable"
android:onClick="onSendButton"

/>

Task 4: Send the text from EditText to the second screen and display on the TextView.

4.4.Send data from the EditText

Hint: In MainActivity.java inside onSendButton method, you need to add extra data inside intent
txt1=findViewById(R.id.Editable);

Intent in=new Intent(MainActivity.this,SecondActivity.class);


in.putExtra("msg",txt1.getText().toString());
startActivity(in);

4.5.Receive Data and display on TextView

Hint: In MainActivity.java inside onCreate method, you need to get the intent, and get the Extras
from the intent.
Intent repylyintent=getIntent();

String reply=repylyintent.getExtras().getString("msg");

Task 6: Send data back from second activity to first activity.


Note: If you need a reply back from the next intent, then use
startActivityForResult(in,1); instead of startActivity(in);
Also to receive the reply back override the method.
Hint:
@Override

protected void onActivityResult(int requestCode, int resultCode,


@Nullable Intent data) {

super.onActivityResult(requestCode, resultCode, data);


}

Task 7: Change the label of the app bar in second screen.

Hint: Goto AndroidManifest.xml file and find activity tag for SecondActivity and add a label to the
activity
Hint:
<activity android:name=".SecondActivity"

android:label="Second Screen"></activity>

You might also like