As I am new to android, trying to learn the basics in detail. Basically I am an iOS programmer. I would like to know how the "Activity in android" is compared with iOS. That means, what is Activity in android when comparing with iOS.
Thanks in advance.
Check out the Documentation for Activity All of these are in there, and many of them contain more detail than what I've listed here.
1.This hook is called whenever the content view of the screen changes (due to a call to Window.setContentView or Window.addContentView).
2.Called when activity start-up is complete (after onStart() and onRestoreInstanceState(Bundle) have been called).
3.Called when activity resume is complete (after onResume() has been called).
4.This hook is called whenever the window focus changes.
5.Called as part of the activity lifecycle when an activity is about to go into the background as the result of user choice. For example, when the user presses the Home key, onUserLeaveHint() will be called, but when an incoming phone call causes the in-call Activity to be automatically brought to the foreground, onUserLeaveHint() will not be called on the activity being interrupted. In cases when it is invoked, this method is called right before the activity's onPause() callback. This callback and onUserInteraction() are intended to help activities manage status bar notifications intelligently; specifically, for helping activities determine the proper time to cancel a notfication.
6.Called whenever a key, touch, or trackball event is dispatched to the activity.
7.Called when the window has been attached to the window manager.
Although there is no 1:1 match for an Activity in iOS SDK, UIViewController is the closest match.
Beginning Android for iOS Developers can help you to cover some ground in writing your first Android app.
The Activity in Android is like the controller in iOS. It receives UI events, interacts with the data model, and updates the UI.
better to read this :
http://developer.android.com/reference/android/app/Activity.html
Related
I learning Android about 6 month, and all this time i described Lifecycle methods like some kind of callback, which "trigerred" when OS change state or configuration.
Questions:
1) For example, User rotate the screen. As i think, Android invoce some interface and Activity runing it's code when callback come. I'm not sure about it, because i got throw documentation(Activity/Window/...classes) and i did't find this interface or smth like it. What's really happen when user rotate the screen?
2) For example, user push the button on his mobile app and create new Activity. Lifecycle methods will be called, but OS state or configuration was't change. It means, my definition of lifecycle methods is wrong. How to describe it correctly? Help me to understand what's wrong with my first definition.
I know about this link:
https://developer.android.com/guide/components/activities/activity-lifecycle
And there is a line:
The Activity class provides a number of callbacks that allow the activity to know that a state has changed...
But inside Activity class i see, for example:
#MainThread
#CallSuper
protected void onCreate(#Nullable Bundle savedInstanceState) {
and i have not see override word, so how Activity can implement it?
There is #CallSuper annotation, but parent classes(include Context) have no code connected with lifecycle. So it this real callbacks?
Not sure that I understand your question, because it is vague, but let me try to address the 2 specific examples:
1) For example, User rotate the screen. As i think, Android invoce
some interface and Activity runing it's code when callback come. I'm
not sure about it, because i got throw
documentation(Activity/Window/...classes) and i did't find this
interface or smth like it. What's really happen when user rotate the
screen?
In the usual case, when the user rotates the screen, a "configuration change" occurs. Android responds to this by killing off the currently active Activity. Android calls the "lifecycle methods": onPause(), onStop() and onDestroy() on the current Activity. Android then creates a new instance of the Activity and calls the "lifecycle methods": onCreate(), onStart() and onResume on that instance.
In case the application has declared that it wants to handle the configuration change itself (by specfying android:configChanges="..." for the Activity in the manifest), Android does not kill the Activity when the screen is rotated. Instead, Android calls the "lifecycle method": onConfigurationChanged() on the current Activity.
2) For example, user push the button on his mobile app and create new
Activity. Lifecycle methods will be called, but OS state or
configuration was't change. It means, my definition of lifecycle
methods is wrong. How to describe it correctly? Help me to understand
what's wrong with my first definition.
The "lifecycle methods" do not only refer to changes int he OS state or configuration. "Lifecycle methods" are also called to inform your Activity about changes in the state of the Activity itself. onCreate() is called to inform the Activity that a new instance of the Activity has just been created by Android and that the Activity should initialize itself. onResume() is called to inform the Activity that it is now the current Activity that the user will see (topmost Activity in its task and visible on screen). onPause() is called to inform the Activity that another Activity is going to become the current Activity. onConfigurationChanged() is called to inform the Activity that a configuration change has occured on the device (this can be one of many differnt things including: locale, screen size, screen density, screen orientation, keyboard presence, etc. There are many other "lifecycle methods" that are called by Android at specific times during the lifetime of an Activity.
I'm making a simple e-book reader app, and an activity can be called by many cases.
I'd like to distinguish callee activity to know its origin action in case of
From my another activity: this can be easily solved by
StartActivityForResult from calling activity.
Called by back button click from other package app after share action ("whoops, I missed to click share button, and then back.").
Switched by user's multitasking choice.
Called by user click at the start screen: this might be known by MAIN entry point at the android manifest.
How to know above cases?
I have no idea why you would need to do this but...
1.From my another activity: this can be easily solved by StartActivityForResult from calling activity.
Yes, as long as the calling Activity is your own as you can't guarantee any 3rd-party code will use startActivityForResult(...). You can, however, use getCallingPackage() and getCallingActivity() in other cases.
2.Called by back button click from other package app after share action ("whoops, I missed to click share button, and then back.").
When the user presses the BACK button your Activity isn't being "called" - it's simply being resumed or re-started. The original calling app/Activity/method will still hold true - there is no way to ascertain that this has happened as the normal Activity life-cycle methods (onStart() and onResume()) are always called even when an Activity is first created.
3.Switched by user's multitasking choice.
If you mean using the "Recent" apps view, the same applies for my answer to 2. above.
4.Called by user click at the start screen: this might be known by MAIN entry point at the android manifest.
In this case onCreate() will be called although if your Activity is simply stopped for whatever reason, it may simply be restarted depending on the launch mode you use.
In short, I can't see you being able to gather much in the way of any accurate information as to how your Activity becomes active.
I am not too sure about the actual way for the above question as I am too a new guy in android.
But to the best of my knowledge... called by back button and switched by user's multitasking leads the activity to enter pause state.
So you can access it from "onPause" method in your activity.
I try to make Notification which must work only when Application UI isn't visible.
I tried to store preference which was written in onStart() and onStop() of my Activity. But sometimes, it's not working because another application became visible without MyActivity.onStop() being called.
What other method I can use for a Service to determine, if MyApplication is visible now? Or, maybe MyActivity?
If you already have code to keep track of the state of your app's UI, you can probably get it to work simply by putting the code in onPause() and onResume(), instead of onStart() and onStop().
It is possible for the UI not to be visible, or partially hidden, even before onStop() gets called ... as you found out.
Take a look at the Android Activity lifecycle diagram here:
http://developer.android.com/images/activity_lifecycle.png
and note the description:
The foreground lifetime of an activity happens between a call to
onResume() until a corresponding call to onPause(). During this time
the activity is in front of all other activities and interacting with
the user. An activity can frequently go between the resumed and paused
states -- for example when the device goes to sleep, when an activity
result is delivered, when a new intent is delivered -- so the code in
these methods should be fairly lightweight.
Read more about this in another question here.
Is there anyway tracking down application gets minimized? If there is something please let me know cause I have to update my own application when application gets minimized. It would be better doing somewhere irrespective of any activity.
There is a callback in activity called onUserLeaveHint(). It is called when activity gets left not by back press, but some interruption - home button or may be incoming call. Create a base class Activity which calls to an Application instanse for some action and derive from this abstract class.
I think that you can get an indication that application is being minimized by overriding the onpause and onstop methods then on the basis of your requirements you can do some action there.
Look at this post as well
I'm trying to trigger an action such as showing a popUp on the Home activity of my application when the application is launched or if it was running in the back ground and was brought to front again.
My problem is that there is no onResume() or onRestart() for the Application class, and at the home activity's side there no way of telling if the previous activity was not coming from the my application or not to decide on showing the popup or not.
is there a listener that can be trigged on return to the application?
Thanks in advance.
You should actually put it in the onCreate() method, and do a test for what intent it has been passed. That way you can distinguish when it's been called from another of your activities.
Unfortunately, it's not possible to read the task stack, or determine if the user came from the home screen, see: Android - detecting application launch from home or history
This might not work for everyone, but why not create a base class for the activities in your project? Then you would have access to onResume() and onRestart().
If you are extending other SDK Activities (ListActivity, etc), you could create base flavors of those classes and move the code called in onResume() and onRestart() to a utility class so that the same logic can be called from any "base" Activity.