Review of learning difficulties in the four core components of Android studio

Keywords: Attribute Android

Activity

In an application, an activity can be used to represent an interface, the beginning of an activity, the start of an activity component, and the end of an activity represents the end of an activity's life cycle.

activity status performance
Resumed At the top of the stack, visible and interactive
Paused State when overridden by another transparent or Dialog style activity whose state is visible but not interactive
Stopped The current activity is overwritten by another activity, losing focus and not visible.
/**
     * After the onCreate method, it is used to display the secret, but the user can not interact.
     */
    @Override
    protected void onStart() {
        super.onStart();
        System.out.println("MainActivity-onStart");
    }

    /**
     * Called when an activity in the stopped state is returned, and then onResume is called
     */
    @Override
    protected void onRestart() {
        super.onRestart();
        System.out.println("MainActivity-onRestart");
    }

    /**
     * After the onStart method is invoked, the user can interact when the method is executed, and the current Activity enters the resumeed state.
     * When an activity in the paid state is returned, it will be called again to let the activity enter the running state
     */
    @Override
    protected void onResume() {
        super.onResume();
        System.out.println("MainActivity-onResume");
    }

    /**
     * When other activities (transparent or window mode) enter, the method is called to put the current activity in the paused state
     * The current activity is visible but not interactive. If other higher priority app s need memory, the current activity may be destroyed (kill)
     * The onResume method is called when the current activity is returned
     */
    @Override
    protected void onPause() {
        super.onPause();
        System.out.println("MainActivity-onPause");
    }

    /**
     * When other activities completely cover the activity, it will be called, and the current activity will enter the stopped state (stop state)
     * activity Invisible and non interactive. If other higher priority app s need memory, the current activity may be destroyed (kill)
     * The onStart method is called when the current activity is returned
     */
    @Override
    protected void onStop() {
        super.onStop();
        System.out.println("MainActivity-onStop");
    }


    /**
     * Called when the current activity is destroyed, usually used to release resources in this method. The current activity is destroyed
     */
    @Override
    protected void onDestroy() {
        super.onDestroy();
        System.out.println("MainActivity-onDestroy");
    }

Life cycle diagram of Activity

Activity transfer data

public void  sendObjClick(View v){
        //Objects to transfer
        Cat cat =new Cat();
        cat.name="Pikachu";
        cat.age=2;
        cat.type ="English short";

        Intent intent =new Intent(this,MainActivityB.class);
        intent.putExtra("cat",cat);
        startActivity(intent);

    }

    public void sendClick(View v){
        //Create an intent parameter: context, bytecode of the component to jump.
      Intent intent =new Intent(this,MainActivityB.class);
        String info =editText_info.getText().toString();
        //Encapsulate the data to be passed
       // Bundle data=new Bundle();
       // data.putString("info",info);
       // intent.putExtra("data",data);
        intent.putExtra("info",info);
        intent.putExtra("age",24);     //Package email
        startActivity(intent);      //Start a new Activity
    }

Action

 /**
     * Direct lookup (by component name)
     * @param v
     */
    public void componentClick(View v){
        Intent intent =new Intent(this,MainActivity2.class);
 //       ComponentName componentName =new ComponentName(this,MainActivity2.class);
  //      intent.setComponent(componentName);

        startActivity(intent);
    }

    /**
     *Using the indirect method (through the action attribute and category attribute)
     * The default category must be used in the manifest configuration of an Activity
     * @param v
     */
    public void actionClick(View v){
        Intent intent =new Intent();
        intent.setAction("xxq.action.MY_ACTION");
        intent.addCategory("xxq.category.MY_CATEGORY");
        startActivity(intent);

    }

    /**
     * data Property, usually used with action (this combination is the most common use method)
     * @param v
     */
    public void dataClick(View v){
        Intent intent =new Intent();
//        intent.setAction("xxq.action.MY_ACTION");
        intent.setAction(Intent.ACTION_VIEW);
        Uri data = Uri.parse("http://www.baidu.com");
//        intent.setData(data); sets the type property to null;
//        Int.settypetype); will set the data property to null;
        //To use data and Type, the following methods must be used. Matching pairs must be matched at the same time to pass;
        intent.setDataAndType(data,"text/html");
        startActivity(intent);

    }
    public void flagClick(View v){
        Intent intent =new Intent(this,MainActivity5.class);
        //Set the startup mode of activity
        //Intent.setflags (intent.flag ﹣ activity ﹣ new ﹣ task); start the activity in the new task stack, if any, then start it in this task
        //Flag? Activity? New? Task is equivalent to a single task
        //Flag? Activity? Clear? Top is equivalent to single top
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
    }

Bind Service

The application component (client) can bind the service by calling the bindService() method, and then the Android system will call the onBind() callback method of the service to return an IBinder object that interacts with the server.
The binding is asynchronous, bindService () returns immediately, and does not return the IBinder object to the client.
To receive the IBinder object, the client must create an instance of the ServiceConnection class and pass it to the bindService () method.
Note: only Activity, Service and content provider can bind services

/**
 * Binding service:
 * To achieve the function by binding the service:
 * 1.The client binds a service object through the bindService method. If the binding succeeds, it will call back to use the ServiceConnection port method onServiceConnected
 * 2.Expose business interfaces through Service components
 * 3.The server creates an *. aidl file to define a business interface that can be called by the client
 * An aidl file
 * (1)No modifiers, similar to interface writing
 * (2)Support type: basic data type, String, CharSequence, list < String >, map, custom type
 *         Custom type:
 *         Implement an aidl file to declare the type;
 *         For use in other aidl files, you must use import
 * 4.The server needs an implementation class of business interface, and usually uses the extensions stub class
 * 5.Return the bound object through the onBind method of the Service
 * 6.If the client binding is successful, it can call the remote business method just like calling its own method.
 *
 */

public class MyBoundService extends Service {
    public MyBoundService() {
    }

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public boolean onUnbind(Intent intent) {
        return super.onUnbind(intent);
    }

    @Override
    public IBinder onBind(Intent intent) {
       return  new IMImpl();
    }
}

Posted by jane on Sun, 08 Dec 2019 08:38:03 -0800