Activity Life Cycle
Activity Life Cycle
Ans:
ACTIVITIES
An Android activity is one screen of the Android app's user interface.
In that way an Android activity is very similar to windows in a
desktop application.
An Android app may contain one or more activities, meaning one
or more screens.
The Android app starts by showing the main activity, and from there
the app may make it possible to open additional activities.
Unlike programming paradigms in which apps are launched with a
main () method, the Android system initiates code in an activity
instance by invoking specific callback methods that correspond to
specific stages of its lifecycle.
The activity base class defines s series of event that govern the life
cycle of an activity.
i. onCreate () - This is the first callback and called when the
activity is first created.
ii. onStart () - This callback is called when the activity becomes
visible to the user.
iii. onResume () -This is called when the user starts interacting
with the application.
iv. onPause()-The paused activity does not receive user input
and cannot execute any code and called when the current
activity is being paused and the previous activity is being resumed.
v. onStop ()-This callback is called when the activity is no longer
visible.
vi. onDestroy()-This callback is called before the activity is destroyed
by the system.
vii. onRestart()—Called when the activity has been stopped and is
restarting again
android: theme="@style/Theme.Material3.Dark“
3. Explain the steps to hide an activity in an android application, with an
example.
Ans:
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Window;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
requestWindowFeature(Window.FEATURE_NO_TITLE);
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.myapplication" >
<application
android:allowBackup="true“
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name“
android:supportsRtl="true“
android:theme="@android:style/Theme.NoTitleBar”>
Ans:
<application ...>
<activity android:name=".MainActivity"/>
<activity android:name=".SecondActivity"/>
</application>
This activity contains a button. On clicking the button, it will launch the
SecondActivity.
Java (MainActivity.java):
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;
@Override
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnLaunch.setOnClickListener(new View.OnClickListener() {
@Override
startActivity(intent);
});
The second activity retrieves the message sent from the first activity and
displays it.
Java (SecondActivity.java):
import android.os.Bundle;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
@Override
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
textView.setText(message);
}
4. Design the Layouts
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp">
<Button
android:id="@+id/btnLaunch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
</RelativeLayout>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
</RelativeLayout>
Explanation
1. Creating an Intent:
In MainActivity, we create an Intent object to specify the target activity
(Second Activity).
2. Passing Data:
Using intent.putExtra(), we pass a message from MainActivity to
SecondActivity.
3. Retrieving Data:
In SecondActivity, the data is retrieved using
getIntent().getStringExtra().
Output
This is how you can launch an activity and pass data between activities in
Android.
Ans:
LinearLayout in Android
A LinearLayout is a ViewGroup in Android that arranges its child views
linearly either in a horizontal or vertical direction. This layout
organizes its elements one after the other, either row-wise or column-
wise, depending on the orientation attribute.
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button 1"
android:layout_marginEnd="8dp" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button 2"
android:layout_marginStart="8dp" />
</LinearLayout>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp" />
</LinearLayout>
i. Parent LinearLayout:
ii. TextView:
iv. EditText:
Output
An EditText appears at the bottom where the user can enter text.
This layout is simple and useful for scenarios where child views need
to be arranged in a single row or column.
Ans:
RelativeLayout in Android
layout_alignParentTop,layout_alignParentBottom,
layout_centerInParent: Align a child relative to the parent
layout.
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
android:background="#F5F5F5">
<TextView
android:id="@+id/headingText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Welcome to RelativeLayout"
android:textSize="20sp"
android:textColor="#333"
android:layout_centerHorizontal="true"
android:layout_marginTop="32dp" />
<ImageView
android:id="@+id/sampleImage"
android:layout_width="150dp"
android:layout_height="150dp"
android:src="@android:drawable/ic_dialog_info"
android:layout_below="@id/headingText"
android:layout_centerHorizontal="true"
android:layout_marginTop="16dp" />
<Button
android:id="@+id/actionButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click Me"
android:layout_below="@id/sampleImage"
android:layout_centerHorizontal="true"
android:layout_marginTop="24dp" />
</RelativeLayout>
1. Parent RelativeLayout:
2. TextView:
3. ImageView:
4. Button:
o Positioned below the ImageView using
layout_below="@id/sampleImage".
Output
Advantages:
Disadvantages:
May become hard to manage if the layout has too many children.