Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Android Activity life cycle

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 172
    Comment on it

    In the below example I have created Activity life cycle. Activity start's with a call on onCreate() callback method. There is a sequence of callback methods that start's an activity and a sequence of callback methods. See the below example for detail.


    . onCreate() - called before the first components of the application starts

    . onStart()- Called when the activity is becoming visible to the user.

    . onPause() - Called when the system is about to start resuming a previous activity. This is
    typically used to commit unsaved changes to persistent data, stop animations and other things
    that may be consuming CPU, etc. 

    . onStop()- Called when the activity is no longer visible to the user, because another
    activity has been resumed and is covering this one. This may happen either
    because a new activity is being started, an existing one is being brought in front of
    this one, or this one is being destroyed.

    . onDestory()-The final call you receive before your activity is destroyed. This can happen either because the activity is finishing (someone calledfinish() on it.
    Program-

    public class FindnerdActivity extends Activity {
    
      @Override
      protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        notify("onCreate");
      }
    
      @Override
      protected void onPause() {
        super.onPause();
        notify("onPause");
      }
    
      @Override
      protected void onResume() {
        super.onResume();
        notify("onResume");
      }
    
      @Override
      protected void onStop() {
        super.onStop();
        notify("onStop");
      }
    
      @Override
      protected void onDestroy() {
        super.onDestroy();
        notify("onDestroy");
      }
    
      @Override
      protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        notify("onRestoreInstanceState");
      }
    
      @Override
      protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        notify("onSaveInstanceState");
      }
    
      private void notify(String methodName) {
        String name = this.getClass().getName();
        String[] strings = name.split("\\.");
        Notification noti = new Notification.Builder(this)
         .setContentTitle(methodName + " " + strings[strings.length - 1]).setAutoCancel(true)
         .setSmallIcon(R.drawable.ic_launcher)
         .setContentText(name).build();
        NotificationManager notificationManager = 
            (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        notificationManager.notify((int) System.currentTimeMillis(), noti);
      }
    
    } 
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: