I am programming an app with the use of OpenGL and SurfaceView.
I need to detect when an activity finishes and I return to my activity.
I need it because I don't want to respond to touch and key events until my activity is shown.
Now I detect it with onResume, but onResume is called before the finishing activity disappears.
I need to enable these events when my activity is being shown (mostly because of onBackPressed).
Anyone with a solution?
EDIT:
find some information about it, so when the activity is doing something in its onStop/onDestroy method, then it is after onResume of my activity was called -> my problem
http://developer.android.com/guide/components/activities.html#CoordinatingActivities
EDIT2:
onStop and onDestroy are called when activity is not showing
EDIT3: found that it is caused because of new thread doing work in onPause, still no solution
Weird because onResume should be called when your activity is visible.
You could activate your touch controls with a small delay using a handler.
Related
I am navigating from Activity A to Activity B without finishing Activity A (because i want to go back on it and have some variables values).
In Activity B I launch camera and save captured image but the problem is, after capture camera the Activity A is re-create and resumed, causing re initialization of my variables.
How to stop it?
Note: The problem occurs only in Nougat Version.
The thing you want to know here is about the complete activity life cycle. Basically to summerize them they are
OnCreate, - called when activity is created
OnStart, - called when activity starts
OnREsume, - when activity gets back
OnPause, - when activity is overlapped
OnStop, - when activity is closes
and onDestroy - when finish() is called.
Though you have not mentioned how you started an activity and got back to the same activity, the correct way to get back to previous activity is by calling
finish()
on camera activity.
I guess you have got back to activity A by using
Intent i= new Intent(this, ActvityA.class)
startActivity(i)
which is the the correct way to do.
Just call the finish() when you want to get back to Activity A from B (Here B is in top of A).
Additional
If you want to pass data from activity B to A, just put something called as Intent Extra or Bundle
Activity A is create again and resume, so my variables are initialize again. How to stop it.
Yes that is normal. First rule with Android programming is that your activity can be killed at any time. So to not loose your variables you have to save them at the right moment.
You would do that to override onSaveInstanceState() putting your variables in the bundle.
Then you can retrieve them in onCreate() from the function parameter.
I am developing an application in Android which has several Activities and need to check some details only when application is minimized and maximized, not on Activity screens' navigation. onPause() and onResume() doesn't work. onWindowFocusChanged() also doesn't helped as that is also calling while navigating among screens. Please help in which way I can be able to get an event only when Application is minimized/maximized, not on screen navigation.
I think that this Activity callback onUserLeaveHint() could be a start.
from the doc:
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.
It's not something which is common on Android, so I would agree to CommonsWare, that you should consider a different approach.
Nevertheless, there is a (bit hacky) way to achieve this.
Override onStop() and onResume() in every of your activities, set a boolean to true in onResume() and to false in onStop(). If this is true, the application is active, if it's false it's not.
You could use a listener, which is always called after the boolean value is set, to execute your actions.
If you can add a property, let's name it isShown in your customized Application object, which means you need extend the application for this purpose. it is in your subclass of Application and it is a signleton.
Be realized the lifecycle methods for onPause and onResume, when the onPause is called, it means the activity would be going to background, invisible any more, or put into the the activity stack. onResume means the activty status is recovered and ready for the user's interaction. So if you set the isShown property to false, which is in your application object, within the activity's onPause, and set it back to true in your onResume. of cause, you can make this behavior to be extracted into a superclass for the reuse purpose.
with the composite of onPause and onResume, and a given time we can identify the min/maxmized status. Let's say, isShown is set to false for a while, we can say it is minimized. otherwise, we can explain the maximized too.
But what are you going to do with this flag? did you still have a Service running in backend ready to receive this property to do something?
I have an app that responds to Internet message by creating a new Activity and user has 20 seconds to respond.
The problem is when the app is running on background.
I can show a notification, but when the user returns to the App the new Activity isn't started.
Is there any way to start an Activity even when the App isn't on foreground (without the activity getting focus) or any easy workaround where the activity would start right after returning to the app? (which would be worse solution, cause I would have to rework the sync timer :))
Thanks
Take a look at the Activity life cycle
Here you have the onResume() method you can override and do something before the Activity itself is shown. From there you could do some kind of check that you are returning from a notification or a check on that the user has to answer something now and launch a new Activity from the onResume() method.
I often see examples of classes which end with finish(), but definitely not always. My question is when should you end a class with finish()? And what does it do exactly, what is the difference between ending a class with the back button and ending it with finish()?
Thanks in advance!
finish() can be called to kill (destroy) an Activity instance. If you don't need to close your Activity manual, which is true in many cases, you don't need to call this method.
But if you require a button somewhere in your activity that says "close", then you should use this method. But in general the back button behavior in Android will handle things like this.
The back button does not actually finish your activity, finish() calls the onDestory() method right away, while the back button does not.
When the back button is pressed, the onStop() method is called, but the onDestory() method call might be delayed by the system, this so that the Activity can be resumed by the system which is cheaper (in resources) than a full restart.
Lifecycle:
http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle
Finish():
http://developer.android.com/reference/android/app/Activity.html#finish()
You finish Activity class with finish() method when you need to end the Activity immediatly at specific point. If you press back button, you will go through the lifecycle (onPause(), onStop() then onDestroy()) automatically.
One reason to use finish is when you have started the activity with
void android.app.Activity.startActivityForResult(Intent intent, int requestCode)
In this case you dont go back to the Activity which started it with another startActivity but with finish
Back button takes you to the last activity in the stack whereas finish() destroys the current activity. You can call finish() on an activity for which you don't want the user return to. Example: LoginActivity
public void finish ()
Call this when your activity is done and should be
closed. The ActivityResult is propagated back to whoever launched you
via onActivityResult().
finish() is called to kill your activity instance after you did what you have to do with the activity. After that, you have no use with the activity. So simply call finish() and it will kill your activity.
If you press back button, it will call finish() automatically to kill the activity. It will go through the activity lifecycle(onPause(), onStop() and finally onDestroy())
The one reason with calling finish() is that, if you don't call it, when you navigate through activities, all the activities will be added to the backstack. So when you go back, you have to come back through these activities. So for e.g when you click back button, it simply kills the activity and it will be removed from the backstack.
My application shows an alert that the user must respond to before continuing to do other things. I'm trying to figure out the best way to implement this. Using an Activity for the alert isn't quite working.
In my current implementation, the alert is activity (A). When another activity from the same package is started and onStop is called, it starts itself again using FLAG_ACTIVITY_REORDER_TO_FRONT so that it's always at the top of the stack. This works as described, unless Activity A uses Theme.Dialog or Theme.Translucent.
Modified log:
Activity A created
Activity A started
Activity A resumed
Activity A paused
Activity B created
Activity B started
Activity B resumed
Activity B gains window focus
Activity A stopped
Top activity in stack is Activity B, so Activity A relaunches itself
Activity B paused
Activity A started
Activity A resumed
The top activity in the stack should be Activity A, however Activity B remains in the foreground.
Another implementation detail: my application is not for a phone, so I'm not concerned with a back button finishing the activity or interactions with other apps. Still, I agree that on principle I should prevent such problems anyway, so in my code I check whether the activity that has come in front is from the same package (i.e. from our code base). This should work around the theoretical problem of interfering with other apps.
Is there a way to bring Activity A into focus? I understand that this is unusual behavior, but it is necessary for Activity A to remain in the foreground until it is deliberately finished.
I'm also open to suggestions about a completely different and better approach!
FWIW, I'm running 2.2.
(Cross-posted from http://groups.google.com/group/android-developers/browse_thread/thread/d46fd7d59abe15a0, where we got no response.)
You can't do this. Please don't do this. The activity at the top of the stack is the one that has input focus. What you are trying to do fundamentally breaks the user interaction that is supposed to happen.
What you are doing is generally considered by the platform to be an abuse of it, and Android has increasingly been doing things to prevent applications like this from causing harm.
Well, here's what I had in mind:
public class ActivityA extends Activity
{
...
public void onStop() {
super.onStop();
finish();
Intent i = new Intent();
i.setClass(getApplicationContext(), ActivityA.class);
startActivity(i);
}
}
ActivityA is finished in onStop() and started again right away. You might have to check issues regarding device rotation, but this approach should work.
Having window focus means that activity B is still in its visible lifetime, since it has on top the activity A which has a translucent bg or is dialog-like.
Having the window focus doesn't mean that activity B is on the foreground. They are different things.
If you don't want this, then don't use those two themes.