[Android] Transfer data between activities, Activity declaration cycle

One, transfer data between activities

1. How to transfer data?

Intent intent = new Intent(MainActivity.this, SplashActivity.class);
intent.putExtra("title", title);
startActivity(intent);

2. What data can be transmitted?

intent.putExtras(String, type)中的type:

byte
char
short
int
long
float
double
boolean
String 
Bundle
Parcelabel
CharSequence
Serializable(以上都有对应的数组类型,但Serializable无数组类型)

3. Can objects be passed?

can. Serialize when defining the class:

package com.jsc4.aboutactivity;

import java.io.Serializable;

public class UserInfo implements Serializable{
    
     // 这里要序列化implements Serializable,new出的对象就是序列化对象
    private String mUserName;
    private int mAge;

    public String getmUserName() {
    
    
        return mUserName;
    }

    public void setmUserName(String mUserName) {
    
    
        this.mUserName = mUserName;
    }

    public int getmAge() {
    
    
        return mAge;
    }

    public void setmAge(int mAge) {
    
    
        this.mAge = mAge;
    }

    public UserInfo(String userName, int age){
    
    
        mUserName = userName;
        mAge = age;
    }
}
intent.putExtra("userInfo", userInfo);  // 装包塞入
UserInfo userInfo = (UserInfo) intent.getSerializableExtra("userInfo");  // 拆包拿出,要用强制类型转换(UserInfo)

4. Can Activity return data?

startActivity(intent);
变成:
startActivityForResult(intent, REQUEST_CODE);startActivityForResult()函数配套使用的接收函数如下:
 @Override
    protected void onActivityResult(int requestCode, 
                                    int resultCode, 
                                    @Nullable Intent data) {
    
    
        super.onActivityResult(requestCode, resultCode, data);
        Log.i(TAG, "onActivityResult: requestCode="+requestCode
              +"  resultCode="+ resultCode);
        if(requestCode == REQUEST_CODE && 
           resultCode == SplashActivity.RESULT_CODE){
    
    
            if(data != null){
    
    
                String title = data.getStringExtra("title");
                textView.setText(title);
            }
        }
    }

5. Execute the task once after a delay of 1s

import android.os.Handler;

Handler mhandler = new Handler();

mhandler.postDelayed(new Runnable() {
    
    
            @Override
            public void run() {
    
    
                // 执行的内容
            }
        }, 1000);//延时1s后执行

Second, the life cycle of Activity

1. Methods involved in the life cycle

onCreate()—Newborn

onStart()-cry

onResume()—conscious

onPause()—Pause (there are other applications covered on it)

onStop()-no longer visible

onDestroy()—Destroyed

onReStart()—restarted

2. The execution process of the method:

When entering the interface for the first time, the interface will execute:

onCreate
onStart
onResume

Jump to another interface, the interface will execute:

onPause
onStop

Jump back from another interface, this interface will execute:

onRestart
onStart
onResume

Click the HOME button to return to the desktop, the interface will execute:

onPause
onStop

Click the icon to enter the interface again, the interface will execute

onRestart
onStart
onResume

Completely exit, the interface will execute:

onPause
onStop
onDestroy

Guess you like

Origin blog.csdn.net/qq_30885821/article/details/108718897