For now, I only know that the activity cannot be reborn in the following cases.
1. Call the functions of the finish () series.
In fact, the system clearly knows that the activity does not need to be reborn. Therefore, the onsaveinstancestate (bundle B) is not called to save the information. Of course, it is impossible to regenerate the information.
Note 1: In the above cases, onsaveinstancestate (bundle B) will not be called)
NOTE 2: By default, when you press the back key for an activity, the system calls the onbackpressed () function of the acitielist, and calls the finish () function in the function. The programmer can rewrite this function to modify the behavior of the activity after the back key is pressed.
Activity can be reborn only in the following two cases.
1. Because of memory, the activity can be reborn when it is killed.
2. in the default configuration changes action configuration, after configuration changes, the activity will also be reborn. It experiences onpause (), onstop (), and ondestroy (),Then, we can use the things saved in onsaveinstancestate (bundle) to realize rebirth.
The system also calls ondestroy () when the activity is killed due to memory usage (). However, in this case, the system has called onsaveinstancestate (bundle B) to save the information.
Therefore, this activity can read the information saved by onsaveinstancestate (bundle B) in oncreate () or onrestoreinstancestate () to realize rebirth.Note 1: ondestroy () is called whenever the activity dies ().
I used to think that the idea of "saving information of onsaveinstancestate (bundle B) after ondestroy () is incorrect.
NOTE 2: although the description of ondestroy () in the logical inference and Google reference documents is described, the system also calls ondestroy () when the activity is killed due to memory reasons ().
However, the life cycle diagram of the Google document activity does not match this. Of course, I may not understand the graph.
In the Google document activity life cycle diagram, when the activity is killed due to memory reasons, the onstop box is left and down. Google document Activity lifecycle diagram:
I tried to write a program for testing, but failed. Because this program does not consume a lot of Android memory. A process occupies a certain amount of memory. The common application is 3 M. This test program is not scientific. So you can ignore it completely.
The test procedure is as follows:
File 1
Eatmemoryactivity. Java
Package com. gameloft;
Import Android. App. activity;
Import Android. OS. Bundle;
Import Android. util. log;
Import Android. View. view;
Import Android. View. View. onclicklistener;
Import Android. widget. Button;
Public class eatmemoryactivity extends activity {
String tag = "hubin2 ";
Final Static int KMB = 1024*1024;
Final Static int KKB = 1024;
/** Called when the activity is first created .*/
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );
Button button = (button) findviewbyid (R. Id. button01 );
Onclicklistener listener = new onclicklistener (){
Public void onclick (view v ){
Eatmemory (1024*1024 );
}
};
Button. setonclicklistener (listener );
Button button2 = (button) findviewbyid (R. Id. button02 );
Onclicklistener listener2 = new onclicklistener (){
Public void onclick (view v ){
For (INT I = 0; I <50; I ++)
Eatmemory (KMB/100 );
}
};
Button2.setonclicklistener (listener2 );
Button button3 = (button) findviewbyid (R. Id. button03 );
Onclicklistener listener3 = new onclicklistener (){
Public void onclick (view v ){
For (INT I = 0; I <100; I ++)
Eatmemory (KMB/1000 );
}
};
Button3.setonclicklistener (listener3 );
Button button4 = (button) findviewbyid (R. Id. button04 );
Onclicklistener listener4 = new onclicklistener (){
Public void onclick (view v ){
For (INT I = 0; I <50; I ++)
Eatmemory (KMB/1000 );
}
};
Button4.setonclicklistener (listener4 );
Button button5 = (button) findviewbyid (R. Id. button05 );
Onclicklistener listener5 = new onclicklistener (){
Public void onclick (view v ){
For (INT I = 0; I <100; I ++)
Eatmemory (KKB/10 );
}
};
Button5.setonclicklistener (listener5 );
}
Byte tempbuffer [] [] = new byte [1, 10000] [];
Void eatmemory (INT size)
{
Int I = 0;
For (I = 0; I <tempbuffer. length; I ++)
{
If (tempbuffer= NULL)
{
Try {
Tempbuffer= New byte [size];
} Catch (exception E)
{
Log. E (TAG, "erro ");
}
Break;
}
}
Log. I (TAG, "consume memory" + (I + 1) + "MB ");
System. GC ();
}
}
File 2
Main. xml file
<? 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: text = "@ + String/eat1m" Android: Id = "@ + ID/button01" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content"> </button>
<Button Android: text = "@ + String/eat500k" Android: Id = "@ + ID/button02" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content"> </button>
<Button Android: text = "@ + String/eat100k" Android: Id = "@ + ID/button03" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content"> </button>
<Button Android: text = "@ + String/eat50k" Android: Id = "@ + ID/button04" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content"> </button>
<Button Android: text = "@ + String/eat10k" Android: Id = "@ + ID/button05" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content"> </button>
</Linearlayout>
File 3
Strings. xml
<? XML version = "1.0" encoding = "UTF-8"?>
<Resources>
<String name = "hello"> Hello world, eatmemoryactivity! </String>
<String name = "app_name"> eatmemory </string>
<String name = "eat1m"> eat 1 MB meory </string>
<String name = "eat1k"> eat 1 kb meomry </string>
<String name = "eat100k"> eat 100 kb memory </string>
<String name = "eat500k"> eat 500 kb </string>
<String name = "eat10k"> eat 10 KB </string>
<String name = "eat50k"> eat 50 kb </string>
</Resources>
Note:This method does not kill invisible processes (applications)