On the click of a button, I want onPause to be called. On clicking back/after a time interval, I want onResume to be called.
I find that system alerts, dialogs etc. do not call onPause and onResume on show and cancel.
I need to do this to verify a functional test. If i show a transparent activity, it's lifecycle methods would be called which would contradict what I am trying to test.
I only want to verify onPause and onResume on my current activity. Is this possible?
You need to create a new transparent (or partially transparent) Activity to trigger onPause. Simply adding a View won't do anything.
Then, hitting back, or finish-ing the newly created transparent Activity will trigger the onResume method.
If you want to test onStop functionality in addition to onPause, you'll want your new Activity to be opaque and the full screen size.
You should test it this way -- let the Android system trigger these methods to be called, instead of manually running them. Hence, you can let a button click launch the new Activity, then either hit back to close it, or add a button to the new Activity to finish it.
Related
I've searched through dozens of Stackoverflow posts and the android doc but just couldn't find the answer.
According to the accepted answer of this SF-post the onCreate method runs when the activity is first created. It also notes that in here views are supposed to be created and list data is being binded.
Then the onStart Method runs but here's the issue. Where's the difference? If you do everything inside of onCreate, switch activities, your app will still display the same data, regardless whether you put the app in the background or switched activities.
So if you declare views in onCreate, what do you do in onStart? initiliaze the views to their R.id.view ? Fetch data?
onResume I suppose is then used for listeners since it's the gas and brake according to this SF-posts accepted answer.
onCreate() is called when the activity is first created. onStart() is called whenever the activity becomes visible, which includes when it is first created (after onCreate()) and after it is coming back to the screen from being stopped (e.g., another activity took over the screen).
So:
Put code in onCreate() that needs to happen when the activity is created (and use onDestroy() to clean it up)
Put code in onStart() that needs to happen either when the activity is created or when the activity returns to the foreground (and use onStop() to clean it up)
Frequently, we do not do anything special when the activity returns to the foreground, in which case you do not need to worry about onStart() or onStop().
I am creating a simple button application for Android. The application controls a very expensive machine so I have to make sure that user cannot accidentally click on any button. So I created a "lock" screen as an activity and start it whenever the application becomes active (when onResume() method is called). But when I am inside the app and just lock the phone and then unlock it I can see the activity for about half a second before the "lock" screen jumps in.
I was trying to start it when onPause() is called, but when the back button is pressed, it will navigate to "lock" screen instead of going out of the app.
I was wondering if I can put the activity to the front but activate it when onResume() method is called.
Thank you for your answers.
I have some floating bugs in my app, which unable to reproduce clearly. I suspect them from inproper work of my SaveInstanceState|restoreInstanceState mechanism, so I need to check case, when activity is being stopped when goes to background, and recreating after I press back button from spawned activity. Is there a way to force android stop and destroy activity which went to background? It should remain on activity stack, so I cannot just finish it.
Just enable the developer option "don't keep activities" (or whatever its called). This won't remove the activity from the stack, but will actually call onStop() and onDestroy() whenever the user leaves the activity and opens another. When the user presses BACK, Android will create a new instance of the activity, and call onCreate() and onRestoreInstanceState()` as expected.
Looks like it's a copy of this and this questions. Override onPause() method and call method finish() in it.
We know that the android activity cycle has multiple phases.
between onStart() and onStop() is called the visible lifetime
between onResume() and onPause() is called the foreground lifetime
What are the the key difference between them? Please give examples if possible.
If I display an Activity on the screen and the user is interacting with it, it is both in the foreground and visible.
If I start another Activity, which is transparent and shows a dialog box over the previous Activity, then the new Activity (the dialog box) is in the foreground and the old Activity is not in the foreground but still visible.
between onStart() and onStop() called visible lifetime that mean that the activity is visible either entire activity or partially visible and the user can see it on the screen and interacte with
between onResume() and onPause() called foreground lifetime that your activity is full visible and running and have full focus .
UPDATE
partially visible for example if another activity come in front of the current one and it only display a dialog and a transparent background . the user can see that activity but cant interact with it
Foreground Activity: You’d think what the user is currently interacting with would be the most important thing to keep alive.
Visible Activity: You’ll find that there are situations where your activity can be visible but not in the foreground. A simple example is when the foreground activity starts a new activity with Dialog theme or a translucent activity. Another example might when you invoke the runtime permission dialog.
Please find below link for better understanding
https://medium.com/androiddevelopers/who-lives-and-who-dies-process-priorities-on-android-cb151f39044f
The Visible Lifetime: Although this is termed the "visible lifetime", the app may not be directly visible and interacting with the user at any one time if it is not in the foreground. The feature that distinguishes this lifetime is that, even if not in the foreground, the app maintains resources such that it can instantaneously return to the foreground.
The Foreground Lifetime: During foreground lifetime the activity is in front of all other activities and interacting with the user.
I have a little problem to detect when the application is finished. I need to do some actions onDestroy like save the parameters into the database and make a final connection to the server.
The problem is that if I put the code in onDestroy its is called when the orientation changes for example. Putting
android:configChanges="orientation|keyboardHidden"
in the manifest for that activity the landscape/portrait layouts don't swap. And adding
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
setContentView(R.layout.main);
}
Changes the layouts but the buttons and labels do not get the onClickListeners and the text labels correctly. How can I solve that? Thanks
The problem is that your layout items aren't initialized again because you're initializing them in your onCreate() function, and then you're disrupting them with a new layout in onConfigurationChanged().
One option is to move the initialization to a new function that gets called from both onCreate() and onConfigurationChanged().
Another option is to use the android:onclick="" (and related) attributes in your layout.
The option I would choose is different though. I would allow Android to manage orientation (and to call onDestroy()) and in onDestroy() I would install an Alarm for, say, 10 seconds (which I imagine is plenty of time to have onCreate() called again). In onCreate() I would cancel the alarm. When the alarm fires, I would perform my save actions.
Declare buttons and labels as class variable.
setContentView recreates your view, so you must rebind your data. the best approach would be a function called both from onCreate() and onConfigurationChanged(), with layout creation and bindings.
If you don't want to anything to happen when orientation changes occur, than you should not re-setContentView(). Basically you are telling your app: "DO NOTHING WHEN ORIENTATION CHANGES". So, remove the setContentView inside the onConfigurationChanged() or test for which orientation currently is active and then load desired layout resources.
When orientation changes onDestroy() is called because the changes restart your entire activity.
Read more here:
http://developer.android.com/guide/practices/screens_support.html#qualifiers
http://developer.android.com/guide/topics/resources/providing-resources.html
Orientation testing:
Setting the background of an Activity
Could you do that stuff in overriden finish() of the activity?