2

I am trying to understand the actual concepts of the lifecycle methods of an Activity.

I am focusing on only primary lifecycle methods as mentioned below

onCreate()
onStart()
onResume()
onPause()
onStop()
onDestroy()

Can someone please explain a scenario where we can skip a method and rest lifecycle works perfectly fine?

E.g. if I swipe up and kill the application, onPause() and onStop() methods will be skipped and only the onDestroy() method will execute.

Onik
  • 19,396
  • 14
  • 68
  • 91
  • Why? You don't need to override those methods, and they're empty in the Activity base class. – TheWanderer Sep 06 '18 at 20:16
  • Yes i am aware of their being overridden from base class, I am searching for real life scenarios where we can explore the skipping of those methods. – Tushar Srivastava Sep 06 '18 at 20:19
  • I don't think that exists. I'll have to check the source though. – TheWanderer Sep 06 '18 at 20:21
  • It actually exist, as by coding we can add finish() in on create and all other methods will be skipped except on destroy(), If this is possible via coding then there will be a scenario to replicate this situation in real life also. That is what i am looking for :) – Tushar Srivastava Sep 06 '18 at 20:26
  • 2
    @Tushar That isn't skipping a method. Its never starting them. The Activity Lifecycle is a state machine with defined transitions. None of those transitions allow you to skip a state, except in the case of abnormal termination. You're thinking of things totally wrong by thinking of it as skipping, which will leave you a confused view of the world. – Gabe Sechan Sep 06 '18 at 20:27

3 Answers3

4

you cannot skip those methods, because when being implemented, they must call to their super class eg. AppCompatActivity - and when not being implemented, the will nevertheless be triggered within the super class. just see the documentation: Understand the Activity Lifecycle.

Activity Lifecycle

Martin Zeitler
  • 1
  • 19
  • 155
  • 216
3

Can someone please explain a scenario where we can skip a method and rest lifecycle works perfectly fine.

It never can happen unless you manually call finish(), say, in onCreate() which is quite pointless. If a lifecycle method is "skipped" by the system (and the only scenario when it happens is an app process being killed) the rest of the methods are also "skipped".

If I swipe up and kill the application, onPause() and onStop() methods will be skipped and only onDestroy() method will execute.

Again, it never can happen by the reason described above.

Onik
  • 19,396
  • 14
  • 68
  • 91
0

onDestroy() method never calls when you swipte up and kill the application.

Erselan Khan
  • 692
  • 6
  • 13