--> android source code full: Life cycle | Deskripsi Singkat Blog di Sini

Find The Complete Source Code Android


Showing posts with label Life cycle. Show all posts
Showing posts with label Life cycle. Show all posts

Sunday, December 3, 2017

Life Cycle of Activity and Application

Life Cycle of Activity and Application

Life Cycle tutorial covered the all information about the activity and application life cycle .






Steps to use Life Cycle tutorial in your project.

  1. Crate a project(LifeCycleDemo) in android studio.

  2. Create a class MyApplication.Java and update it-


    package com.pankaj.lifecycledemo;

    import android.app.Application;
    import android.content.res.Configuration;
    import android.util.Log;

    /**
    * Created by gsolc on 29/11/17.
    */

    public class MyApplication extends Application {
    String TAG="lifecycledemoTag";

    @Override
    public void onCreate() {
    super.onCreate();
    Log.e(TAG, "MyApplication onCreate");
    }

    @Override
    public void onLowMemory() {
    super.onLowMemory();
    Log.e(TAG, "MyApplication onLowMemory");

    }

    @Override
    public void onTerminate() {
    super.onTerminate();
    Log.e(TAG, "MyApplication onTerminate");

    }

    @Override
    public void onTrimMemory(int level) {
    super.onTrimMemory(level);
    Log.e(TAG, "MyApplication onTrimMemory");

    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    Log.e(TAG, "onConfigurationChanged");
    }

    }

      
  3. Create a class MainActivity.Java and update it-


    package com.pankaj.lifecycledemo;

    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.util.Log;

    public class MainActivity extends AppCompatActivity {
    String TAG="lifecycledemoTag";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Log.e(TAG, "MainActivity onCreate");
    }

    @Override
    protected void onDestroy() {
    super.onDestroy();
    Log.e(TAG, "MainActivity onDestroy");
    }

    @Override
    public void onBackPressed() {
    super.onBackPressed();
    Log.e(TAG, "MainActivity onBackPressed");
    }

    @Override
    protected void onPause() {
    super.onPause();
    Log.e(TAG, "MainActivity onPause");
    }

    @Override
    protected void onResume() {
    super.onResume();
    Log.e(TAG, "MainActivity onResume");
    }

    @Override
    protected void onStart() {
    super.onStart();
    Log.e(TAG, "MainActivity onStart");
    }

    @Override
    protected void onRestart() {
    super.onRestart();
    Log.e(TAG, "MainActivity onRestart");
    }
    }

     
  4. Open your main_activity.xml file and update it-
  5. <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.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="com.pankaj.lifecycledemo.MainActivity">

    <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World!"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

    </android.support.constraint.ConstraintLayout>
  6. All the application development process with Life Cycle  has completed, Now run the app and look the screen. 







  7. Good bye, Thanks to read this blog.