Android App Development | Lecture#28 | Hive Learners

Words
355
Reading
2 min
Listen
Play
4y

饾摉饾摶饾摦饾摦饾摻饾摬饾摲饾摪饾摷

Hello dear Hive Learners, Welcome to the 28th lecture on Android App Development. Today we will learn the useful lifecycle of activity in android. These lifecycles are executed before or after the full layout render. We will add up more lifecycles in the future. First, let's keep it simple.

alt

GitHub Link

Use this GitHub project to clone into your directory. It will constantly get updated in the following lecture so you will never miss the latest code. Happy Coding!.

What Should I Learn

  • What is the lifecycle
  • How to use the activity lifecycle

Assignment

  • Try all the lifecycles

Procedure

The first question is what is the lifecycle of an activity in Android. It starts from the start when an activity opens it always triggers the onStart function. At the same time, onResume also runs. If an activity is not finished and we come back to it by any button it will trigger onResume only. If we destroy it the onDestroy trigger is executed. If we change the activity without destroying it will trigger onPause.

alt
image source

Now let's implement these triggers. These triggers are always present in JAVA activity. But to write out custom code in it we need to override these functions. We need to right-click in the Main Activity public class or we can press the Ctrl + O to generate the override. We can use the words to find the override from the long list.

First, we will use the onStart()

alt

alt

We will search for the onStart() and click on it and then press Ok.

alt

It will override the onStart code in our activity like this

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onStart() {
        super.onStart();
    }

alt

Now we will move the logic that we are using to check if the user is already login or not here.


    @Override
    protected void onStart() {
        super.onStart();

        if (!sharedPreferences.getString("login_account", "").equals("")) {
            startActivity(new Intent(MainActivity.this, Welcome_Activity.class));
            finish();
        }
    }

alt

In the same way, we can implement onResume, onDestroy, and onPause.


@Override
protected void onStart() {
super.onStart();
Toast.makeText(this, "Activity Started", Toast.LENGTH_SHORT).show();

    if (!sharedPreferences.getString("login_account", "").equals("")) {
        startActivity(new Intent(MainActivity.this, Welcome_Activity.class));
        finish();
    }
}

@Override
protected void onResume() {
    super.onResume();
    Toast.makeText(this, "Activity Resumed", Toast.LENGTH_SHORT).show();

}

@Override
protected void onDestroy() {
    super.onDestroy();
    Toast.makeText(this, "Activity Destroyed", Toast.LENGTH_SHORT).show();
}

@Override
protected void onPause() {
    super.onPause();
    Toast.makeText(this, "Activity Paused", Toast.LENGTH_SHORT).show();

}

![](https://images.ecency.com/DQmdNkHK8Y2xwczZkdwGgfDgtc62tc4k6LBJWLPWuE5yD9T/image.png)




Android App Development | Lecture#28 | Hive Learners | Ecency