Analysis of the differences between onStart of and onResume of in Android Activity

  • 2021-01-03 21:04:01
  • OfStack

This paper analyzes the differences between onStart() and onResume() in Android Activity. To share for your reference, the details are as follows:

First you need to know the four states of Activity:

When Activity is launched, it is at the front of the screen, at the top of the stack, at this time it is visible and can interact with the user in the active state.

The state of Activity when it is overwritten by another transparent or ES17en-style Activity. It is still connected to the window manager, and the system continues to maintain its internal state, so it is still visible, but it has lost focus and cannot interact with the user.

Stoped is in Stoped state when Activity is overwritten by another Activity and losing focus is not visible.

Killed Activity is in the state of Killed when it is killed and recovered by the system or not started.

Analysis:

protected void onStart() this method is called after the onCreate() method, or when Activity transitions from the Stop state to the Active state, and onResume() is executed after onStart() is normally executed.

protected void onResume() is called when Activity transitions from Pause state to Active state.

onResume is the activity that gets the user's focus and interacts with the user

onStart is activity visible to the user, including 1 activity above him, but not completely overwritten, the user can see part of activity but cannot interact with it.

Supplement: Six main functions in Android Activity

Each Activity1 in Android needs to implement 6 functions:

onCreate(), onStart(), onResume(),onPause(),onStop(),onDestroy().

1. onCreate function: Register the variables you want to use, such as service, receiver, which can be responded to whether your Activity is in the foreground or in the background, and then call the above function to initialize the layout information.

2. onStart function: Register 1 variable. These variables must be responded to when the Android Activity class is in the foreground.

3. onResume functions: call 1 to refresh UI and refresh the state of each UI control every time Activity is called here.

4. onPause functions :1 usually do 1 variable Settings, because Activity is about to cut to background processing, some variables may be released or the state needs to be adjusted accordingly.

5. onStop function: Unregister variables registered in the onStart function.

6. onDestory functions: Unregister variables registered in the onCreate function.

In Android - Hello,


public class UbiLrnActivity extends Activity {
 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
 }
}

First, an onCreate function is declared, which takes an argument of savedInstanceState. Type Bundle, Bundle is a data parameter, commonly used for data transfer between Activity. The arguments to onCreate() are of the Bundle class.

super. onCreate represents the call to the parent class onCreate.

setContentView(ES123en.layout.main) refers to the resource that loads the system by loading ES126en.xml.

For more information about Android Activity, please refer to Android Programming: Summary of activity Operation Techniques.

I hope this article has been helpful for Android programming.


Related articles: