0. Know which activity the current interface is
Create a new BaseActivity class, let BaseActivity inherit AppActivity and override the onCreate() method. The code is as follows:
public class BaseActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); Log.d("BaseActivity",getClass().getSimpleName()); } }
We get the class name of the current instance in the onCreate() method and print it out through logcat.
Next, we need to make BaseActivity the parent of all activities in ActivityTest project, modify the inheritance structure of FirstActivity, SecondActivity and ThirdActivity so that they can no longer inherit from AppCompatActivity, but from BaseActivity. Because BaseActivity inherits from AppCompaActivity, the existing functions of all activities in the project will not be affected. View log information:
Whenever we enter an activity's interface, the activity's class name is printed.
1. Exit the procedure anytime, anywhere
Create a new Collection Class Activity Collector to manage all activities as an Activity Manager. The code is as follows:
public class ActivityCollector { public static List<Activity> activities = new ArrayList<>(); 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(); } } } }
In the activity manager, a list is used to temporarily survive an activity, then an addActivity() method is provided to add an activity to the List, a removeActivity() method is provided to delete an activity from the List, and finally a finishAll() method is provided to destroy all the activities stored in the List.
Next, modify the code in BaseActivity as follows:
public class BaseActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); Log.d("BaseActivity",getClass().getSimpleName()); ActivityCollector.addActivity(this); } @Override protected void onDestroy(){ super.onDestroy(); ActivityCollector.removeActivity(this); } }
In BaseActivity's onCreate() method, the addActivity() method of ActivityCollector is invoked, indicating that the currently created activity is added to the activity manager, and then the onDestroy() method is overridden in BaseActivity, and the removeActivity() method of ActivityCollector is invoked to indicate that an activity to be destroyed is removed from the activity manager.
2. Best Writing for Starting Activities
Assuming that two string parameters are required in Second Activity, they must be passed in when Second Activity is started
Traditional writing:
Intent intent = new Intent(FirstActivity.this,SecondActivity.class); intent.putExtra("param1","data1"); intent.putExtra("param2","data2"); startActivity(intent);
The problem is, if Second Activity is not written by you, but the part you are responsible for needs to start the function of Second Activity, and you don't know what data you need to transmit to start the activity, how to do it? Two ways, one is to look at the code of Second Activity, the other is to ask colleagues who wrote Second Activity. Hey, bother.
Best Writing:
public class SecondActivity extends BaseActivity{ public static void actionStart(Context context,String data1,String data2){ Intent intent = new Intent(context,SecondActivity.class); intent.putExtra("param1",data1); intent.putExtra("param2",data2); context.startActivity(intent); } }
An action Start () method is added to the Second Activity, in which the intent is constructed. In addition, all the data needed in the Second Activity are passed through the parameters of the action Start () method, and then stored in Intent. Finally, the startActivity() method is called to start the Second Activity.
The advantage of this is that the data required by Second Activity is reflected in the method parameters.