First, the source of the problem
Recently I learned the life cycle of Android development activities, and I am familiar with the life cycle of a single activity. When an activity enters onStop(), how do I know if the user clicked the home button or if it entered another activity of this program?
2. Method exploration
After checking online resources, I learned that you can use android’s own methods to determine. Some methods, however, require user authorization or a later version of Android. However, this article focuses on the activity’s own lifecycle callback function.
3. Basic principles and ideas
In designing a jump between two activities, we should consider the order of callbacks in the life cycle of multiple activities. When jumping to an activity, execute the original activity firstonPause()
And then execute the second activityonCreate()
,onStart()
,onResume()
To execute the first activityonStop
. As shown, red is the first activity’s callback execution and blue is the second.
We can take advantage of this feature by using a static variable to record the number of activities currently run. When this number is 0, the program is pushed into the background. When this number is not 0, a jump operation is currently performed.
Four, code implementation
4.1 BaseActivity class
Start by creating a new BaseActivity class and declaring a static variable that all other activities inherit. And change count in onStart() and onStop().
public class BaseActivity extends AppCompatActivity {
static int count = 0;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
protected void onStart(a) {
count++;
super.onStart();
}
@Override
protected void onStop(a) {
count--;
super.onStop(); }}Copy the code
4.2 MainActivity class
Create the MainActivity class, which inherits from the BaseActivity class, and modify onStop() to check the value of count to determine the cause of onStop(). I added a button to this activity to jump to the second activity.
public class MainActivity extends BaseActivity implements View.OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = findViewById(R.id.button);
button.setOnClickListener(this);
}
@Override
protected void onStop(a) {
super.onStop();
Toast toast;
if (count == 0){
toast = Toast.makeText(getApplicationContext(), "Program goes into the background", Toast.LENGTH_SHORT);
}else {
toast = Toast.makeText(getApplicationContext(), "Program to second activity", Toast.LENGTH_SHORT);
}
toast.show();
}
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, SecondActivity.class); startActivity(intent); }}Copy the code
4.3 SecondActivity class
This class is simple and does nothing except jump over and execute the onCreate() method of the base class.
public class SecondActivity extends BaseActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); }}Copy the code
Five, the summary
At this point, we have implemented the activity lifecycle callback function to determine whether it is pushed into the background. However, my senior later raised a loophole in this way — when referring to the external SDK, and the external SDK can pop up activities that do not inherit our custom base class, then how to deal with this situation? Here is going to use the android SDK provides the life cycle of the class — Application. RegisterActivityLifecycleCallbaks, here we can talk later.