• Android 开发学习(四)


    1. Fragment 动态添加和管理


    其实对于fragment都是通过transaction来操作的!

    • 通过transaction调用各种方法来操作fragment。
    // fragment事务
    FragmentTransaction transaction = fragmentManager.beginTransaction();
    // 替换R.id.framelayout  fixme 其实对于fragment都是通过transaction来操作的!
    transaction.replace(R.id.framelayout,fragment);
    
    • 1
    • 2
    • 3
    • 4

    通过addToBackStack方法来实现一个栈的效果,进而达到一个返回键的时候出栈的退回的效果。

    activity_main.xml

    
    <LinearLayout
        tools:context=".MainActivity"
        android:orientation="vertical"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools">
    
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/btn"
            android:text="@string/change">
    
        Button>
    
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/btn2"
            android:text="@string/replace">
    
        Button>
    
        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/framelayout"
            android:background="@color/teal_200"
            >
    
        FrameLayout>
    
    LinearLayout>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35

    MainActivity

    package com.example.fragmentbase;
    
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.widget.Button;
    
    import androidx.appcompat.app.AppCompatActivity;
    import androidx.fragment.app.Fragment;
    import androidx.fragment.app.FragmentManager;
    import androidx.fragment.app.FragmentTransaction;
    
    import java.util.logging.Logger;
    
    public class MainActivity extends AppCompatActivity implements View.OnClickListener{
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            Button button = findViewById(R.id.btn);
            // 让MainActivity实现View.OnClickListener接口,进而直接传入this,就可以直接定义onClick方法。
            button.setOnClickListener(this);
    
            Button button2 = findViewById(R.id.btn2);
            button2.setOnClickListener(this);
        }
    
        @Override
        public void onClick(View view) {
            // view.getId()获取id值
            switch (view.getId()){
                case R.id.btn:
                    replaceFragment(new BlankFragment1());
                    break;
                case R.id.btn2:
                    replaceFragment(new ItemFragment2());
                    break;
            }
        }
    
        // 动态切换fragment
        private void replaceFragment(Fragment fragment) {
            // fragment管理器
            FragmentManager fragmentManager = getSupportFragmentManager();
            // fragment事务
            FragmentTransaction transaction = fragmentManager.beginTransaction();
            // 替换R.id.framelayout  fixme 其实对于fragment都是通过transaction来操作的!
            transaction.replace(R.id.framelayout,fragment);
            // 给当前transaction添加一个栈,参数是栈的名字。这样手机点击返回的时候就是根据出栈来折返fragment页面了。
            transaction.addToBackStack(null);
            // 事务提交
            transaction.commit();
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56

    BlankFragment1 和 ItemFragment2对象就是创建出来的fragment。

    2. Activity 与 Fragment通信(原生方案 Bundle)


    通过原生Bundle来进行通信。

    MainActivity

    // 通过使用bundle来传递值:
    Bundle bundle = new Bundle();
    bundle.putString("message","hello,world!");
    BlankFragment1 bf = new BlankFragment1();
    // 给bf对象设置bundle参数
    bf.setArguments(bundle);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    BlankFragment1

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        // 这样就可以直接该对象通过getArguments()方法来获取
        Bundle bundle = getArguments();
        String message = bundle.getString("message");
        Log.d(TAG,"onCreate:" + message);
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    bundle.putParcelable方法的使用:
    在这里插入图片描述

    3. 动态添加Fragment 5个步骤


    在这里插入图片描述

    4. Activity 与 Fragment通信(Java语言中类与类自己通信常用方法:接口)


    Java语言中类与类自己通信常用方法:接口。

    IFragmentCallback接口:

    package com.example.fragmentbase;
    
    public interface IFragmentCallback {
    
        void sendMsgToActivity(String string);
    
        String getMsgFromActivity(String msg);
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    BlankFragment1:在里面声明一个对应上面的接口即可。

    package com.example.fragmentbase;
    
    import android.os.Bundle;
    
    import androidx.fragment.app.Fragment;
    
    import android.util.Log;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.Button;
    import android.widget.Toast;
    
    
    public class BlankFragment1 extends Fragment {
    
        private static String TAG = "BlankFragment1";
    
        private View rootView;
    
        public BlankFragment1() {
        }
    
        // 直接声明一个对应的接口变量。
        private IFragmentCallback fragmentCallback;
    
        public void setFragmentCallback(IFragmentCallback callback){
            fragmentCallback = callback;
        }
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            // 这样就可以直接该对象通过getArguments()方法来获取
            Bundle bundle = getArguments();
            String message = bundle.getString("message");
            Log.d(TAG,"onCreate:" + message);
        }
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            if (rootView == null){
                rootView = inflater.inflate(R.layout.fragment_blank1, container, false);
            }
            Button btn3 = rootView.findViewById(R.id.btn_3);
            btn3.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    String msgFromActivity = fragmentCallback.getMsgFromActivity(null);
                    Toast.makeText(BlankFragment1.this.getContext(),msgFromActivity,Toast.LENGTH_SHORT).show();
                    fragmentCallback.sendMsgToActivity("hello,I’m from Fragment");
                }
            });
            return rootView;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57

    MainActivity:在里面声明接口对象并进行操作。

    • Toast.makeText(MainActivity.this,msg,Toast.LENGTH_SHORT).show();提示框的效果。
    package com.example.fragmentbase;
    
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.widget.Button;
    import android.widget.Toast;
    
    import androidx.appcompat.app.AppCompatActivity;
    import androidx.fragment.app.Fragment;
    import androidx.fragment.app.FragmentManager;
    import androidx.fragment.app.FragmentTransaction;
    
    import java.util.logging.Logger;
    
    public class MainActivity extends AppCompatActivity implements View.OnClickListener{
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            Button button = findViewById(R.id.btn);
            // 让MainActivity实现View.OnClickListener接口,进而直接传入this,就可以直接定义onClick方法。
            button.setOnClickListener(this);
    
            Button button2 = findViewById(R.id.btn2);
            button2.setOnClickListener(this);
        }
    
        @Override
        public void onClick(View view) {
            // view.getId()获取id值
            switch (view.getId()){
                case R.id.btn:
                    Bundle bundle = new Bundle();
                    bundle.putString("message","hello,world!");
                    BlankFragment1 bf = new BlankFragment1();
                    // 给bf对象设置bundle参数
                    bf.setArguments(bundle);
                    bf.setFragmentCallback(new IFragmentCallback() {
                        @Override
                        public void sendMsgToActivity(String msg) {
                            // 传递一个上下文参数
                            Toast.makeText(MainActivity.this,msg,Toast.LENGTH_SHORT).show();
                        }
                        @Override
                        public String getMsgFromActivity(String msg) {
                            return "hello,world!!!!!";
                        }
                    });
                    replaceFragment(bf);
                    break;
                case R.id.btn2:
                    replaceFragment(new ItemFragment2());
                    break;
            }
        }
    
        // 动态切换fragment
        private void replaceFragment(Fragment fragment) {
            // fragment管理器
            FragmentManager fragmentManager = getSupportFragmentManager();
            // fragment事务
            FragmentTransaction transaction = fragmentManager.beginTransaction();
            // 替换R.id.framelayout  fixme 其实对于fragment都是通过transaction来操作的!
            transaction.replace(R.id.framelayout,fragment);
            // 给当前transaction添加一个栈,参数是栈的名字。这样手机点击返回的时候就是根据出栈来折返fragment页面了。
            transaction.addToBackStack(null);
            // 事务提交
            transaction.commit();
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73

    5. Fragment生命周期


    对应生命周期如下:
    在这里插入图片描述


    几个生命周期对应的几个操作:
    在这里插入图片描述


    总结:
    在这里插入图片描述

    6. ViewPager2的使用(场景:页面来回切换)


    android有两种一个是ViewPager和ViewPager2。推荐使用ViewPager2。

    • ViewPager2具有懒加载的功能。
      在这里插入图片描述

    activity_main.xml: 先声明一个ViewPager2

    
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        xmlns:android="http://schemas.android.com/apk/res/android" >
    
        <androidx.viewpager2.widget.ViewPager2
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/viewPager"
            >
        androidx.viewpager2.widget.ViewPager2>
    
    LinearLayout>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    MainActivity:

    • 配置一个adapter。
    package com.example.viewpagerandfragment;
    
    import androidx.appcompat.app.AppCompatActivity;
    import androidx.viewpager2.widget.ViewPager2;
    
    import android.os.Bundle;
    
    public class MainActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            ViewPager2 viewPager2 = findViewById(R.id.viewPager);
            ViewPagerAdapter viewPagerAdapter = new ViewPagerAdapter();
            // 配置viewPager2的adapter
            viewPager2.setAdapter(viewPagerAdapter);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    item_pager.xml:配置每一页的控件。

    
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/container">
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:textColor="#ff4532"
            android:textSize="32dp"
            android:text="hello"
            android:id="@+id/tvTitle" />
    
    RelativeLayout>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    ViewPagerAdapter:

    • 配置holder。
    • 配置list的数据。
    • 在onCreateViewHolder中,创建并返回hoder对象,并且指定好对应的控件组件(对应上面的item_pager.xml)。
    • 在onBindViewHolder中,操作控件的操作。
    package com.example.viewpagerandfragment;
    
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.RelativeLayout;
    import android.widget.TextView;
    
    import androidx.annotation.NonNull;
    import androidx.recyclerview.widget.RecyclerView;
    
    import java.util.ArrayList;
    import java.util.List;
    
    // ViewPagerAdapter.ViewPagerViewHolder 对应 ViewPagerAdapter类中的ViewPagerViewHolder类。
    public class ViewPagerAdapter extends RecyclerView.Adapter<ViewPagerAdapter.ViewPagerViewHolder> {
    
        // 用来改变页面内容的data数据
        private List<String> titles = new ArrayList<>();
    
        // 改变界面颜色值
        private List<Integer> colors = new ArrayList<>();
    
        public ViewPagerAdapter(){
            for (int i = 0; i < 10; i++) {
                titles.add("itholmes" + i);
                if (i % 2 == 0){
                    colors.add(R.color.black);
                } else {
                    colors.add(R.color.blue);
                }
            }
        }
    
        @NonNull
        @Override
        public ViewPagerViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            // LayoutInflater.from(parent.getContext()).inflate(R.layout.item_pager,parent,false) 获取item_pager对应的layout。
            return new ViewPagerViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_pager,parent,false));
        }
    
        // position来指定item的位置。
        @Override
        public void onBindViewHolder(@NonNull ViewPagerViewHolder holder, int position) {
            // 设置TextView的文本内容
            holder.mTv.setText(titles.get(position));
            // 设置背景颜色 , 注意因为这里是用的colors里面的配置内容所以要用setBackgroundResource方法来定义。
            holder.mContainter.setBackgroundResource(colors.get(position));
        }
    
        // 这里不能一直返回0,如果一直返回0那么就没有页面。一般设置为对应data的list的size。
        @Override
        public int getItemCount() {
            return 10;
        }
    
        // 继承一个RecyclerView.ViewHolder,同样操作holder。
        class ViewPagerViewHolder extends RecyclerView.ViewHolder{
    
            TextView mTv;
    
            RelativeLayout mContainter;
    
            public ViewPagerViewHolder(@NonNull View itemView) {
                super(itemView);
                mContainter = itemView.findViewById(R.id.container);
                mTv = itemView.findViewById(R.id.tvTitle);
            }
    
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72

    7. Fragment 与 ViewPager 联合操作(经常使用!)


    模拟微信下方的导航栏:


    总结:
    在这里插入图片描述

    8. 什么是Activity?


    可以理解为每打开一个应用就相当于打开了一个activity。

    在这里插入图片描述

    9. Activity的创建


    默认的Activity:
    在这里插入图片描述
    如下:

    
    <manifest xmlns:android="http://schemas.android.com/apk/res/android">
    
        <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:roundIcon="@mipmap/ic_launcher_round"
            android:supportsRtl="true"
            android:theme="@style/Theme.Demo01">
            
            <activity
                android:name=".MainActivity"
                android:exported="true">
                
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                intent-filter>
    
                <meta-data
                    android:name="android.app.lib_name"
                    android:value="" />
            activity>
        application>
    
    manifest>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    两种创建Activity的方式:

    第一种:直接new一个,创建一个Activity:
    在这里插入图片描述

    第二种:手动创建。

    • 1.创建类继承AppCompatActivity。
    package com.example.activityjudge;
    
    import androidx.appcompat.app.AppCompatActivity;
    
    import android.os.Bundle;
    
    // 继承AppCompatActivity
    public class MainActivity2 extends AppCompatActivity {
    
        // 实现onCreate
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            // 指定layout
            setContentView(R.layout.activity_main2);
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 2.创建类对应的layout。
    
    <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity2">
    
    androidx.constraintlayout.widget.ConstraintLayout>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 3.要在AndroidManifest.xml清单文件中,进行注册。
    
    <manifest xmlns:android="http://schemas.android.com/apk/res/android">
    
        <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:roundIcon="@mipmap/ic_launcher_round"
            android:supportsRtl="true"
            android:theme="@style/Theme.Demo01">
            
            <activity
                android:name=".MainActivity"
                android:exported="true">
    
                
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                intent-filter>
    
                <meta-data
                    android:name="android.app.lib_name"
                    android:value="" />
            activity>
            
            <activity
                android:name=".MainActivity2">
            activity>
        application>
    
    manifest>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33

    10. Activity之间的跳转


    MainActivity:

    • startActivity(new Intent(this,MainActivity2.class));来进行跳转。
    package com.example.activityjudge;
    
    import androidx.appcompat.app.AppCompatActivity;
    
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.View;
    
    // 默认的activity
    public class MainActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
        }
    
        // 跳转第二个activity的点击事件
        public void startActivity2(View view){
            // 进行跳转
            startActivity(new Intent(this,MainActivity2.class));
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    activity_main.xml布局

    
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context=".MainActivity">
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="我是第一个Activity"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    
        <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="startActivity2"
            android:text="跳转activity2" />
    
    
    LinearLayout>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    MainActivity2:要跳转到的activity。

    package com.example.activityjudge;
    
    import androidx.appcompat.app.AppCompatActivity;
    
    import android.os.Bundle;
    
    // 继承AppCompatActivity
    public class MainActivity2 extends AppCompatActivity {
    
        // 实现onCreate
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            // 指定layout
            setContentView(R.layout.activity_main2);
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    activity_main2.xml

    
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context=".MainActivity2">
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="我是第二个Activity"/>
    
    LinearLayout>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    别忘记在清单里面注册:

    
    <manifest xmlns:android="http://schemas.android.com/apk/res/android">
    
        <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:roundIcon="@mipmap/ic_launcher_round"
            android:supportsRtl="true"
            android:theme="@style/Theme.Demo01">
            
            <activity
                android:name=".MainActivity"
                android:exported="true">
    
                
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                intent-filter>
    
                <meta-data
                    android:name="android.app.lib_name"
                    android:value="" />
            activity>
            
            <activity
                android:name=".MainActivity2">
            activity>
        application>
    
    manifest>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33

    11. Activity的生命周期


    其实和fragment差不多。
    在这里插入图片描述

    要注意的就是一个activity跳转到另外一个activity会经过那些周期函数:

    • 仅仅会走onPause和onStop两个函数。
  • 相关阅读:
    【前端设计模式】之代理模式
    掌握这些你也可以拿offer到手软,让Java面试官佩服的没话说!
    把简单留给用户,把复杂交给 AI
    分开的两个程序使用共同的mysql,一端更新了表,另一端怎么及时更新缓存,使用mybatis
    TensorRT C# API 项目更新 (1):支持动态Bath输入模型推理
    MySQL基础篇之多表查询(内连接、外连接、自连接、子查询、union)
    Web服务器与Apache(虚拟主机基于ip、域名和端口号)
    2018-Adversarial Learning for Semi-Supervised Semantic Segmentation
    CSS平面转换
    遥感IDL二次开发(大气校正)
  • 原文地址:https://blog.csdn.net/IT_Holmes/article/details/127625748