Activity of Android interview questions


Preface

Autumn recruits are coming soon, Ji Meng will prepare a set of Android primary interview questions before the end of the National Day holiday. I hope it will be helpful to everyone.


Tip: The following is the content of this article

One, Activity

1. What is Activity?

One of the four major components, generally, a user interaction interface corresponds to an activity

setContentView() ,// 要显示的布局 

Activity is a subclass of Context, which also implements window.callback and keyevent.callback, which can handle events that interact with the user of the form

button.setOnclickLinstener{
    
    
}//点击事件

The commonly used ones in my development are FragmentActivitiy, ListActivity, PreferenceActivity, TabAcitivty, etc... If the interface has common features or functions, I will also define a BaseActivity myself


2. Please describe the Activity life cycle

Activity has a variety of states from creation to destruction. From one state to another, the corresponding callback methods are triggered. These callback methods include:

onCreate onStart onResume onPause onStop onDestroy,onRestart

Most of these methods correspond to each other

onCreate 创建与 onDestroy 销毁
onStart 可见与 onStop 不可见
onResume 可编辑(即焦点)与 onPause

3. Please describe the four states of Activity

running-poused-stopped-killed

1.running->The activity currently displayed on the screen (at the top of the task stack), the user can see the state.
2. poused-> is still visible to the user, but the focus of the interface has been lost, and this Activity cannot interact with the user.
3.stopped->the user cannot see the current interface, and cannot interact with the user. It is completely covered.
4.killed->the current interface is destroyed, waiting for the system to be recycled


4. To transfer data between two activities, besides intent, broadcast receiver, content provider, what else?

1) Use static static data and public static member variables
2) Use external storage for transmission, such as File file storage

 SharedPreferences 首选项
 Sqlite 数据库

5. What is the difference between Context, Activity and Appliction in Android?

  • Same: Activity and Application are both subclasses of Context. Context
    literally means the meaning of context. In actual applications, it really plays the role of managing various parameters and variables in the context environment, so that we can easily access various resources.
  • Different: The maintenance life cycle is different. Context maintains the life cycle of the current Activity, and Application maintains the life cycle of the entire project. When using context, be careful of memory leaks to prevent memory leaks. Pay attention to the following aspects:

1. Don't let objects with a long life cycle refer to the activity context, that is, ensure that the object that references the activity has the same life cycle as the activity itself.

2. For objects with a long life cycle, application and context can be used.

3. Avoid non-static internal classes, try to use static classes, avoid life cycle problems, and pay attention to life cycle changes caused by internal classes' references to external objects.


6. What is Context?

1. It describes the information of an application environment, that is, the context.

2. This class is an abstract class. Android provides a concrete implementation class (ContextIml) of this abstract class.

3. Through it, we can obtain application resources and classes, as well as some application-level operations, such as starting an Activity, sending broadcasts, receiving Intents, information, etc.


7. How to save the status of Activity?

The status of Activity is usually automatically saved by the system. This function is only needed when we need to save additional data.

  • Generally speaking, the activity instance after calling the onPause() and onStop() methods still exists in the memory, and all the information and status data of the activity will not disappear. When the activity returns to the foreground, all changes will be retained.

  • But when the system memory is insufficient, the activity after calling the onPause() and onStop() methods may be destroyed by the system, at this time there will be no instance objects of the activity in the memory. If the activity returns to the foreground afterwards, the changes made before will disappear. In order to avoid this situation, we can override the onSaveInstanceState() method. The onSaveInstanceState() method accepts a Bundle type parameter. Developers can store state data in this Bundle object, so that even if the activity is destroyed by the system, when the user restarts the activity and calls its onCreate() method, the above-mentioned Bundle The object will be passed as an actual parameter to the onCreate() method, and the developer can retrieve the saved data from the Bundle object, and then use these data to restore the activity to the state before it was destroyed.

  • It should be noted that the onSaveInstanceState() method is not necessarily called, because some scenarios do not need to save state data. For example, when the user presses the BACK key to exit the activity, the user obviously wants to close the activity, but it is not It is necessary to save the data for the next recovery, that is, the onSaveInstanceState() method will not be called. If the onSaveInstanceState() method is called, the call will occur before the onPause() or onStop() method.

@Override
   protected void onSaveInstanceState(Bundle outState) {
    
    
    // TODO Auto-generated method stub super.onSaveInstanceState(outState);
}

8. Activity life cycle when switching between horizontal and vertical screens

The life cycle at this time is related to the configuration in the manifest file.

  • When the activity's android:configChanges is not set, the screen will be re-invoked for each life cycle. By default, the current activity is destroyed first, and then reloaded.

  • When setting the activity's android:configChanges="orientation|keyboardHidden|screenSize", each life cycle will not be called again when the screen is cut, only the onConfigurationChanged method will be executed.

Usually in game development, the orientation of the screen is hard-coded.


9. Which methods are inevitably executed when jumping between two activities?

Under normal circumstances, for example, there are two activities, called A and B.
When component B is activated in A, A will call the onPause() method, and then B will call onCreate(), onStart(), onResume().
At this time, B covers the form, and A will call the onStop() method. If B is a transparent or dialog box style, A's onStop() method will not be called.


10. How to set an Activity as a window style

You only need to configure the following properties for the Activity.

android:theme="@android:style/Theme.Dialog"

11. What is the difference between singletop and singletask in the four startup modes of Activity?

singleTop is similar to standard mode. The only difference is that when the jump object is the activity at the top of the stack (it should be understood as the activity seen by the user), the program will not generate a new activity instance, but jump directly to the existing stack The top activity instance. Take the above example, when Act1 is in singleTop mode, there is still only one instance in the stack after the jump is executed. If you press the return key now, the program will exit directly.


12. There are four startup modes for Activity. The general bookmark usage mode is singletop, so why not use singletask?

Both singleTask mode and singleInstance mode only create one instance. In this mode, no matter whether the jumped object is the activity at the top of the stack, the program will not generate a new instance (of course, the premise is that this instance already exists in the stack). This mode is quite useful. In future multi-activity development, multiple instances of the same page will often be generated due to the jump relationship. This is always a bit bad in user experience, and if you declare the corresponding activity as a singleTask mode , This problem will no longer exist. Activity on the home page is very commonly used


13. How to exit Activity? How to safely exit an Application that has called multiple activities?

1. Normally, users only need to press the return key to exit an activity. We write code to exit the activity and directly call the finish() method.

2. Record the opened Activity: record it every time an Activity is opened. When you need to exit, just close each Activity.

//伪代码
      List<Activity> lists ;// 在 application 全局的变量里面 lists = new ArrayList<Activity>();
      lists.add(this);
      for(Activity activity: lists)
      {
    
    
      activity.finish();
      }
       lists.remove(this);

3. Send a specific broadcast: When you need to end the application, send a specific broadcast. After each Activity receives the broadcast, it can be closed.

//给某个 activity 注册接受接受广播的意图

registerReceiver(receiver, filter)
//如果过接受到的是 关闭 activity 的广播 就调用 finish()方法 把当前的 activity finish()掉

4. Recursive exit: Use startActivityForResult when opening a new Activity, then add the flag yourself, handle it in onActivityResult, and close it recursively.

5. In fact, intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) can also be used to activate a new activity through the intent flag. At this time, if the activity already exists in the task stack, the system will kill all the activities on the activity. In fact, it is equivalent to the startup mode configured for Activity as SingleTop.


14. The relationship between Activity-Window-View

Activity: is one of the four major components of Android, responsible for interface display, user interaction and business logic processing;
Window: is the functional department responsible for interface display and interaction, which is equivalent to the subordinate of Activity, and the life cycle method of Activity is responsible for business processing;
View: is an element placed in the Window container, Window is the carrier of View, and View is the specific display of Window.

Activity realizes the display of view elements through Window. Window can be understood as a container that holds views one by one to perform specific display work.


About sorting out

When everything is sorted out, it will be sorted into pdf format for easy reading. The file is obtained as shown below (after October 8)


Insert picture description here

Guess you like

Origin blog.csdn.net/qq_42761395/article/details/108867634