Android App Development | Lecture#28 | Hive Learners

avatar

๐“–๐“ป๐“ฎ๐“ฎ๐“ฝ๐“ฒ๐“ท๐“ฐ๐“ผ

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.

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.


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()

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

It will override the onStart code in our activity like this

public class MainActivity extends AppCompatActivity {

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

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();
        }
    }

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)






0
0
0.000
5 comments
avatar
(Edited)

Here you can find most of apps bugs lol! thanks for bringing it
!1UP

You can earn passive income by delegation of tribe tokens to "The Cartel".

dlmmqb-TheCartel-banner
Click this banner to join "The Cartel" discord server to know more.

0
0
0.000
avatar

Thanks for your contribution to the STEMsocial community. Feel free to join us on discord to get to know the rest of us!

Please consider delegating to the @stemsocial account (85% of the curation rewards are returned).

You may also include @stemsocial as a beneficiary of the rewards of this post to get a stronger support.ย 
ย 

0
0
0.000
avatar

This is so nice! I learned a few things from you since I have a background in programming. I haven't learned this part since we're not yet in app development sir @faisalamin. I'll read your previous posts.

0
0
0.000
avatar

Thank you so much for this 28th edition. I really learnt alot. Following for the next lectures๐Ÿ˜Š

0
0
0.000