Intent and Lifecycle
Intent and Lifecycle
Intent Classification:
There are two types of intents in android
1. Implicit Intent
2. Explicit Intent
Implicit Intent
Using implicit Intent, components can’t be specified. An action to be performed
is declared by implicit intent. Then android operating system will filter out
components that will respond to the action. For Example,
XML
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/btn"
android:text="Search"
android:onClick="search"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editText" />
</androidx.constraintlayout.widget.ConstraintLayout>
Java
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
@Override
EditText editText;
Button button;
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = findViewById(R.id.btn);
button.setOnClickListener(new View.OnClickListener() {
@Override
String url=editText.getText().toString();
startActivity(intent);
});
}
Android Explicit intent specifies the component to be invoked from activity. In other
words, we can call another activity in android by explicit intent.
We can also pass the information from one activity to another using explicit intent.
Here, we are going to see an example to call one activity from another and vice-versa.
activity_main.xml
File: activity_main.xml
1. <?xml version="1.0" encoding="utf-8"?>
2. <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.androi
d.com/apk/res/android"
3. xmlns:app="http://schemas.android.com/apk/res-auto"
4. xmlns:tools="http://schemas.android.com/tools"
5. android:layout_width="match_parent"
6. android:layout_height="match_parent"
7. tools:context="example.javatpoint.com.explicitintent.FirstActivity">
8.
9. <TextView
10. android:layout_width="wrap_content"
11. android:layout_height="wrap_content"
12. android:layout_marginEnd="8dp"
13. android:layout_marginStart="8dp"
14. android:layout_marginTop="8dp"
15. android:text="First Activity"
16. app:layout_constraintBottom_toBottomOf="parent"
17. app:layout_constraintEnd_toEndOf="parent"
18. app:layout_constraintHorizontal_bias="0.454"
19. app:layout_constraintLeft_toLeftOf="parent"
20. app:layout_constraintRight_toRightOf="parent"
21. app:layout_constraintStart_toStartOf="parent"
22. app:layout_constraintTop_toTopOf="parent"
23. app:layout_constraintVertical_bias="0.06" />
24.
25. <Button
26. android:id="@+id/button"
27. android:layout_width="wrap_content"
28. android:layout_height="wrap_content"
29. android:layout_marginEnd="8dp"
30. android:layout_marginStart="8dp"
31. android:layout_marginTop="392dp"
32. android:onClick="callSecondActivity"
33. android:text="Call second activity"
34. app:layout_constraintEnd_toEndOf="parent"
35. app:layout_constraintStart_toStartOf="parent"
36. app:layout_constraintTop_toTopOf="parent" />
37.
38. </android.support.constraint.ConstraintLayout>
ActivityOne class
File: MainActivityOne.java
1. package example.javatpoint.com.explicitintent;
2.
3. import android.content.Intent;
4. import android.support.v7.app.AppCompatActivity;
5. import android.os.Bundle;
6. import android.view.View;
7.
8. public class FirstActivity extends AppCompatActivity {
9.
10. @Override
11. protected void onCreate(Bundle savedInstanceState) {
12. super.onCreate(savedInstanceState);
13. setContentView(R.layout.activity_first);
14. }
15. public void callSecondActivity(View view){
16. Intent i = new Intent(getApplicationContext(), SecondActivity.class);
17. i.putExtra("Value1", "Android By Javatpoint");
18. i.putExtra("Value2", "Simple Tutorial");
19. // Set the request code to any code you like, you can identify the
20. // callback via this code
21. startActivity(i);
22. }
23.
24. }
activitytwo_main.xml
File: activitytwo_main.xml
1. <?xml version="1.0" encoding="utf-8"?>
2. <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.androi
d.com/apk/res/android"
3. xmlns:app="http://schemas.android.com/apk/res-auto"
4. xmlns:tools="http://schemas.android.com/tools"
5. android:layout_width="match_parent"
6. android:layout_height="match_parent"
7. tools:context="example.javatpoint.com.explicitintent.SecondActivity">
8.
9. <TextView
10. android:layout_width="wrap_content"
11. android:layout_height="wrap_content"
12. android:layout_marginEnd="8dp"
13. android:layout_marginStart="8dp"
14. android:layout_marginTop="8dp"
15. android:text="Second Activity"
16. app:layout_constraintBottom_toBottomOf="parent"
17. app:layout_constraintEnd_toEndOf="parent"
18. app:layout_constraintHorizontal_bias="0.454"
19. app:layout_constraintLeft_toLeftOf="parent"
20. app:layout_constraintRight_toRightOf="parent"
21. app:layout_constraintStart_toStartOf="parent"
22. app:layout_constraintTop_toTopOf="parent"
23. app:layout_constraintVertical_bias="0.06" />
24.
25. <Button
26. android:id="@+id/button"
27. android:layout_width="wrap_content"
28. android:layout_height="wrap_content"
29. android:layout_marginEnd="8dp"
30. android:layout_marginStart="8dp"
31. android:layout_marginTop="392dp"
32. android:onClick="callFirstActivity"
33. android:text="Call first activity"
34. app:layout_constraintEnd_toEndOf="parent"
35. app:layout_constraintStart_toStartOf="parent"
36. app:layout_constraintTop_toTopOf="parent" />
37. </android.support.constraint.ConstraintLayout>
ActivityTwo class
File: MainActivityTwo.java
1. package example.javatpoint.com.explicitintent;
2.
3. import android.content.Intent;
4. import android.support.v7.app.AppCompatActivity;
5. import android.os.Bundle;
6. import android.view.View;
7. import android.widget.Toast;
8.
9. public class SecondActivity extends AppCompatActivity {
10.
11. @Override
12. protected void onCreate(Bundle savedInstanceState) {
13. super.onCreate(savedInstanceState);
14. setContentView(R.layout.activity_second);
15. Bundle extras = getIntent().getExtras();
16. String value1 = extras.getString("Value1");
17. String value2 = extras.getString("Value2");
18. Toast.makeText(getApplicationContext(),"Values are:\n First value: "+value1+
19. "\n Second Value: "+value2, Toast.LENGTH_LONG).show();
20. }
21. public void callFirstActivity(View view){
22. Intent i = new Intent(getApplicationContext(), FirstActivity.class);
23. startActivity(i);
24. }
25.
26. }
Android Activity Lifecycle
By the help of activity, you can place all your UI components or widgets in a single
screen.
The 7 lifecycle method of Activity describes how activity will behave at different states.
Method Description
onResume called when activity will start interacting with the user.
import android.os.Bundle;
import android.widget.Toast;
@Override
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
super.onStart();
@Override
super.onRestart();
super.onPause();
Toast toast = Toast.makeText(getApplicationContext(), "onPause Called",
Toast.LENGTH_LONG).show();
super.onResume();
super.onStop();
super.onDestroy();