Android Activity Lifecycle is controlled by 7 techniques for android.app.Activity class. The android Activity is the subclass of ContextThemeWrapper class.
A movement is the single screen in android. It resemble window or edge of Java. By the assistance of action, you can put all your UI parts or gadgets in a solitary screen. The 7 lifecycle technique for Activity depicts how action will act at various states.
You should always call up to your superclass when implementing these methods.
public class Activity extends ApplicationContext { protected void onCreate(Bundle savedInstanceState); protected void onStart(); protected void onRestart(); protected void onResume(); protected void onPause(); protected void onStop(); protected void onDestroy(); }
Activity Lifecycle
■ Activities in the foreground are running (onResume)
■ If something transparent obscurs the activity it is paused (onPause)
■ If you can’t see the activity it is stopped. (onStop)
■ If it is garbage collected (onDestroy)
Android Activity
Life Cycle
- Lifetime:
- onCreate() to
- onDestroy()
- Visible when:
- onStart() to
- onStop()
- Foreground
- onResume to
- onPause()
Android Activity Lifecycle Example
In this example, we are displaying the content on the logcat.
File: MainActivity.java
package com.example.activitylifecycle; import android.os.Bundle; import android.app.Activity; import android.util.Log; import android.view.Menu; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Log.d("lifecycle","onCreate invoked"); } @Override protected void onStart() { super.onStart(); Log.d("lifecycle","onStart invoked"); } @Override protected void onResume() { super.onResume(); Log.d("lifecycle","onResume invoked"); } @Override protected void onPause() { super.onPause(); Log.d("lifecycle","onPause invoked"); } @Override protected void onStop() { super.onStop(); Log.d("lifecycle","onStop invoked"); } @Override protected void onRestart() { super.onRestart(); Log.d("lifecycle","onRestart invoked"); } @Override protected void onDestroy() { super.onDestroy(); Log.d("lifecycle","onDestroy invoked"); } }
Android Books To Learn Mobile Apps Programming
- Android Programming: The Big Nerd Ranch Guide
- Android Design Patterns: Interaction Design Solutions for Developers
- Android Application Development Cookbook – Second Edition
- Android User Interface Design: Turning Ideas and Sketches into Beautifully Designed Apps (Usability)
- Android Recipes: A Problem-Solution Approach for Android 5.0
- Hello, Android: Introducing Google’s Mobile Development Platform (Pragmatic Programmers)
- Beginning Android Games
Leave a Reply