第四章activity
1.生命周期方法
onResume获取焦点
onPause失去焦点
package com.example.lifestyle;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.i("MainActivity","调用onCreate方法");
}
@Override
protected void onStart() {
super.onStart();
Log.i("MainActivity","调用onStart方法");
}
@Override
protected void onResume() {
super.onResume();
Log.i("MainActivity","调用onResume方法");
}
@Override
protected void onPause() {
super.onPause();
Log.i("MainActivity","调用onPause方法");
}
@Override
protected void onStop() {
super.onStop();
Log.i("MainActivity","调用onStop方法");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.i("MainActivity","调用onDestroy方法");
}
@Override
protected void onRestart() {
super.onRestart();
Log.i("MainActivity","调用onRestart方法");
}
}
目的是显示这些方法的顺序
第一个Log会红色需要alt+enter
打开程序时方法进行顺序
onCreate->onStart->onResume
退出程序时
onPause->onStop->onDestroy
单个程序无法重启onRestart
2.创建activity
方法一:自动生成
取新activity名称和并创建对应布局layout
方法二:手动写java文件创建activity(其实activity就是java文件)但这样只增加了activity,但layout没增加
方法在存放activity的包下创建java文件
记得继承时导包 ,alt+shift+enter
干完这些以后最重要的是在manifest文件里配置新增加的activity(注册)
方法是
<activity android:name=".新增加的activity名"></activity>
黄色部分也可以写这个activity所在包名.新增加的activity名
3.开启关闭activity
在mainactivity里写
mainactivity
package com.example.oc;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.start).setOnClickListener(this);
findViewById(R.id.finish).setOnClickListener(this);
}
@Override
public void onClick(View v) {
if(v.getId()==R.id.start){
Intent intent=new Intent(MainActivity.this,activitytwo.class);
startActivity(intent);}
else if(v.getId()==R.id.finish){
finish();
}
}
}
activity_main
<?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"
>
<Button
android:id="@+id/start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="start"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="finish"
android:id="@+id/finish"/>
</LinearLayout>
总结:
重点俩函数
(1)开启activity:
Intent 名称=new Intent(上下文.this,要打开的activity名称.class);
startActivity(名称);
还有一种方法
Intent 名称=new Intent();
名称.setClass(上下文.this,要打开的activity名称.class);
startActivity(名称);
(2)关闭activity
finish();
除此之外需要在onCreate函数里先设置监听器 findViewById(R.id.start).setOnClickListener(this);
然后写点击函数
onClick函数
public void onClick(View v) {
if(v.getId()==R.id.start){
Intent intent=new Intent(MainActivity.this,activitytwo.class);
startActivity(intent);}
else if(v.getId()==R.id.finish){
finish();
} }
4.显式intent,隐式intent
(1)显式intent
与开启关闭intent用同法
Intent 名称=new Intent(上下文.this,要打开的activity名称.class);
startActivity(intent);
(2)隐式intent
Intent n=new Intent();n.setAction("thirdact");//这个引号里面填的是manifest里面为这个要打开的activity设置的action可以设置任意值
startActivity(n);
Mainactivity
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.start1).setOnClickListener(this);
findViewById(R.id.start2).setOnClickListener(this);
}
@Override
public void onClick(View v) {
if(v.getId()==R.id.start1){
//显示意图打开
Intent i=new Intent(MainActivity.this,secondactivity.class);
startActivity(i);
}
else if(v.getId()==R.id.start2){
Intent n=new Intent();
n.setAction("thirdact");
startActivity(n);
}
}
}
activity_main
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:orientation="vertical"
>
<Button
android:id="@+id/start1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="显示意图"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/start2"
android:text="隐式意图"
/>
</LinearLayout>
manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.MyApplication"
tools:targetApi="31">
<activity
android:name=".thirdactivity"
android:exported="false" >
<intent-filter>
<action android:name="thirdact" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".secondactivity"
android:exported="false" />
<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>
</activity>
</application>
</manifest>
注意要为隐式意图设置category,action
category里面要设置成default
action的name可以设置任何值
<action android:name="thirdact" />
<category android:name="android.intent.category.DEFAULT" />
5.数据传递
方法一:putExtra
在MainActivity里
package com.example.myapplication;import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.start1).setOnClickListener(this);
findViewById(R.id.start2).setOnClickListener(this);
}
@Override
public void onClick(View v) {
if(v.getId()==R.id.start1){
//显示意图打开
Intent i=new Intent();
i.setClass(MainActivity.this,secondactivity.class);
//传递数据(传过去)
i.putExtra("stuname","wmy");
i.putExtra("num",1);
startActivity(i);
}
else if(v.getId()==R.id.start2){
Intent n=new Intent();
n.setAction("thirdact");
startActivity(n);
}
}
} 总结:intent的名称.putExtra("变量名",值); 收到数据 在secondactivity.java里写 package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;
public class secondactivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_secondactivity);
Intent f=getIntent();
String stn=f.getStringExtra("stuname");
int a=f.getIntExtra("num",0);
Toast.makeText(secondactivity.this,stn+a,Toast.LENGTH_LONG).show();
}
} 总结:收数据 显获取当前Intent getIntent 如何当前intent名称.get数据类型Extra("变量名在来源地定义的"); 方法二:Bundle(数据集装箱)
Mainactivity.java
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.start1).setOnClickListener(this);
findViewById(R.id.start2).setOnClickListener(this);
}
@Override
public void onClick(View v) {
if(v.getId()==R.id.start1){
//显示意图打开
Intent i=new Intent();
i.setClass(MainActivity.this,secondactivity.class);
//传递数据
Bundle s=new Bundle();
s.putString("stn","小明");
s.putInt("num",1);
i.putExtras(s);
startActivity(i);
}
else if(v.getId()==R.id.start2){
Intent n=new Intent();
n.setAction("thirdact");
startActivity(n);
}
}
}
放入集装箱
Bundle 名称=new Bundle();
Bundle名称.put数据类型(变量名,值);//可能很多句
intent名称.putExtras(Bundle名称);
secondactivity.java
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;
public class secondactivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_secondactivity);
Bundle h=getIntent().getExtras();
String n=h.getString("stn");
int m=h.getInt("num");
Toast.makeText(secondactivity.this,n+m,Toast.LENGTH_LONG).show();
}
}
取出集装箱
Bundle 名称=getIntent().getExtras();//获取当前intent的Bundle
Bundle名称.get数据类型(变量名);
6.数据回传
例如:
先从mainactivity跳向另一个activity再带着从哪个activity弄来的数据跳回来
描述:
从mainactivity点击按钮先跳向sj,在sj里点击按钮传入数据,带回mainactivity并用Toast显示
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<Button
android:id="@+id/a1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="数据回传"
android:onClick="click"
/>
</LinearLayout>
Mainactivity.java
package com.example.sjhch;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void click(View v){
Intent i=new Intent(MainActivity.this, sj.class);
startActivityForResult(i,1);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==1&&resultCode==2){
String h=data.getStringExtra("studentname");
Toast.makeText(MainActivity.this,h,Toast.LENGTH_LONG).show();
}
}
}
activity_sj.xml
<?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"
>
<Button
android:id="@+id/out"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="数据回"/>
</LinearLayout>
sj.java
package com.example.sjhch;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class sj extends AppCompatActivity implements View.OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sj);
findViewById(R.id.out).setOnClickListener(this);
}
@Override
public void onClick(View v) {
Intent m=new Intent();
m.putExtra("studentname","abc");
setResult(2,m);
finish();
}
}
总结:
其实就三个函数
1.从mainactivity跳向sj 函数一 startActivityForResult
这个intent使用显式法创建
Intent i=new Intent(MainActivity.this, sj.class);startActivityForResult(intent名称,请求码整型自定义值)
2.从sj带着数据调回mainactivity 函数二 setResult(返回码自定义整数,intent名称);
setResult(intent名称,返回码自定义整数);
这里装入数据是这么弄(创个intent,然后putExtra)
Intent m=new Intent();//(里面不用写)m.putExtra("studentname","abc");
setResult以后记得关闭当前界面返回主界面
finish()
3.在返回主界面显示结果 函数三 onActivityResult(请求码,返回码,intent名称)
7.activity启动模式
标签:void,Bundle,Intent,activity,import,android,第四章 From: https://www.cnblogs.com/luckyhappyyaoyao/p/18145117