What is Activity ? Starting Another Activity and Finishing It (Bonus: with an Animation)

In this tutorial, we will start another activity from an activity and finish it with transition animation.

  1. What is Activity ?
  2. Activity Lifecycle
  3. Starting and Finishing Activities
  4. Animation Between Activity Transitions

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.

Screen Shot 2015-10-25 at 11.48.47

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

activity_lifecycle

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.

Screen Shot 2015-10-25 at 12.08.59

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.

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

    Add below color items to res/values/colors.xml

     
  • Go on with designing our layout. Open your res/layout/activity_main.xml and write below code.

     
  • Above layout design contains a textview and button. You can check with switching to design mode.
    Screen Shot 2015-10-25 at 12.45.06

 

 

  • 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.

     
  • This bring us to defining another activity. Click right on your package and create a class named SecondActivityScreen Shot 2015-10-25 at 12.53.36 Screen Shot 2015-10-25 at 12.53.50

 

  • 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_secondScreen Shot 2015-10-25 at 12.58.18 Screen Shot 2015-10-25 at 12.58.54

 

  • Write below code to res/layout/activity_second.xml. This contains a textview and a button also. You can check switching to design mode.

    Screen Shot 2015-10-25 at 13.02.35

     

 

  •  Our goal is finishing SecondActivity with clicking that button.
    Open your SecondActivity class and write below code.

     
  • 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.
    Note

    If we create an activity, we need to declare it in AndroidManifest.xml. You can find it here.
    Screen Shot 2015-10-25 at 13.09.49


    In AndroidManifest.xml , you need to find an activity declaration. (It doesnt have to be the same just pay attention <activity> …. </activity> tag).

    This activity declaration is for MainActivity


    Declare SecondActivity below MainActivity declaration like this:


    You can ask yourself that what <intent-filter> tag is. Android applications may have lots of activities. So the system needs to know which activity is the first activity which comes to screen when application starts. So <intent-filter></intent-filter> indicates the MainActivity as the first activity of application.

     

  • Right now, we are ready to start SecondActivity. Open your MainActivity class and fill startSecondActivity() method like below. You need to Intent object to start SecondActivity.

     

  • Run application and click button, see the SecondActivity begins.
  • Next goal is finishing the SecondActivity and back to the MainActivity.
    Open your SecondActivity and fill the click listener scope of button like below.

     

  • 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 under res. So,click right on res and create a folder named anim.
    Screen Shot 2015-10-26 at 15.24.47
    Screen Shot 2015-10-26 at 15.26.19

 

  • In anim folder, we create four different animation resources. Click right and create animation resource file four times.
    Screen Shot 2015-10-26 at 15.29.21

 

  • 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

 

  • So anim folder should look after creatings.
    Screen Shot 2015-10-26 at 15.33.48

 

  • Write below codes for each animation resource.
    activity_enter_left_to_right

    activity_enter_right_to_left

    activity_exit_left_to_right

    activity_exit_right_to_left

     
  • Our animation resources are ready. Lets implement them on transitions.
    Open your SecondActivity and add overridePendingTransition method like below.

     
  • 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 override onResume method like below.

    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.

download_source_icon Download Source Code
If you don’t know how to open an existing Android Studio Project, follow this tutorial.

Bir Cevap Yazın