"The First Line of Code" Study Notes 3: Exploring Activities

There are four states of activity:

1. Running state: An activity is in a running state when it is at the top of the return stack.

2. Pause state: When the activity is not on the top of the stack, but the activity is still visible, the activity belongs to the suspended state. For example, activity A calls an activity in the form of a dialog box, and the activity has not yet filled the screen. At this time, activity A is suspended. condition.

3. Stop state: When an activity is not on the top of the stack and is not visible, it enters the stop state.

4. Destroyed state: An activity becomes the destroyed state when it is removed from the back stack.


7 callback methods in the Activity class:     protected   

1.onCreate(): activity initialization

2.onDestroy(): called before the activity is destroyed

3.onStart(): Called when the activity changes from invisible to visible

4.onStop(): Called when the activity is completely invisible

5.onResume(): The activity returns to the top of the stack

6.onPause(); Called when another activity is started or resumed

7.onRestart(): The activity is restarted.



Active start mode

1.standard: The default launch mode, each time an activity is launched, a new activity will be created

2. singleTop: If the activity is on the top of the stack, calling this activity does not need to recreate an activity, but if it is not on the top of the stack, it still needs to

3.singleTask: If there is this activity in the return stack, there is no need to re-create an activity to start this activity, but it will destroy all the activities above the activity stack to be started.

4.singleInstance: Start an activity in this mode, and a separate stack will be created to manage this activity.



Know which event is currently on

public class BaseActivity extends Activity {
	//This code is to better know which activity is currently in based on the current interface
	protected void onCreate(Bundle savedInstanceState){
		super.onCreate (savedInstanceState);
		Log.d("BaseActivity",getClass().getSimpleName());}

The main thing is to create this class, then let all classes inherit this class, and then use getClass. getSimpleName() method to get the current activity name.


Exit the program at any time

It seems to be an exit function

public class ActivityCollector {
	public static List<Activity>activities =new ArrayList<Activity>();
	
	public static void addActivity(Activity activity){
		activities.add(activity);
	}
	
	public static void removeActivity(Activity activity){
		activities.remove(activity);
	}
	
	public static void finishAll(){
		for(Activity activity:activities){
			if(!activity.isFinishing()){
				activity.finish();
			}
			
		}
	}

}
 
 

Best way to start an activity

Write a static method to start your own in the method to be started, and then start the method to be started through the class name + method name in the current activity

//The method of starting SecondActivity, which is convenient for other activities to call and transfer data
		public static void actionStart(Context context,String data1,String data2){
			Intent intent = new Intent(context,SecondActivity.class);
			intent.putExtra("paraml1",data1);
			intent.putExtra("paraml2",data2);
			context.startActivity(intent);
		}
Convenient docking, making it easier for others to know what parameters you want to pass



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324602194&siteId=291194637