Android app timeout how to apply? when it minimized - android

I want to make a exam app I want to apply a feature in my app in this feature when student minimize app then exam will automatically cancelled. so they can't cheat so please tell me what will and how will I do this in Android app in adt.

In your exam activity, override onPause() and paste cancel exam method before super.onPause().
Edit: I think you need onPause() instead of onStop(). Learn more about Activity Life cycle

When you minimize app, onStop will called.
Called when you are no longer visible to the user
So inside onStop you can cancel the example
#Override
protected void onStop() {
super.onStop();
// cancel exam here
}

Related

Sending data on user closing the android app

I need to update user information everytime the user after working with the Android App closes the App.How can do the same. do i have some onDestroy kind of method in Application that i can use for the same.
Kindly update.
thanks
There absolutely is such a method - and you guessed it's name correctly. You could override the onDestroy() method.
However, onDestroy() is also called with the application is rotated. There are other issues as well.
Android doesn't really have a concept of closing the app. The best way to do this is to save onPause() - which will be basically every time the user leaves the app for some reason, such as for multitasking. Just override the onPause() method and go from there:
#Override
protected void onPause() {
super.onPause();
saveData()
}
private void saveData() {
//do whatever to save data here
}
I would also read about the android application lifecycle to find out more about how all of this works.

Pausing the Activity in Android

I have developed a game in android and it has some timer in it,when ever I'm playing the game if some one rings me the game is not going to paused state the time is still running in the background.what i need is when some in coming call has arived it should display a prompt message that the game is in pause state how to achieve this..?
You will have to override the onPause() function which is called everytime you get called you go back to the main menu or whatever. Here is a sample of the docs :
When the system calls onPause() for your activity, it technically means your activity is still partially visible, but most often is an indication that the user is leaving the activity and it will soon enter the Stopped state.
I advise you to read this link carefully to understand the different steps in an activity life and that link as well
Here is how to override your onPause() function :
#Override
protected void onPause() {
//Do something like pausing the timer
}
When the user goes back to the game, you want the timer to restart and therefore, you'll override the onResume() function :
#Override
protected void onResume() {
//Resume the timer
}
Have a good day !

Android onRestart for whole App?

On Android, I'm trying to catch the event where my app comes back to the foreground from the background. In other words, the app was active, then the user minimized it (by clicking the home button or starting another app or something), and then the user runs the app again.
I can add the onRestart() method to an activity as follows:
#Override
public void onRestart() {
super.onRestart();
Log.d("MAIN", "onRestart called.");
}
But this only works when that specific activity is the active one when the user minimizes the app.
Is there a way to catch this for the whole app somehow? Or do I need to add onRestart to every single activity I have? (I suppose I could create a superclass on which all the other activities are based).
Thanks!
Is there a way to catch this for the whole app somehow?
No, because there is no concept of an app "restarting".
Or do I need to add onRestart to every single activity I have?
Presumably. Or, find a way to avoid needing to "catch the event where [your] app comes back to the foreground from the background".
I think the method you need is void onResume()
here is there android developers page for activities , check the "Implementing the lifecycle callbacks" part of the page .
http://developer.android.com/guide/components/activities.html
hope this helps.

How to use onResume()?

Can anyone give me an example that uses onResume() in Android?
Also, if I want to restart the activity at the end of the execution of another, which method is executed—onCreate() or onResume()?
And if I want to update data, how do I put it in onResume()?
Any Activity that restarts has its onResume() method executed first.
To use this method, do this:
#Override
public void onResume(){
super.onResume();
// put your code here...
}
Restarting the app will call OnCreate().
Continuing the app when it is paused will call OnResume(). From the official docs at https://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle here's a diagram of the activity lifecycle.
The best way to understand would be to have all the LifeCycle methods overridden in your activity and placing a breakpoint(if checking in emulator) or a Log in each one of them. You'll get to know which one gets called when.
Just as an spoiler, onCreate() gets called first, then if you paused the activity by either going to home screen or by launching another activity, onPause() gets called. If the OS destroys the activity in the meantime, onDestroy() gets called. If you resume the app and the app already got destroyed, onCreate() will get called, or else onResume() will get called.
Edit: I forgot about onStop(), it gets called before onDestroy().
Do the exercise I mentioned and you'll be having a better understanding.
When you open the app it will go through below states:
onCreate() –> onStart() –> onResume()
When you press the back button and exit the app
onPaused() — > onStop() –> onDestory()
When you press the home button
onPaused() –> onStop()
After pressing the home button, again when you open the app from a recent task list
onRestart() –> onStart() –> onResume()
After dismissing the dialog or back button from the dialog
onResume()
If a phone is ringing and user is using the app
onPause() –> onResume()
After the call ends
onResume()
When your phone screen is off
onPaused() –> onStop()
When your phone screen is turned back on
onRestart() –> onStart() –> onResume()
Happy Coding...#Ambilpura
Most of the previous answers do a good job explaining how, why, and when to use onResume() but I would like to add something about re-creating your Activity.
I want to know if I want to restart the activity at the end of exectuion of an other what method is executed onCreate() or onResume()
The answer is onCreate() However, when deciding to actually re-create it, you should ask yourself how much of the Activity needs to be re-created. If it is data in an adapter, say for a list, then you can call notifyDataChanged() on the adapter to repopulate the adapter and not have to redraw everything.
Also, if you just need to update certain views but not all then it may be more efficient to call invalidate() on the view(s) that need updated. This will only redraw those views and possibly allow your application to run more smoothly. I hope this can help you.
onResume() is one of the methods called throughout the activity lifecycle. onResume() is the counterpart to onPause() which is called anytime an activity is hidden from view, e.g. if you start a new activity that hides it. onResume() is called when the activity that was hidden comes back to view on the screen.
You're question asks abou what method is used to restart an activity. onCreate() is called when the activity is first created. In practice, most activities persist in the background through a series of onPause() and onResume() calls. An activity is only really "restarted" by onRestart() if it is first fully stopped by calling onStop() and then brought back to life. Thus if you are not actually stopping activities with onStop() it is most likley you will be using onResume().
Read the android doc in the above link to get a better understanding of the relationship between the different lifestyle methods. Regardless of which lifecycle method you end up using the general format is the same. You must override the standard method and include your code, i.e. what you want the activity to do at that point, in the commented section.
#Override
public void onResume(){
//will be executed onResume
}
KOTLIN
Any Activity that restarts has its onResume() method executed first.
To use this method, do this:
override fun onResume() {
super.onResume()
// your code here
}
Re-review the Android Activity Lifecycle reference. There is a nice picture, and the table showing what methods get called. reference Link google
https://developer.android.com/reference/android/app/Activity.html
After an activity started, restarted (onRestart() happens before onStart()), or paused (onPause()), onResume() called. When the activity is in the state of onResume(), the activity is ready to be used by the app user.
I have studied the activity lifecycle a little bit, and here's my understanding of this topic:
If you want to restart the activity (A) at the end of the execution of another, there could be a few different cases.
The other activity (B) has been paused and/or stopped or destroyed, and the activity A possibly had been paused (onPause()), in this case, activity A will call onResume()
The activity B has been paused and/or stopped or destroyed, the activity A possibly had been stopped (onStop()) due to memory thing, in this case, activity A will call onRestart() first, onStart() second, then onResume()
The activity B has been paused and/or stopped or destroyed, the activity A has been destroyed, the programmer can call onStart() manually to start the activity first, then onResume() because when an activity is in the destroyed status the activity has not started, and this happens before the activity being completely removed. If the activity is removed, the activity needs to be created again.
Manually calling onStart() I think it's because if the activity not started and it is created, onStart() will be called after onCreate().
If you want to update data, make a data update function and put the function inside the onResume(). Or put a loadData function inside onResume()
It's better to understand the lifecycle with the help of the Activity lifecycle diagram.

Killing android application on pause

I have an application which I would like to be fully disabled/closed when it is paused (IE. When the user presses the Home, End (call) and Back button I would like the application to be closed, instead of being saved in the history stack).
How do I do this....?
Thanks.
Implement onPause() in your activity and call finish() on your activity. Bear in mind, though, that this will occur on every pause, including dialogs, incoming calls, users activating a Notification. You might want to consider doing finish() in onStop(), which would at least solve the dialog problem.
Also, bear in mind that users will may get confused when using your app, thinking it has crashed since it is gone when they try to get back to it.
you can easily do that by setting true the "noHistory" attribute in to your activity element, in the manifest
http://developer.android.com/guide/topics/manifest/activity-element.html#nohist
You know how you have an OnCreate() method in your activity which performs actions when you start. You need to add something like:
#Override
protected void onPause(){
finish();
super.onPause();
}
in your activity to add actions before it starts
in this case the
finish();
command is what you want to execute before your activity pauses.

Categories

Resources