文章目录

  • Activity
  • 认识
  • 概述:
  • 4种状态
  • 生命周期
  • 创建启动和关闭
  • 创建
  • 启动
  • 关闭Activity
  • 刷新当前Act
  • 多个Activity的使用
  • Bundle在Activity之间交换数据
  • 调用另一个Activity并返回结果
  • 使用Fragment
  • 概念/生命周期
  • 创建Fragment
  • 将Fragment添加到Activity中
  • 使用Fragment


Activity

  • 概述
  • 创建、配置、启动、关闭
  • 多个Activity的使用
  • 使用Fragment

认识

概述:

一个Android程序的一屏。

4种状态

  • 运行状态 正在展示的状态
  • 暂停状态 按下退出键,弹出是否退出的灰色状态,可见不可操作,一般弹出对话框都是暂停状态
  • 停止状态 确认退出 进入停止状态 不可见
  • 销毁状态 应用程序-强制停止-销毁状态 不可见

运行 -> 暂停 -> 停止 -> 销毁
运行 -> 停止
运行 -> 销毁

生命周期

android fragment 关闭自己 安卓关闭activity的方法_android

  • onCreate 生成
  • onStart 开始
  • onResume 运行
  • onPause 暂停
  • onStop 停止
  • onDestroy 销毁

在 onStop状态时候 可以 调用OnRestart方法 重新到onCreate后的状态 执行 onStart方法

创建启动和关闭

创建

  1. 创建继承于Activity的Activity
  2. 重写需要的回调方法 一般onCreate是需要冲写的其他的更具需求
  3. 在onCreate中设置需要使用的布局文件
  4. 配置Activity 在AndroidManifests中配置
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.liyanfeng.act">

    <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/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".DetailActivity"
            android:label="详情">
            
        </activity>
    </application>

</manifest>

或者通过Android Studio 右键 创建Activity的向导 以界面的方式创建

启动

  • 入口Activity 配置文件中配置
  • 其他Activity

配置入口Activity 子 配置文件的中添加

<intent-filter>
    <action android:name="android.intent.action.MAIN" />

    <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>

intent:意图

action:指定响应的动作名

category:指定在什么情况下动作会响应

在Java中通过startActivity() 启动其他Activity

Intent intent = new Intent(this, DetailActivity.class);
startActivity(intent);

关闭Activity

  • finish()

刷新当前Act

  • onCreate(null)

多个Activity的使用

Bundle在Activity之间交换数据

act1 -(intent)-> act2

intent 借助 Bundle 保存数据

Bundle 是 键值对类型的组合,可以保存键值对数据

通过Bundle在两个Activity中传递参数

布局一

<?xml version="1.0" encoding="utf-8"?>
<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"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!" />

    <Button
        android:id="@+id/btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按钮" />

    <EditText
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

布局二

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

    <TextView
        android:id="@+id/res2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <Button
        android:id="@+id/closeAct1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="关闭"
        />
</LinearLayout>

java 一

package com.liyanfeng.act;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity implements View.OnClickListener {

    private Button btn1;
    private EditText text;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // 创建
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        Log.i("生命周期", "onCreate");
    }

    @Override
    protected void onStart() {
        // 开始
        super.onStart();
        Log.i("生命周期", "onStart");

    }

    @Override
    protected void onResume() {
        // 运行
        super.onResume();
        Log.i("生命周期", "onResume");
    }

    @Override
    protected void onPause() {
        // 暂停
        super.onPause();
        Log.i("生命周期", "onPause");

    }


    @Override
    protected void onStop() {
        // 停止
        super.onStop();
        Log.i("生命周期", "onStop");

    }

    @Override
    protected void onDestroy() {
        // 销毁
        super.onDestroy();
        Log.i("生命周期", "onDestroy");

    }

    private void initView() {
        btn1 = (Button) findViewById(R.id.btn1);

        btn1.setOnClickListener(this);
        text = (EditText) findViewById(R.id.text);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn1:
                String textString = text.getText().toString().trim();
                if (TextUtils.isEmpty(textString)) {
                    Toast.makeText(this, "textString不能为空", Toast.LENGTH_SHORT).show();
                    return;
                } else {

                    Intent intent = new Intent(this, DetailActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putCharSequence("edit", textString);
                    intent.putExtras(bundle);
                    startActivity(intent);
                }

                break;
        }
    }

}

java 二

package com.liyanfeng.act;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.Nullable;
public class DetailActivity extends Activity implements View.OnClickListener {
    private Button closeAct1;
    private TextView res2;
    String res;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail);
        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        res = bundle.getString("edit");
        initView();
    }

    private void initView() {
        closeAct1 = (Button) findViewById(R.id.closeAct1);
        closeAct1.setOnClickListener(this);
        res2 = (TextView) findViewById(R.id.res2);
        res2.setText(res);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.closeAct1:
                finish();
                break;
        }
    }
}

调用另一个Activity并返回结果

使用startActivityForResult()方法

public viod startActivityForResult(Intent intent,int requestCode)

参数为 intent对象 和一个整型请求码,请求码标识请求来源,更具不同的请求码执行不容的操作

使用Fragment

概念/生命周期

Activity中内嵌的类似于Activity的可切换屏幕,Fragment必须被嵌入到Activity中,可以嵌入多个

当 Activity被暂停的时候Fragment也会被暂停,销毁的时候也会被销毁,只有当Activity正在运行的时候,才能对Fragment进行操作。

生命周期

android fragment 关闭自己 安卓关闭activity的方法_生命周期_02

  1. onAttac
  2. onCreate
  3. onCreateView
  4. onActivityCreate
  5. onStart
  6. onResume
  7. 运行状态
  8. onPause
  9. 暂停状态
  10. onStop
  11. 停止状态
  12. onDestroyView
  13. onDestroy
  14. onDetach
  15. 销毁状态

如果将Fragment添加到返回栈中,那么会在12步的时候回到3步,否则走红色销毁

返回栈:Activity的集合,具有先进后出的原则

创建Fragment

extends Fragment

extends XXXFragment

  1. 手动方式
  • 创建xxxFragment 类 继承于 Fragment
  • 重写onCreateView方法
  • 创建Fragment对应的布局文件 xxx.xml
  • 在onCreateView中加载布局文件 并返回view
package com.liyanfeng.fragmentff;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

public class ListFragment extends Fragment {
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_list, container, false);
        return view;
    }
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="Fragment内容" />

</LinearLayout>
  1. 通过ide 右键 界面的方式添加Fragment

将Fragment添加到Activity中

  1. 直接在布局文件中添加Fragment
  2. Java中添加Fragment

直接添加

<?xml version="1.0" encoding="utf-8"?>
<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="horizontal"
    tools:context=".MainActivity">

    <fragment
        android:id="@+id/list"
        android:name="com.包.ListFragment"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="1" />

    <fragment
        android:id="@+id/detail"
        android:name="com.包.DetailFragment"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="2" />
</LinearLayout>

Java动态添加

在ID为con的节点中添加Fragment

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="horizontal"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/title1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Activity" />

    <LinearLayout
        android:id="@+id/con"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/title1"
        android:orientation="vertical"></LinearLayout>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/con"
        android:text="Act" />

</RelativeLayout>
package com.liyanfeng.fragmentff;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentTransaction;

import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView(){
        // 实例化自定义的两个Fragment
        DetailFragment detailFragment = new DetailFragment();
        ListFragment listFragment = new ListFragment();
        // 实例化FragmentTransaction
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        // 在实例化FragmentTransaction中添加Fragment 第一个参数为添加到的id
        ft.add(R.id.con,detailFragment);
        ft.add(R.id.con,listFragment);
        //提交
        ft.commit();
    }
}

使用Fragment

实现个底部菜单栏界面