I have a quiz application project for our school. I want to prevent the users not to press the home and back button or leave the app, because if they do they will get 0 score.
We know about the lifecycle...
Start Application > onCreate > OnStart > OnResume > Activity Is running > OnFreeze > OnPause
If i pressed the home and back button, what method in the lifecycle will be invoked if I start the application again?
And in that method I will make a code to say the user has committed/cheat/left the application. Im thinking in the onResume?
Thanks.
When back button pressed and exit the app
onPaused() -> onStop() -> onDestory()
When home button pressed
onPaused() -> onStop()
After pressed home button when again open app from recent task list or clicked on icon
onRestart() -> onStart() -> onResume()
Back button pressed from another app or settings then user can see our app
onRestart() -> onStart() -> onResume()
When open another app from notification bar or open settings
onPaused() -> onStop()
Activity base class contains events that govern the life cycle
of an activity.
onCreate():
Called when the activity is first created
onStart():
Called when the activity becomes visible to the user
onResume():
Called when the activity starts interacting with the user
onPause():
Called when the current activity is being paused and the previous activity is being resumed
onStop():
Called when the activity is no longer visible to the user
onDestroy():
Called before the activity is destroyed by the system
onRestart():
Called when the activity has been stopped and is restarting again
Example
public class LifeCycleActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Toast.makeText(LifeCycleActivity.this,"ON CREATE", Toast.LENGTH_SHORT).show();
}
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
Toast.makeText(LifeCycleActivity.this,"ON START", Toast.LENGTH_SHORT).show();
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
Toast.makeText(LifeCycleActivity.this,"ON RESUME", Toast.LENGTH_SHORT).show();
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
Toast.makeText(LifeCycleActivity.this,"ON PAUSE", Toast.LENGTH_SHORT).show();
}
@Override
protected void onRestart() {
// TODO Auto-generated method stub
super.onRestart();
Toast.makeText(LifeCycleActivity.this,"ON RESTART", Toast.LENGTH_SHORT).show();
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
Toast.makeText(LifeCycleActivity.this,"ON STOP", Toast.LENGTH_SHORT).show();
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
Toast.makeText(LifeCycleActivity.this,"ON DESTROY", Toast.LENGTH_SHORT).show();
}
}
Figure
For more details visit here
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.