In this tutorial, we will start another activity from an activity and finish it with transition animation.
|
1. What is Activity
If I give the definition of activity from developer.android.com :
“An Activity
is an application component that provides a screen with which users can interact in order to do something, such as dial the phone, take a photo, send an email, or view a map.”
So, as I said before, you can assume activity as an application page-screen which is used by users to do somethings. But do not think that every different screen is an activity. We will see fragments in next lessons.
For example, in previous tutorials, Showing Toast on Button Click and Making Switch On/Off Button, every page is an activity.
How we define a class as an activity ? Open previous tutorials and see MainActivity.java classes. Those classes extends Activity
. So this is how we do.
2. Activity Lifecycle
Every activity has a standart lifecycle. It starts, pauses, resumes, finishes…etc. with some spesific methods. Thanks to these methods, we can define somethings, do some configurations or release-destroy somethings at the right points. To sum up, we arrange how ui looks to users and decide reactions when users interact with ui ON THESE LIFECYCLE METHODS. Lets look what they are and when they active.
Below image is taken from here
So when an activity starts with onCreate()
method. We can define, bind all view elements of layout and set some listeners to them in onCreate()
scope.
Remember, in previous tutorials, we always do in this way.
What about other methods ? When an activity comes on device screen, respectively onCreate->onStart->onResume
methods run and it becomes visible to user. When user press back button to close application , it follows onPause->onStop->onDestroy
.
You can examine below to understand better.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
public class ExampleActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // The activity is being created. } @Override protected void onStart() { super.onStart(); // The activity is about to become visible. } @Override protected void onResume() { super.onResume(); // The activity has become visible (it is now "resumed"). } @Override protected void onPause() { super.onPause(); // Another activity is taking focus (this activity is about to be "paused"). } @Override protected void onStop() { super.onStop(); // The activity is no longer visible (it is now "stopped") } @Override protected void onDestroy() { super.onDestroy(); // The activity is about to be destroyed. } } |
Thats enough to go on our actual tutorial.
3. Starting and Finishing Activities
Lets do something. Open an Android Studio project, if you forget how,follow this.
- We start with defining some resources which we will use.Add below string items to
res/values/strings.xml
1234<string name="main_activity_button_text">Start Second Activity</string><string name="second_activity_button_text">Finish This Activity</string><string name="main_activity_text">This is first activity</string><string name="second_activity_text">This is second activity</string>
Add below color items tores/values/colors.xml
12<color name="main_activity_back_color">#deda78</color><color name="second_activity_back_color">#de7878</color>
- Go on with designing our layout. Open your
res/layout/activity_main.xml
and write below code.
1234567891011121314151617181920212223242526<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"android:gravity="center"android:background="@color/main_activity_back_color"><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginLeft="20dp"android:layout_marginRight="20dp"android:gravity="center_horizontal"android:textSize="20sp"android:text="@string/main_activity_text"/><Buttonandroid:id="@+id/main_activity_button"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="10dp"android:padding="10dp"android:text="@string/main_activity_button_text"/></LinearLayout>
- Above layout design contains a textview and button. You can check with switching to design mode.
- Our goal is starting another activity with clicking that button.
Open your MainActivity class. Bind the button with its id and add a listener to detect click event.
1234567891011121314151617181920212223242526272829303132import android.app.Activity;import android.os.Bundle;import android.view.View;import android.widget.Button;public class MainActivity extends Activity {private Button button;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//Bind button by idbutton=(Button)findViewById(R.id.main_activity_button);//Set a click listener to catch click on this buttonbutton.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {//Call the function which starts second activitystartSeconActivity();}});}private void startSeconActivity(){}}
- This bring us to defining another activity. Click right on your package and create a class named SecondActivity
- Remember, there must be a layout file for an activity. So click right on your
res/layout
folder and create a layout file named activity_second
- Write below code to
res/layout/activity_second.xml
. This contains a textview and a button also. You can check switching to design mode.
1234567891011121314151617181920212223242526<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"android:gravity="center"android:background="@color/second_activity_back_color"><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginLeft="20dp"android:layout_marginRight="20dp"android:gravity="center_horizontal"android:textSize="20sp"android:text="@string/second_activity_text"/><Buttonandroid:id="@+id/second_activity_button"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="10dp"android:padding="10dp"android:text="@string/second_activity_button_text"/></LinearLayout>
- Our goal is finishing SecondActivity with clicking that button.
Open yourSecondActivity
class and write below code.
123456789101112131415161718192021222324252627import android.app.Activity;import android.os.Bundle;import android.view.View;import android.widget.Button;public class SecondActivity extends Activity{private Button button;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_second);//Bind button by idbutton=(Button)findViewById(R.id.second_activity_button);//Set a click listener to catch click on this buttonbutton.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {//Finish this activity here}});}}
- So we have a SecondActivity now, it seems we need write the code which starts second activity from main activity but there is one important thing to do.
- Right now, we are ready to start SecondActivity. Open your
MainActivity
class and fillstartSecondActivity()
method like below. You need to Intent object to start SecondActivity.123456private void startSeconActivity(){//Below code starts SecondActivity.//You need to create an intent to do this.Intent intent = new Intent(MainActivity.this,SecondActivity.class);startActivity(intent);} - Run application and click button, see the SecondActivity begins.
- Next goal is finishing the SecondActivity and back to the MainActivity.
Open yourSecondActivity
and fill the click listener scope of button like below.1234567button.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {//Call finish() method to finish second activityfinish();}}); - Run application again and start SecondActivity, then click button on SecondActivity and see it finishes. After finishing our MainActivity comes again.
4. Animation Between Activity Transitions
At this chapter, we will add an animation effect to activity transitions, forward and backward.
- We need to define animation resources and these resources must be at
anim
folder underres
. So,click right on res and create a folder named anim.
- In anim folder, we create four different animation resources. Click right and create animation resource file four times.
- Animation resource files names is up to you. I choose below names.
activity_enter_left_to_right.xml
activity_enter_right_to_left.xml
activity_exit_left_to_right.xml
activity_exit_right_to_left.xml
- Write below codes for each animation resource.
activity_enter_left_to_right
12345<?xml version="1.0" encoding="utf-8"?><translate xmlns:android="http://schemas.android.com/apk/res/android"android:fromXDelta="-100%"android:toXDelta="0%"android:duration="500"/>
activity_enter_right_to_left
12345<?xml version="1.0" encoding="utf-8"?><translate xmlns:android="http://schemas.android.com/apk/res/android"android:fromXDelta="100%"android:toXDelta="0%"android:duration="500"/>
activity_exit_left_to_right
12345<?xml version="1.0" encoding="utf-8"?><translate xmlns:android="http://schemas.android.com/apk/res/android"android:fromXDelta="0%"android:toXDelta="100%"android:duration="500"/>
activity_exit_right_to_left
12345<?xml version="1.0" encoding="utf-8"?><translate xmlns:android="http://schemas.android.com/apk/res/android"android:fromXDelta="0%"android:toXDelta="-100%"android:duration="500"/>
- Our animation resources are ready. Lets implement them on transitions.
Open yourSecondActivity
and addoverridePendingTransition
method like below.
12345678910111213141516171819202122232425262728293031import android.app.Activity;import android.os.Bundle;import android.view.View;import android.widget.Button;public class SecondActivity extends Activity{private Button button;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_second);//Define transition animationoverridePendingTransition(R.anim.activity_enter_right_to_left, R.anim.activity_exit_right_to_left);//Bind button by idbutton=(Button)findViewById(R.id.second_activity_button);//Set a click listener to catch click on this buttonbutton.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {//Call finish() method to finish second activityfinish();}});}}
- Run application, start second activity clicking button and see translate animation from main activity to second activity.
- But when you press back button, there is no animation from second to main. We need to declare a translate animation also when main is back. Open your
MainActivity
and overrideonResume
method like below.
12345678910111213141516171819202122232425262728293031323334353637383940414243import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.widget.Button;public class MainActivity extends Activity {private Button button;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//Bind button by idbutton=(Button)findViewById(R.id.main_activity_button);//Set a click listener to catch click on this buttonbutton.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {//Call the function which starts second activitystartSeconActivity();}});}@Overrideprotected void onResume() {super.onResume();//Define transition animation when this activity is backoverridePendingTransition(R.anim.activity_enter_left_to_right, R.anim.activity_exit_left_to_right);}private void startSeconActivity(){//Below code starts SecondActivity.//You need to create an intent to do this.Intent intent = new Intent(MainActivity.this,SecondActivity.class);startActivity(intent);}}Note Remember above image of the activity lifecycle. When starting B activity from A activity, A activity paused. If B activity is finished, A activity comes to screen again with running its onResume method. At this point, we can declare an animation for transition B to A.
Run application and try forward backward animations. Thats all see you next tutorials.
![]() If you don’t know how to open an existing Android Studio Project, follow this tutorial. |