• Android--->activity界面跳转,以及查看生命周期过程


    main.xml界面布局

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        >
        <TextView  
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content" 
            android:text="@string/hello"
            />
        <Button
            android:id="@+id/secondBtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Second"
            />
        <Button
            android:id="@+id/thirdBtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Third"
            />
    </LinearLayout>

    second.xml界面布局

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        >
    <TextView  
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:text="SecondActivity"
        />
    </LinearLayout>

    third.xml界面布局

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        >
    <TextView  
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:text="ThirdActivity"
        />
    </LinearLayout>

    第一个界面操作MainActivity.java

    package com.szy.activity;
    
    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    
    public class MainActivity extends Activity
    {
        private final static String TAG="MainActivity";
        private Button secondBtn=null;
        private Button thirdBtn=null;
        
        @Override
        public void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            secondBtn=(Button)findViewById(R.id.secondBtn);
            thirdBtn=(Button)findViewById(R.id.thirdBtn);
            secondBtn.setOnClickListener(listener);
            thirdBtn.setOnClickListener(listener);
            Log.i(TAG, "MainActivity-->onCreate");
        }
    
        @Override
        protected void onDestroy()
        {
            super.onDestroy();
            Log.i(TAG, "MainActivity-->onDestroy");
        }
    
        @Override
        protected void onPause()
        {
            super.onPause();
            Log.i(TAG, "MainActivity-->onPause");
            try
            {
                Thread.sleep(2000);
            } catch (InterruptedException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    
        @Override
        protected void onRestart()
        {
            super.onRestart();
            Log.i(TAG, "MainActivity-->onRestart");
        }
    
        @Override
        protected void onResume()
        {
            super.onResume();
            Log.i(TAG, "MainActivity-->onResume");
        }
    
        @Override
        protected void onStart()
        {
            super.onStart();
            Log.i(TAG, "MainActivity-->onStart");
        }
    
        @Override
        protected void onStop()
        {
            super.onStop();
            Log.i(TAG, "MainActivity-->onStop");
        }
        
        private OnClickListener listener=new OnClickListener()
        {
            
            public void onClick(View v)
            {
                Button btn=(Button)v;
                Intent intent=new Intent();
                switch (btn.getId())
                {
                case R.id.secondBtn:
                    intent.setClass(MainActivity.this, SecondActivity.class);
                    break;
                case R.id.thirdBtn:
                    intent.setClass(MainActivity.this, ThirdActivity.class);
                    break;
                }
                startActivity(intent);
            }
            
        };
        
    }

    第二个界面操作SecondActivity.java

    package com.szy.activity;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.util.Log;
    
    public class SecondActivity extends Activity {
        private static final String TAG = "MainActivity";
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.second);
            Log.i(TAG, "SecondActivity-->onCreate");
        }
    
        @Override
        protected void onDestroy()
        {
            super.onDestroy();
            Log.i(TAG, "SecondActivity-->onDestroy");
        }
    
        @Override
        protected void onPause()
        {
            super.onPause();
            Log.i(TAG, "SecondActivity-->onPause");
        }
    
        @Override
        protected void onRestart()
        {
            super.onRestart();
            Log.i(TAG, "SecondActivity-->onRestart");
        }
    
        @Override
        protected void onResume()
        {
            super.onResume();
            Log.i(TAG, "SecondActivity-->onResume");
        }
    
        @Override
        protected void onStart()
        {
            super.onStart();
            Log.i(TAG, "SecondActivity-->onStart");
        }
    
        @Override
        protected void onStop()
        {
            super.onStop();
            Log.i(TAG, "SecondActivity-->onStop");
        }
    }

    第三个界面操作ThirdActivity.java

    package com.szy.activity;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.util.Log;
    
    public class ThirdActivity extends Activity
    {
        private static final String TAG = "MainActivity";
    
        @Override
        public void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.third);
            Log.i(TAG, "ThirdActivity-->onCreate");
        }
    
        @Override
        protected void onDestroy()
        {
            super.onDestroy();
            Log.i(TAG, "ThirdActivity-->onDestroy");
        }
    
        @Override
        protected void onPause()
        {
            super.onPause();
            Log.i(TAG, "ThirdActivity-->onPause");
        }
    
        @Override
        protected void onRestart()
        {
            super.onRestart();
            Log.i(TAG, "ThirdActivity-->onRestart");
        }
    
        @Override
        protected void onResume()
        {
            super.onResume();
            Log.i(TAG, "ThirdActivity-->onResume");
        }
    
        @Override
        protected void onStart()
        {
            super.onStart();
            Log.i(TAG, "ThirdActivity-->onStart");
        }
    
        @Override
        protected void onStop()
        {
            super.onStop();
            Log.i(TAG, "ThirdActivity-->onStop");
        }
    }

    对三个界面activity注册一下

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.szy.activity"
          android:versionCode="1"
          android:versionName="1.0">
        <uses-sdk android:minSdkVersion="8" />
    
        <application android:icon="@drawable/icon" android:label="@string/app_name">
            <activity android:name=".MainActivity"
                      android:label="@string/app_name">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
            <activity android:name=".SecondActivity"
                      android:label="@string/app_name">
            </activity>
            <activity android:name=".ThirdActivity"
                      android:label="@string/app_name"
                      android:theme="@android:style/Theme.Dialog">
            </activity>
        </application>
    </manifest>
  • 相关阅读:
    shell之ping减少时间间隔&ping的次数&用IP1去ping IP2的技巧
    kali界面乱码解决方案记录
    win10子系统kali-linux安装图形化界面总结
    树莓派4 64bit 编译安装QT5.13.2 和 Redis Desktop Manager 2020.1-dev
    树莓派4 (8GB) RaspiOS 64 bit 入手配置流程 2020-06-10
    阿里巴巴Java开发手册(泰山版)个人阅读精简
    Java 8 新API Steam 流 学习笔记
    IDEA中maven项目部署到云服务器上(简易)
    收藏模块的设计
    js常用代码片段(更新中)
  • 原文地址:https://www.cnblogs.com/yihujiu/p/5721345.html
Copyright © 2020-2023  润新知