Android Tutorial – Android Activity Lifecycle




Android Tutorial
Android Tutorial

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

 


Partner Sites

VideoToGifs.com

EasyOnlineConverter.com

SqliteTutorials.com





Be the first to comment

Leave a Reply

Your email address will not be published.


*