The ninth week of Android development learning summary

Write in front

After a long time, the pigeons were finally picked up in the face of the sprint. I played too hard in the past few weeks, and I forgot about the business. Start the formal study below.
These days I learned the content of Activity. It is one of the four major components of Android, arguably the most fundamental thing.

Understanding AndroidManifest.xml

This file exists to describe our project file. All four of our components need to be registered in the manifest file to work properly. As shown in the figure:

application is the full name of APP, representing our entire app project. We can set the APP icon, name and other attributes in it.
The following is the registration statement of activity. It contains intent filtering, used to set the application's startup entry, etc.

Interface jump

Explicit intent app jump

The implementation is very simple, the following code:

                //显式启动的第一种写法
                Intent intent = new Intent();
               intent.setClass(MainActivity.this,SecondActivity.class);
                startActivity(intent);

                //显式启动的第二种写法
               Intent intent1 = new Intent();
               intent1.setClassName(MainActivity.this,"com.test.activitiyleanring.SecondActivity");
               startActivity(intent1);

                //显式启动的第三种写法
               Intent intent = new Intent();
               ComponentName componentName = new ComponentName(MainActivity.this,SecondActivity.class);
              intent.setComponent(componentName);
             startActivity(intent);

In fact, under normal circumstances we all write this (the most used way):

Intent intent = new Intent(this,SecondActivity.classs);
startActivity(intent);

Implicit intent in-app Activity jump

                //隐式启动的第一种写法
                Intent intent = new Intent("abcd.SecondActivity");
                startActivity(intent);
                //隐式启动的第二种写法
                Intent intent = new Intent();
                intent.setAction("abcd.SecondActivity");
                startActivity(intent);

The so-called implicit intent is actually not clear which activity I want to start, and read from the activity directory.

The use of explicit intent and implicit intent?

Having said so much, I am also puzzled. Implicit intent is so troublesome, why use him? In fact, it is to start a third-party application (such as calling a mobile phone camera, etc.)
Then let's take a look at how to start a third-party application explicitly and implicitly:

Explicit intent to launch third-party applications

    /**
     * 这个方法会在点击按钮的时候执行
     *
     * @param view 这个其实是我们点击的button
     *             <p>
     *             其实组件的名称 ComponentName = 包名/类得路径名称,如果类和前面的包名一样就可以省略成.
     *             <p>
     *             以下这种方式是显式意图跳转到浏览器界面
     */
    public void skip2BrowserVisible(View view) {
        Log.d(TAG, "skip2Browser");
        Intent intent = new Intent();
        //第一种写法
        //intent.setClassName("com.android.chrome","com.android.browser.BrowserActivity");
        //第二种写法
        ComponentName componentName = new ComponentName("com.android.chrome", "com.android.browser.BrowserActivity");
        intent.setComponent(componentName);
        startActivity(intent);
    }

As you can see, we have to find the package name, etc., which is very troublesome.
What about implicit startup?

Implicit intent to launch third-party applications

   /**
     * 下面我们通过隐式意图跳转到浏览器界面
     * 步骤:
     * 1.创建Intent对象
     * 2.给Intent对象设置Action值,设置它的category值,如果5.1以上系统需要设置包名
     * 3.startActivity来跳转到另外一个界面
     *
     * @param v
     */
    public void skip2BrowserInvisible(View v) {
        Intent intent = new Intent();
//        <intent-filter>
//        <action android:name="android.intent.action.SEARCH" />
//        <category android:name="android.intent.category.DEFAULT" />
//        </intent-filter>
        intent.setAction("android.intent.action.SEARCH");
        intent.addCategory("android.intent.category.DEFAULT");
        intent.setPackage("com.android.browser");
        startActivity(intent);
    }

As you can see, implicit startup is relatively simple. This is why implicit startup is often used to start third-party applications.

Realize interface jump pass value

Pass by value

First look at the code:

    private void initListener() {
        mLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //这里面也就是登陆按钮被点击了
                Log.d(TAG,"login被点击了");
                handlerLogin();
            }
        });
    }

    private void handlerLogin() {
        String accountText = mAccount.getText().toString().trim();
        if (TextUtils.isEmpty(accountText)) {
            Toast.makeText(this,"输入的账号为空!",Toast.LENGTH_SHORT).show();
            return;
        }
        String passwordText = mPassword.getText().toString().trim();
        if (TextUtils.isEmpty(passwordText)) {
            Toast.makeText(this,"输入的密码为空!",Toast.LENGTH_SHORT).show();
            return;
        }
        //有密码有账号以后,我们把数据传到另外一个界面
        //先要创建一个意图对象,然后通过startActivity方法来跳转
        /**
         * 这部分是用于显式意图跳转到另外一个Activity
         */
        //Intent intent = new Intent(this,SecondActivity.class);
        Intent intent = new Intent();
        String packageName = this.getPackageName();
        String  targetActivityClsName = SecondActivity.class.getName();
        intent.setClassName(packageName,targetActivityClsName);
        Log.d(TAG,"packageName "+packageName);
        Log.d(TAG,"className "+ targetActivityClsName);

        intent.putExtra("account",accountText);
        intent.putExtra("password",passwordText);
        startActivity(intent);

As you can see, we use the putExtra method to put the value, and then we receive it in the second interface:

public class SecondActivity extends Activity {
    private static final String TAG = "SecondActivity";
    @Override
    protected void onCreate( Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        TextView info = this.findViewById(R.id.info);
        Intent intent = getIntent();
        String account = intent.getStringExtra("account");
        String password = intent.getStringExtra("password");
        Log.d(TAG,"account =="+account);
        Log.d(TAG,"password =="+password);
        info.setText("您的账号是:"+account+"  密码是:"+password);

    }
}

Similarly, objects, etc. can also be passed, but the user class must be serialized (implements Parcelable), then use the putExtra method

Callback

                Intent intent = new Intent(MainActivity.this,PayActivity.class);
                //第一步,使用startActivityForResult代替原来的startActivity
                startActivityForResult(intent,PAY_REQUEST_CODE);
    /**
     * 返回的结果会在这里回调
     * @param requestCode
     * @param resultCode
     * @param data
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == PAY_REQUEST_CODE) {
            String resultContent = null;
            if (resultCode == 2) {
                //充值成功
                resultContent = data.getStringExtra("resultContent");
            }else if(resultCode ==3){
                //充值失败
                resultContent = data.getStringExtra("resultContent");
            }
            mPayResultText.setText(resultContent);
        }
    }

The callback is a bit more troublesome. First, you must use the startActivityForResult method, and then overwrite the onActivityResult method to get the callback parameters.

Activity life cycle

I have also learned before that the onCreate onStart onResume onPause onStop onDestroy life cycle moves in order. Among them, we generally store data in the onDestroy method, and then restore it in the onCreate method.

The effect of horizontal and vertical screens on Activity life cycle

The horizontal and vertical screen will cause the Activity to be destroyed and restarted, which will cause some difficulties in development. At this time, we can fix it and configure it:

android:configChanges="keyboardHidden|screenSize|orientation"

Guess you like

Origin www.cnblogs.com/wushenjiang/p/12709229.html