When starting the activity that has a MediaRouteButton implemented according to the CastCompanionLibrary docs, the button does not display even when there are Chromecast devices in the network. (Note the MediaRouteButton here is not in the ActionBar; the activity we're talking about here actually has no ActionBar, but it extends ActionBarActivity. Other activities in our application has the Cast Button implemented into the Action Bar according to the docs, which behaves correctly when there are Chromecast devices in the network.)
We observed that onCastAvailabilityChanged (as well as onCastDeviceDetected) in the VideoCastConsumer was not being called. We traced the code and found that onRouteAdded in /cast/CastMediaRouterCallback.java wasn't being called when the activity starts.
In our activity, we create the consumer in onResume
#Override
protected void onResume() {
mCastManager = VideoCastManager.getInstance();
if (mCastManager != null) {
mVideoCastConsumer = new VideoCastConsumerImpl() {
.....
#Override
public void onCastAvailabilityChanged(boolean castPresent) {
mMediaRouteButton.setVisibility(castPresent ? View.VISIBLE : View.GONE);
}
.....
}
mCastManager.incrementUiCounter();
mCastManager.addVideoCastConsumer(mVideoCastConsumer);
}
…..
and do decrementUiCounter and remove videoCastConsumer from the manager in onPause, as the implementation docs state.
However, we did observe that strangely the onCastAvailabilityChanged and onCastDeviceDetected callbacks always get triggered after we background and foreground the application. Any ideas why that happens? Is there a way to replicate this behaviour to happen on activity load or at least in onResume/onPause?
EDIT: Worth mentioning, we tried calling mCastManager.startCastDiscovery, but the callbacks weren't called until we backgrounded/foregrounded again.
There are two things that need to happen: (1) applications should register themselves to be notified when a change in the availability of routes happen and (2) they should be able to get the current state, i.e. to see if there is any route available at that moment or not. Then in each activity that has a MediaRouterButton, both of these should be used; first register the callback and then make a direct call to see if there is any route available or not. This will set the state current at the start-up and the callback keeps the state current with changes.
I am making some small changes in this area in CCL soon to make these two steps more robust so if you can wait a few days, you should be able to see the updated version; if you can't, then you might need to do a bit of work on your side.
Update: in CCL v2.0.1, some changes were made to make this process more robust; you basically need to register to the callback onCastAvailabilityChanged() and also when you start an activity that has the button, call VideoCastManager.isAnyRouteAvailable() to set the initial visibility of the cast button.
I have a solution works for me: call mCastManager.stopCastDiscovery();
on Acitivity's onCreate() first.
stopCastDiscovery() removes the mediaroute callback. Is this the reason? (●-●)
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.
Im trying to integrate Tapresearch survey in my android app. The company provides an SDK. The SDK gives the ability to use events listeners. One of the listeners doesn't work which is onSurveyModalClosed()` where normally the app resumes running. All other listeners works fine except this one.
When Tapresearch open surveys it runs this activity : com.tapr.internal.activities.survey.SurveyActivity .
Is there any way check if this activity has been finished without using the listener onSurveyModalClosed() ?
For your question, "is there any way check if this activity has been finished without using the listener", yes there is a way.
Just put a Log message in both the methods. And see which Log message is printed in the console or which is printed first and take actions accordingly.
#Override
public void onSurveyModalClosed() {
//Survey isn't visible resume app
Log.i("Sequence_of_execution", "SurveyModalClosed");
}
#Override
protected void onStop() {
super.onStop();
// Your code goes here
Log.i("Sequence_of_execution", "ActivityStopped");
}
I guess onSurveyModalClosed() is called when you manually close the survey by taking any action and not when the Activity stops.
And for your question in comments to check if you are coming from SurveyActivity to MainActivity, do this since you don't have control over SurveyActivity-
Declare a shared preferences variable called "SurveyActivityStarted" in MainActivity and set it to false.
When starting the SurveyActivity from MainActivity, set "SurveyActivityStarted" to true.
on onResume of the MainActivity check if "SurveyActivityStarted" is true, if its true its coming from SurveyActivity.
Reinitialise "SurveyActivityStarted" to false in the MainActivity for future use cases.
I'm working with Receiver and I can detect Wifi state changes in Wifi Setting System.
But I need to know specified application is showing or not to do more thing,
ex. if specified application was hide in Background, do nothing, and if it was showing for user to integrate, I will do something.
People who know how to check specified application was running is showing or not,
Please help me,
p/s :
I have 1 Receiver and 1 Activity (is "specified Application" as I mentioned)
I can integrate in onReceive method, it means I know how to receive signal.
Thanks.
SOLUTION
Okay, thank you,
I know how to check.
I need stored public static boolean in onPause method, to indicate current is invisible state.
And when onResume method was called, the above boolean will be in Visible state.
How to check if activity is in foreground or in visible background?
Okay, thank you,
I know how to check.
I need to store public static boolean in onPause() method, to indicate its currently in invisible state.
And when onResume method is called, the above boolean will be in Visible state.
How to check if activity is in foreground or in visible background?
Let's say a user is using my Android app, I have a DialogFragment YourTanks
(it shows the various tanks you own!)
public class YourTanks extends DialogFragment implements View.OnClickListener
So again, the user is literally looking at that "page", seeing their tanks and so on.
They get a phone call or decide to use another app, perhaps their calendar, email, whatever.
Note that, of course, my app does not quit, it's still running in the background.
They finish with the other app. On the home screen or app screen of the Android, they click the icon of my app. Now, my app appears again and once again they can see their colourful tanks, etc.
So - I want to know that they have opened the app, I want YourTanks to be alert to that.
#Override
public void onStart ()
{
super.onStart();
Utils.Log("WE ARE IN onStart !!========");
Utils.Log("I think this means the user just 'opened' or 'reopened' the app.");
Utils.Log("Better ask on SO though.");
}
Indeed, is this the best way to do what I describe, or, do you have to do something at (perhaps/) the activity level, or some other concept?
(Note that on iPhone, for instance, you more or less use something like -(void)applicationDidBecomeActive:(UIApplication *)application in the overall application singleton.)
you need onResume instead, the activity could just be paused when the app is re-opened,
This function is also called after onCreate.
http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle
Here is the documentation how it's all called and working.
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