How to tell when the user is not focused on this activity? - android

I wasn't exactly sure how to word this question, but I know it's a very simple one to answer. How can tell when the user is no longer on the activity I want the user to be on. For example, how do I know if the user has unexpectedly pressed HOME or if a phone call is received and interrupts the current activity?

You can check if your Activity has focus with this method:
hasWindowFocus()
If you want to capture when the user leaves the activity, you'd want implement this in your Activity:
protected void onPause() {
super.onPause();
// Code here...
}
Also there is more info about activity life cycle here:
http://developer.android.com/reference/android/app/Activity.html

Check the Activity life cycle:
http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle
If the user leaves the Activity for any reason, onPause() will be called.

Related

Restart app from launch screen instead of resuming

I have an application with basically a list of items and a detail screen for each items.
When initially started, we show the list of items. If the user switches to another app when viewing a details screen, when he comes back to the app, the details screen is shown.
All that is the standard, and working well. However, my client needs the user to come back to the list screen instead of the details screen each time the app is resumed.
My first idea would be to remember the time at which the details activity got paused, and when started, if the time is greater than X seconds, finish and launch list activity instead of resuming.
Any more reliable way to do that?
PS: I know we should not do that, I already explained that to my client, decision is not mine.
Use SharedPreference to save the time of your paused detail activity in onPause and when it resume check the saved time with current time whether it has passed your threshold if it is passed then close it otherwise remain it opened.
Implement this solution and it definitely helps.
Basically, the app is not actually restarting completely, but your launch Activity is being started and added to the top of the Activity stack when the app is being resumed by the launcher. You can confirm this is the case by clicking the back button when you resume the app and are shown the launch Activity. You should then be brought to the Activity that you expected to be shown when you resumed the app.
The workaround I chose to implement to resolve this issue is to check for the Intent.CATEGORY_LAUNCHER category and Intent.ACTION_MAIN action in the intent that starts the initial Activity. If those two flags are present and the Activity is not at the root of the task (meaning the app was already running), then I call finish() on the initial Activity. That exact solution may not work for you, but something similar should.
Here is what I do in onCreate() of the initial/launch Activity:
if (!isTaskRoot()
&& getIntent().hasCategory(Intent.CATEGORY_LAUNCHER)
&& getIntent().getAction() != null
&& getIntent().getAction().equals(Intent.ACTION_MAIN)) {
finish();
return;
}
for more details on isTaskRoot()method reference.
You have to provide the following onPause() method to all the activity classes except your list_item activity(Initial Activity).
#Override
protected void onPause() {
super.onPause();
Intent i = new Intent(getApplicationContext(), list_item_activity.class);
startActivity(i);
}
I might understand your problem incorrectly but why you do all the timing stuff? I mean, assuming you've fragments for your list and detail views, just put a a flag to monitor your activity has stopped and listen to catch your activity resume ( via onResume or onWindowFocusChanged ). If it's stopped and resumed then transition to list fragment if it's not already visible.
You can you a broadcast receiver here.
And on activity OnResume method use a call to broadcast receiver and perform whatever you need like this.
#Override
protected void onResume() {
super.onResume();
sendBroadcast(new Intent("YourActionHere"));
}

Confusion in understanding methods in android

I want to play audio on lock screen and I know for this I have to use service. But I am little confused So I have following questions.
I want to know on home and back button press which method is called i.e. onPause or onStop?
If my app running and someone call me and I pick the call then which method is called i.e. onPause or onStop?
On screen lock which method is called?
first onPause() will be called for each of your question and onStop() will be called when another activity comes in foreground and your previous one goes in background.
The Basic difference is that when onStop() called the previous activity in not visible and new activity comes in foreground and overlap the previous one.
On home press: onPause->onStop. On back press: onBackPressed->onPause->onStop->onDestroy
Same as pressing home essentially.
Same as 2.
Have a look at the documentation:
http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle

android Back button functionalit

I just need to know this. Since calling the finish() in the activity takes you to the previous activity, when you press the Back button it actually finishes the current activity and takes you to the previous activity?
When the user presses the back button on the current activity it is popped from the activity stack and destroyed and the previous activity is resumed with its re-stored state.
Read Tasks and Backstack for a more detailed information. It is essential that you understand this concept thoroughly.
Hope this helps!!
This would be true if you haven't gotten rid of the activity by say calling finish() on your previous activity. You also can change the functionality by calling
#Override
public void onBackPressed() {
// do something on back.
return;
}

Which actions does the back button/back key on Android trigger?

I am really confused. I have read that the back button
calls onDestroy()
can close up your currently-running activity
calls onPause()
I think onPause() should be right. But this is an side effect, because the Activity gets into the background. I found nothing in the docs. But maybe I have overlooked something.
Can someone please explain to me what the back button is supposed to do programmatically? Some references would also be nice. :-)
I have read that the back button calls onDestroy(), can close up your currently-running activity, calls onPause()
All three are correct.
I found nothing in the docs.
Quoting the Android documentation:
When the user presses the BACK key, the current activity is popped from the top of the stack (the activity is destroyed) and the previous activity resumes (the previous state of its UI is restored).
To elaborate, if there is nothing else that will consume the BACK button press (e.g., an open options menu), your Activity will be called with onBackPressed(). The default implementation of this calls finish(). This will take your activity from the running to the destroyed states, calling onPause(), onStop(), and onDestroy() in sequence, as shown in the event flow diagram:
Just to add, browser application overrides onBackPressed() to go back to previously opened tabs (if available) and it not, closes the application.

Android Detect what caused the onPause

I am not sure if there is a better way of doing this but I want to detect somehow what caused the application to pause.
On one particular activity that displays a tracking map if the user click back or home I want to stop GPS but if the screen goes off I want to keep the gps running I am also using a wake lock so it doesn't sleep (yes I know this probably should be in a service, but that will be v2 I'm running out of time!)
I am overriding when the back button is pressed
#Override
public void onBackPressed() {
wl.release();
this.mMyLocationOverlay.disableMyLocation();
this.mMyLocationOverlay.disableCompass();
if (mLocManager != null) {
mLocManager.removeUpdates(mLocListener);
}
super.onBackPressed();
}
but I can't find a way of doing the same for home.
Can anyone help?
Bex
in the onPause() you can call isFinishing() to know if the activity is finishing for whatever may be the reason or simply getting paused. See the doc here
But first please check whether your logic that you have written comes to onBackPressed(). write some log to confirm that that part of your code is active
If i understand you correctly. You have one activity which has some view from that activity you have started another activity. But you want when you click back key of the latest activity some actions you have to perfrom in previous activity. If this is the case you have to write logic in onResume() method. Because as soon as you click back key activity on top is finished and your focus comes to onResume() method of previous activity. donot override onKEyDown() or onBackPressed().
Donot forget to vote if you feel my response is useful for you.
Thanks
Deepak

Categories

Resources