On Finish activity in Android - android

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.

Related

best place to add an action when application starts up or returns to the forground

I need my application to do 2 network calls when ever the app is started for the 1st time, when it has been killed and started again as well as when it has been placed in the background then returned.
I know I can put it in my "MainActivity" onCreate/onResume. I have a class that extends Application which is where I am initialising logging and crash reporting, but I noticed there is no onResume method, which from my understand is the method that is called when the application comes from the background.
Where would be the best place to do these 2 network calls to update certain aspects in my app when the app is started for the 1st time, started when killed and resumed when it comes back from the background.
FYI. The reason I don't want it to go in my "MainActivity" is that I don't want these network calls to be called when ever i return to the MainActivity from another screen in the app, only when the user returns to the app?
Thanks
The use case of calling whenever the app returns from background is by implementing Activity Life Cycle methods in your Application class:
public class myApp extends Application implements Application.ActivityLifecycleCallbacks {
...
}
In this case you make sure that whenever an onPause() is called a corresponding onResume() should also be called (i.e. normal screen switching). If not, then you know that your app is now in background. The next callback to onResume() should mean that it has come to foreground again and you can make your network call.

CastCompanionLibrary detecting presence of Chromecast devices on network

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? (●-●)

Is any way to detect when any activity of my app are displaying and when all activities are closing?

I.e I would like to know when user interact with my application and when not.
I have tried do it using ActivityManager.getRecentTasks(). I have checked root activity at a top task to detect interact user with my application or not.
I have forced to check it in separated thread each second or two.
This way is bad for me. There is another way to detect when any activity of my app are opening or closed?
Have a look at the lifecycle of an Activity.
There are callback methods (onStart, onResume, onPause, onDestroy, ...) that are invoked by the system whenever your activity is created, becomes active or inactive etc.
You might create your own application class (just inherit from android.app.Application) and do your tracking there. The application will be around as long as your app is running.
For example you could put a flag or a counter there and set it from the activities' callbacks. A simple example for that could be:
public void onResume() {
super.onResume();
((MyApplication)getApplication()).active = true;
}
public void onDestroy() {
super.onDestroy();
((MyApplication)getApplication()).destroyed += 1;
}

Android - How to track app Resumes only when you exit and come back to the app?

I have an issue. For analytic purposes I need to track when the APP (not activity) is resumed. The problem I have now is that if I put the tracker on the OnResume event of an activity, it will get fired every time the user goes back and forth on different activities.
How can I avoid that? How can I track the real "Application Resume," (when user actually exits the app and come back) and not the activity resume?
Any ideas is greatly appreciated. Thanks.
I encountered the same problem and solved it by creating base activity :
public class mActivity extends Activity{
public static final String TAG = "mActivity";
public static int activities_num = 0;
#Override
protected void onStop() {
super.onStop();
activities_num--;
if(activities_num == 0){
Log.e(TAG,"user not longer in the application");
}
}
#Override
protected void onStart() {
super.onStart();
activities_num++;
}
}
all the other activities in my app inherited mActivity. When an activity is no longer visible than onStop is called. when activities_num == 0 than all activities are not visible (meaning the the user close the app or it passed to the background). When the user start the application (or restarting it from the background) onStart will be called (onStart is called when the activity is visible) and activities_num > 0. hopes it helps...
Use the Application object of your app (see http://developer.android.com/reference/android/app/Application.html). If you create a custom Application class and configure it in your AndroidManifest.xml file you can do something like this:
Start tracking in the onCreate() of the Application object.
Instrument all your Activities so their onPause() and onResume() methods check with the Application object and see if they are the first Activity to run, or if they are continuing a previously running instance of the app.
Stop tracking in the onDestroy() of the Application object.
To a certain degree most of the analytics packages (Flurry and their ilk) do something similar to this. You'll need to do a little state machine work to get this to work right, but it shouldn't be too complicated.
Instead of OnResume(), hook into the OnCreate() event of your main activity.

Android Activity lifecycle testing

I have Activity, which is to save its data in case system decides to kill it while it is in the background.
So, I’ve got onSaveInstanceState:
#Override
protected void onSaveInstanceState(Bundle outState){
outState.putString("value", "some_value");
}
I check whether Bundle object is null in onCreate:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
isRestarted=(savedInstanceState==null);
How do I write test method? I tried
public void testRecreate(){
Instrumentation mInstr=this.getInstrumentation();
mInstr.callActivityOnSaveInstanceState(mActivity, null);
mActivity.finish();
mActivity=this.getActivity();
assertEquals(false, mActivity.isRestarted);
}
but it seems to be wrong.
You can use some hidden API functionality. In your test's setup, call the
android.app.ActivityManagerNative.getDefault().setAlwaysFinish()
method via reflection (because it's a hidden API) and confirm that the value was successfully set using
android.provider.Settings.System.getInt(getContentResolver(), Settings.System.ALWAYS_FINISH_ACTIVITIES, 0)
Then in the test cleanup, set this setting back to false.
Enabling the AlwaysFinish setting causes the system to destroy activities as soon as they are no longer on the screen, immediately triggering the onSaveInstanceState event. For this code to work, you will need the ALWAYS_FINISH and WRITE_SETTINGS permissions.
See the code for the SetAlwaysFinish tool linked in this blog: How to test onSaveInstanceState and onRestoreInstanceState on a real device
For manual testing, this routine works:
Launch the activity you want to test
Hit the home menu button on your device (activity is stopped)
Kill the process from DDMS (activity is still not destroyed)
Bring back your app again from the recent apps list
This should trigger onCreate again on your activity while not restarting the entire application. If you're not saving and loading the state properly, you'll know soon enough.
If you haven't been through it before, I found it worthwhile to do this at least once.
Stuff like that could be easily test using robolectric. Check it out!

Categories

Resources