0

Android activity and fragment lifecycles have many stages (onCreate(), onStart(), onResume(), onPause(), onStop(), onDestroy(), etc). I have been coding in Android Studio for a few months now and there're many times where I haven't used all of lifecycle methods.

My question is that do you need to use all of the lifecycle methods of a fragment or an activity to write a good code? Will it cause crashes otherwise?

Zack
  • 347
  • 3
  • 16

3 Answers3

1

Nope. You can override those methods to add more functionality to your app but those methods already have their own function and will run whether you override it or not.

You could read more on the Android Activity Life Cycle: http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle

You could see this post as well: Android activity life cycle - what are all these methods for?

Community
  • 1
  • 1
Kurt Acosta
  • 1,935
  • 1
  • 11
  • 28
1

No, You dont need to write all the lifecycle. But you should have the idea of what lifecycle is going on and what will be the behaviour of Android app. Like why you have to attach activity context to fragment context in onAttach() life cycle method.

What lifecycle will be perform on dialog open or moving from one activity to another??

Read here more.

https://developer.android.com/reference/android/app/Activity.html

Avinash Verma
  • 1,792
  • 14
  • 22
0

Not all, only methods you thing is essential for your task. see docs on Activity's lifecycle: https://developer.android.com/reference/android/app/Activity.html

Alex Shutov
  • 3,128
  • 2
  • 11
  • 11