I have googled but I have not found an equivalent flowchart showing the life cycle of PreferenceActivities in android. Does such a flowchart exist? The one showing basic activity lifecycle was a big help getting to understand how activities work.
The PreferenceActivity class extends the Activity class¹, hence the same lifecycle applies.
A PreferenceActivity just does some things for you in the relevant lifecycle methods, see the source. Since you have to call the relevant superclass methods anyway, there isn't any major difference (e.g. you have to call super.onPause() when you override onPause()).
¹ it extends a ListActivity, which then extends the Activity class
Related
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?
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?
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
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
I was following Android tutorials from mybringback and he was using his created class and it was, by default, extended to use Activity and since that video was made a while ago, I'm guessing that ActionBarActivity wasn't available then.
However in the tutorial he uses super.onPause in his media file video, which is not available in the override methods for ActionBarActivity, so I was wondering, if there was another way for me to do the same thing, if onPause would be called something else in ActionBarActivity, or if I should just change ActionBarActivity to Activity instead.
Thanks!
In eclipse if you want to use Source->Override/Implement Methods, to generate the onPause method, you need to look under the FragmentActivity expansion, since ActionBarActivity is a subclass of FragmentActivity
ActionBarActivity subclasses FragmentActivity
FragmentActivity subclasses Activity
Activity contains method onPause()
Therefore, yes! There is an onPause() method in ActionBarActivity
You should really learn to use documentation and not rely on override implement methods feature in Eclipse https://developer.android.com/reference/android/support/v7/app/ActionBarActivity.html
I need to design a timeout activity for my android application
These are my concerns-
From every activity in the app the timeout would be 90 mins.
On any activity in the app the timer should reset itself.
there would be a authentication screen thereafter and it would return to the last activity
upon success
What I need is a generic design as I may have parent-child relationship and would apply on events like onUserInteraction(),onPause() onStop() but it may result in lot of redundant coding and lot of methods to be taken care of.
Can I use services or any cross-cutting features like aspects?
Implement one abstract class that extends your intended super class (i.e., Activity).
Implement your "timeout" concept in that abstract class's onUserInteraction(), onPause(), onStop() methods.
Extend that abstract class in your actual "worker" classes.
Let's say I have two classes:
Class Show is my normal Android activity.
Class Work is a Java class which does a lot of work:
public class Work extends Activity {...}
As you can see, Work extends Activity. Thats because it needs some methods that are only in Activity (I don't mean methods which regard the UI).
In my Activity Show I make an Objekt of class Work
protected void onCreate(){
Work mWork = new Work();
mWork.doSomething();
}
My question:
How shall I handle my object Work regarding its lifecycle? Is it like a normal Java object and I don't have to care about its lifecycle, or is it like a normal activity and i have to call finish()? I am confused because it's kind of both.
Based on our discussion in the comments, the answer here would be to pass the Context of your current activity to the target class, and use the accessible methods through that context instance to do whatever task it is you want, as the Activity class basically extends ApplicationContext.
For example (in a class not extending Activity) I can add:
myLocationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
and use myLocationManager as I please. Also, this way the object created locally would be set for garbage collection upon destruction of your main Activity.
Thought it'd be worth having an answer others could refer to rather than scrounging through the comments.
you should read this to understand activity lifecycle.
Why is it that you need to call the super class in the android lifecycle? For example, in onCreate you need to call super.onCreate, or onDestroy super.onDestroy.
It makes sure that any relevant lifecycle management code across the full class hierarchy gets invoked.
If you have MyBaseActivity that extends Activity, and MySpecificActivity that extends MyBaseActivity, calling through to the lifecycle methods in the superclass at each level means MyBaseActivity will still be able to respond to lifecycle events.