Professional Documents
Culture Documents
Mobile
Programming
LESSON 8
Application
Components and
Lifecycle
Lifecycle in Android Architecture Components
1. Lifecycle
2. Lifecycle Owner
3. Lifecycle Observer
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
Answer 2
public class MainActivity extends AppCompatActivity {
private EditText nameEditText;
private Button greetButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
nameEditText = findViewById(R.id.nameEditText);
greetButton = findViewById(R.id.greetButton);
Answer 2
greetButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name = nameEditText.getText().toString().trim();
if (!name.isEmpty()) {
String message = "Hello, " + name + "!";
Toast.makeText(MainActivity.this, message, Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "Please enter your name",
Toast.LENGTH_SHORT).show();
}
}
});
}
}
Answer 2
Question 3
Create an Android application for a calculator. The
application should include text fields where the user
can input two numbers, and buttons for addition,
subtraction, multiplication, and division operations.
When the user clicks on one of the operation
buttons, the result should be displayed in a text field.
Answer 3
Answer 3
Answer 3
Answer 3
Answer 3
Question 4
What is the purpose of an Intent in Android
programming?
Answer 4
An Intent in Android programming is used to facilitate communication between
● https://developer.android.com/guide/components/fundamentals
● https://developer.android.com/guide/topics/permissions/overview
Thanks for listening…