"First Line of Code" Basic Summary

The life cycle of activity - the first line of code reading notes

 

1. Dialog activity

 The registration code of DialogActivity is a little different. It uses an android:theme attribute, which is used to specify a theme for the current activity. There are many built-in themes in the Android system to choose from. Of course, we can also customize our own themes, and here @android: style/Theme.Dialog is no doubt to let DialogActivity use a dialog-style theme.

<activity android:name=".NormalActivity" ></activity>

<activity android:name=".DialogActivity" android:theme="@android:style/Theme.Dialog" ></activity>

 

2. Save offline data before the activity is recycled (stop)

The onSaveInstanceState() method will carry a parameter of type Bundle

After the activity is recycled, re-entering will re-enter the oncreate() method

 

Add the following code in MainActivity to save temporary data:

@Override
protected void onSaveInstanceState(Bundle outState) {
	super.onSaveInstanceState(outState);
	String tempData = "Something you just typed";
	outState.putString("data_key", tempData);
}

 

The data has been saved, so where should we restore it? In fact , the onCreate() method we have been using actually has a parameter of type Bundle . This parameter is usually null, but when the data is saved by the onSaveInstanceState() method before the activity is reclaimed by the system, this parameter will contain all the previously saved data , and we only need to pass the corresponding value. method to extract the data.

 

Modify the onCreate() method of MainActivity as follows:

@Override

protected void onCreate(Bundle savedInstanceState) {
	super.onCreate (savedInstanceState);
	requestWindowFeature(Window.FEATURE_NO_TITLE);
	setContentView(R.layout.activity_main);	

	if (savedInstanceState != null) {
		String tempData = savedInstanceState.getString("data_key");
		Log.d (TAG, tempData);
	}
	……
}

 After taking out the value, you can do the corresponding recovery operation, for example, reassign the text content to the text input box

 

3. Active start mode:

Configure android:launchMode="" in activity activity

standard

        (default boot mode)

        A new instance of the activity is created and placed on the top of the stack each time it is started

 

singleTop

        When starting, if it is found that the activity is already on the top of the stack, it will be used directly and will not be created .

        If the activity is not at the top of the stack, you still need to create a new one

 

singleTask

        (Let only one instance of an activity exist in the context of the entire application)

        启动时,检查栈中是否有该活动,有则直接使用(onRestart()),并将其上的所有活动出栈(onDestroy())

        其他活动onDestory(),该活动onRestart();

 

singleInstance

        (指定为 singleInstance 模式的活动会启用一个新的返回栈来管理这个活动)

        场景:程序中的一个活动允许其他活动调用,其他程序和我们的程序共享该活动的实例

        每个应用程序都有自己的返回栈,同一活动在不同的返回栈中入栈必然会创建新的实例,该模式可以解决这个问题。

        该模式下会有一个单独的返回栈来管理这个活动,不论哪个程序来访问这个活动,都公用同一个返回栈。

 

singleInstance:

按下 Back 键进行返回,你会发现 ThirdActivity 竟然直接返回到了 FirstActivity,再按下 Back 键又会返回到 SecondActivity,再按下 Back 键才会退出程序,这是为什么呢?

其实原理很简单,由于 FirstActivity 和 ThirdActivity 是存放在同一个返回栈里的,当在ThirdActivity 的界面按下 Back 键,ThirdActivity 会从返回栈中出栈,那么 FirstActivity 就成

为了栈顶活动显示在界面上,因此也就出现了从 ThirdActivity 直接返回到 FirstActivity 的情况。然后在 FirstActivity 界面再次按下 Back 键,这时当前的返回栈已经空了,于是就显示了

另一个返回栈的栈顶活动,即 SecondActivity。最后再次按下 Back 键,这时所有返回栈都已经空了,也就自然退出了程序。 

 

Android栈:

1.一个应用程序一般都是由多个activity组成的。

2.任务栈(task stack)(别名back stack后退栈) 记录存放用户开启的activity的。

3.一个应用程序一被开启系统就给他分配一个任务栈,当所有的activity都退出的时候,任务栈就清空了。

4.任务栈的id是一个integer的数据类型 自增长的

5.在android操作系统里面会存在多个任务栈,一个应用程序一个任务栈。

6.桌面应用和一般的应用程序是一样的,任务栈的行为也是一样。

7.默认情况下, 关闭掉一个应用程序,清空了这个应用程序的任务栈。应用程序的进程还会保留。

 

Android用栈存放activity

启动一个新的活动,入栈,并处于栈顶。

销毁一个活动时,栈顶活动出栈。 

系统总是会显示处于栈顶的活动给用户,即栈顶活动可见。

 

4、活动状态:

1、运行

        一个活动位于栈顶时,这活动处于运行状态

2、暂停

        一个活动不处于栈顶位置,但仍然可见时,这活动处于暂停状态。活动完全存活着。(比如栈顶活动为对话框形式

3、停止

        一个活动不处于栈顶位置,且完全不可见,这活动处于停止状态。(还在栈中)

4、销毁

        一个活动从栈中移除后,这活动处于销毁状态。

 

 

activity的生存期

1. onCreate() 

活动第一次被创建的时候调用,在该方法中完成初始化操作,加载布局、绑定事件等。 

2. onStart() 

由不可见变为可见的时候调用。(非栈顶到栈顶) 

3. onResume() 

在活动准备好和用户交互的时候调用。(栈顶) 

4. onPause() 

准备去启动或恢复另一个活动时调用。 

5. onStop() 

活动完全不可见时调用。 

6. onDestroy() 

在活动被销毁前调用。完成释放内存操作。 

7. onRestart() 

在活动由停止状态变为运行状态前调用。 

 

除onRestart()外,其他两两对应,活动分为三种生存期: 

1. 完整生存期 

onCreate()和onDestroy()之间所经历的,就是完整生存期。 

2. 可见生存期 

onstart()和onStop()之间所经历的,就是可见生存期。 

3. 前台生存期 

onResume()和onPause()之间所经历的,就是可见生存期。

 

 

 

 

5、活动跳转

 activity1:

传递参数:

Intent intent = new Intent(MainActivity.this , SecondActivity.class);
intent.putExtra("welcome","Hello world !");
intent.putExtra("isTrue",true);
intent.putExtra("int2",11);
//startActivity(intent);
startActivityForResult(intent,1);

 

@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		switch (requestCode) {
			case 1:
				if (resultCode == RESULT_OK) {
					String returnedData = data.getStringExtra("data_return");
					Log.d("FirstActivity", returnedData);
				}
				break;
			default:
		}
	}

 

activity2:

接收:

Intent intent = getIntent();
String data = intent.getStringExtra("welcome");

 

回传:

Intent intent = new Intent();
intent.putExtra("data_return", "Hello FirstActivity");
setResult(RESULT_OK, intent);
//在setResult后,要调用finish()销毁当前的Activity,否则无法返回到原来的Activity
finish();

 

活动跳转的另一方法(启动活动的最佳写法)

可方便知悉需要传递的参数

常规方法:

Intent intent = new Intent(FirstActivity.class,SecondActivity.class);
intent.putExtra("param1",data1);
intent.putExtra("param2",data2);
startActivity(intent);

 该方法在SecondActivity中接收参数时,若不了解SecondActivity中需要的参数,就会很不方便。

改良方法:

MainActivity中这样传递参数:

SecondActivity.actionStart(MainActivity.this,"11","22");

SecondActivity中:

public class SecondActivity extends BaseActivity {

    /*自定义方法*/

    public static void actionStart(Context context,String data1,String data2){
        Intent intent = new Intent(context,SecondActivity.class);
        intent.putExtra("param1",data1);
        intent.putExtra("param2",data2);
        context.startActivity(intent);
    }



    @Override

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second_layout);
        Intent intent = getIntent();    
    Log.d("SecondActivity","data1:"+intent.getStringExtra("param1")+",data2:"+intent.getStringExtra("param2"));

    }
}

 

 6、知晓当前是哪个活动

public class BaseActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityCollector.addActivity(this);
		
		//在BaseActivity的onCreate()方法中添加:
		Log.d("BaseActivity",getClass().getSimpleName());
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ActivityCollector.removeActivity(this);
    }
}

 然后让 BaseActivity 成为项目中所有活动的父类。

 

7、随时随地退出程序:

自定义一个活动管理器ActivityCollector

public class ActivityCollector {

    private static List<Activity> ls = new ArrayList<Activity>();

    public static void addActivity(Activity activity){
        ls.add(activity);
    }

    public static void removeActivity(Activity activity){
        ls.remove(activity);
    }

    public static void finishAll(){
        for(Activity activity : ls){
            if(!activity.isFinishing()){
                activity.finish();
            }
        }
    }
}

//BaseActivity中调用:
public class BaseActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //Log.d("BaseActivity", getClass().getSimpleName());
        ActivityCollector.addActivity(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ActivityCollector.removeActivity(this);
    }
}

 

8、全局Context

Android 提供了一个 Application 类,每当应用程序启动的时候,系统就会自动将这个类进行初始化。而我们可以定制一个自己的 Application 类,以便于管理程序内一些全局的状态

信息,比如说全局 Context。

public class MyApplication extends Application {
	
		private static Context context;
		
		@Override
		public void onCreate() {
			context = getApplicationContext();
		}
		
		public static Context getContext() {
			return context;
		}
	}

 配置完整包名:

<application
		android:name="com.example.networktest.MyApplication"
		…… >
		……
	</application>

 获取全局Context使用 MyApplication.getContext() 即可。

 

9、Intent传递复杂对象

Serializable 方式

//对象序列化:
	public class Person implements Serializable{
		private String name;
		private int age;
		
		//get/set...
	}
	
	//传递
	Person person = new Person();
	person.setName("Tom");
	person.setAge(20);
	Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
	intent.putExtra("person_data", person);
	startActivity(intent);
	
	//接收:
	Person person = (Person) getIntent().getSerializableExtra("person_data");

 Parcelable 方式 Parcelable 方式的实现原理是将一个完整的对象进行分解,而分解后的每一部分都是

Intent 所支持的数据类型,这样也就实现传递对象的功能了。

public class Person implements Parcelable {
		private String name;
		private int age;
		
		……
		@Override
		public int describeContents() {
			return 0;
		}
		
		@Override
		public void writeToParcel(Parcel dest, int flags) {
			dest.writeString(name); // 写出name
			dest.writeInt(age); // 写出age
		}
		
		public static final Parcelable.Creator<Person> CREATOR = new Parcelable.
			Creator<Person>() {
			
			@Override
			public Person createFromParcel(Parcel source) {
				Person person = new Person();
				
				//读取的顺序一定要和刚才写出的顺序完全相同
				person.name = source.readString(); // 读取name
				person.age = source.readInt(); // 读取age
				return person;
			}
			
			@Override
				public Person[] newArray(int size) {
				return new Person[size];
			}
		};
	}
	
	
	//接收:
	Person person = (Person) getIntent().getParcelableExtra("person_data");

 

。。。

 

=================================================================================

面试:

1、什么是Activity

通俗一点说Activity就是一个界面,这个界面里面可以放置各个控件。Activity的界面也是用xml表示,放置在res->layout下面。每生成一个新的Activity后,我们需要在AndroidManifest.xml中注册一下这个Activity。

 

2、请描述一下Activity的生命周期

onCreate(Bundle savedInstanceState):创建Activity时调用,设置在该方法中,还以Bundle的形式提供对以前存储的任何状态的访问。

onStart():activity变为在屏幕上对用户可见时调用

onResume():activity开始与用户交互时调用(无论是启动还是重新启动一个activity,该方法总是被调用)

onPause():activity被暂停或者收回cpu和其他资源时调用,该方法用于保存活动状态的,也是保护现场,压栈吧

onStop():activity被停止并转为不可见阶段及后续的生命周期事件时调用。

onRestart():重新启动activity时调用,该活动仍在栈中,而不是启动新的activity

onDestroy():activity被完全从系统内存中移除时调用

 

3、两个Activity之间跳转时必然会执行的是哪几个方法:

onCreate():在activity生命周期开始时调用

onRestoreInstanceState():用来恢复UI状态

onRestart():当activity重新启动时调用

onStart():当activity对用户即将可见时调用

onResume():当activity与用户交互时,绘制界面

onSaveInstanceState():当activity即将移除栈顶保留UI状态时被调用

onPause():暂停当前活动的activity,提交持久数据的改变,停止动画和其他占用CPu资源的东西,由于下一个activity在这个方法返回之前不会resume,所以这个方法的代码执行要快

onStop():activity不再可见时调用

onDestroy():在activity销毁栈时被调用的最后一个方法

 

4、横竖屏幕切换时候activity的生命周期

 

①不设置activity的android:configChanges时,切屏会重新调用各个生命周期,切横屏时会执行一次,且竖屏时会执行两次

②设置activity的android:configChanges=”orientation"时,切屏还是会调用各个生命周期,切横屏,竖屏时只会执行一次

③设置activity的android:configChanges="orientation|keyboardHidden"时,切屏不会重新调用各个生命周期,只会执行onConfigurationChanged方法

 

5、你后台的Activity被系统回收怎么办?

系统会帮我们记录下回收之前Activity的状态,再次调用被回收的Activity就要重新调用onCreate()方法,不同于直接启动的是这回onCreate()里面带上参数savedInstanceState,savedInstanceState是一个Bundle对象,基本上可以把它理解为系统帮你维护的一个map对象,使用它可以恢复到回收前的状态

 

6、如何将一个activity设置为窗口的样式

 

①在你的style.xml文件中可以新建一如下的类似Dialog的style:

<style name="Theme.FloatActivity" parent="android:style/Theme.Dialog></style>

②在AndroidManifest.xml中在你需要显示为窗口的activity中添加如下属性:

android:theme="@style/Theme.FloatActivity"即可

也可以直接添加对应需要展示为Dialog style的activity的android:theme属性值为:android:theme="@android:style/Theme.Dialog"

 

 

8、在同一个程序,但不同的activity中是否可以放在不同的Task任务栈中?

可以放在不同的Task中。需要为不同的activity设置不同的taskaffinity属性,启动activity的Intent需要包含FLAG_ACTIVITY_NEW_TASK标记

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326817625&siteId=291194637