Life cycle of 1.Activity
1 The activity stack is composed of multiple activities, and the current event is at the top of the stack. Let's take a look at the class diagram of the various activity base classes:
When the activity class is defined, when the activity is instantiated, and when the method it contains is invoked, is not determined by the developer, it should be determined by the Android system.
Let's take a look at the life cycle of the activity:
The use of 2.Activity
1) Start and close activity
First you need to create the corresponding Intent
Intent Intent = new Intent (Mainactivity.this, Twoactivity.class) of the initiating activity;
Start Activity
startactivity (Intent Intent);
Startactivityforresult (Intent Intent, int requestcode); Requestcode: The request code
//startactivityforresult method starts the activity with the specified request code and gets the result returned by overriding the Onactivityresult method.
//Close activity
finish ();
finishactivity (int requestcode);
The Finishactivity method ends the activity initiated by the Startactivityforresult method.
2 Start other activity and return results
Current Activity overrides Onactivityresult (int requestcode, int resultcode, Intent Intent)
Requestcode: The request code (indicates which of the requested results the method was fired from)
The result code returned by the Resultcode:activity (indicating which new activity the returned data originated from)
The activated activity needs to invoke the Setresult () method to set the processing result.
Instance:
Overriding the Onactivityresult method in the current activity
public class Mainactivity extends activity {
Button bn;
@Override public
void OnCreate (Bundle savedinstancestate) {
super.oncreate (savedinstancestate);
Setcontentview (r.layout.main);
Get the Components
on the interface ... Bind Event Listener
Bn.setonclicklistener (new Onclicklistener () {
@Override public
void OnClick (view view) {
Intent Intent = new Intent (mainactivity.this, twoactivity.class);
Startactivityforresult (Intent, 0); 0 is the request code to identify the request
}}
;
@Override public
void Onactivityresult (int requestcode, int resultcode, Intent Intent) {
//process specific results
if ( Requestcode = = 0 && resultcode = 0) {
//Remove Extras data from Intent
Bundle ();
Remove data in bundle
String result = data.getstring ("test");
Toast.maketext (Getapplicationcontext (), result, 0). Show ();}}
Then invoke the Setresult () method to set the processing result in the twoactivity that is started
Gets the Intent
Intent Intent = getintent ()
of the activity corresponding to the activity before the activity was started. Intent.putextra ("Test", "Test");
Set the result code of the selectactivity and set the activity
SelectCityActivity.this.setResult (0, intent) returned after the end
; End twoactivity
TwoActivity.this.finish ();
3. Exchange of data between the activity using bundle
Intent provides a number of overloaded methods to "carry" additional data, as follows:
Intent.putextras (Bundle data); Put the packet
Intent.putextras (String name, Xxx value) into the intent, and put the data in the form of Key-value to the intent
Intent.getextras () ; Remove the packet Intent.getxxxextras (String name) that is carried in the intent
;//Remove the specified type of data from the intent by key
In the above method bundle is a simple data carrying package, intent mainly through bundle objects to carry data, bundle contains several methods to access data:
Bundle Bundle = new Bundle (); First create a Bundle object
bundle.putxxx (string key, XXX data)//////////////Add data bundle.putserializable to bundle
(string key, Serializable data); Put a serializable object into the bundle (that is, implement the Java.io.Serializable interface)
bundle.getxxx (String key);//Fetch data
from bundle Bundle.getserializable (String key); To remove a serializable object from the bundle
Four load modes for 4.Activity
When you configure an activity in Androidmanifest.xml, you can specify that the Android:launchmode property is used to configure the load mode for the activity, which supports 4 property values:
Standard: Standard mode;
Singletop: Stack top single case mode;
Singletask: Single case mode in the stack (if the target activity already exists but not at the top of the stack, the system moves all activity above the activity out of the task stack so that the target activity is transferred to the top of the stack);
SingleInstance: Global single mode (newly created activity will be put into the new stack, a stack contains only one activity, and if the target activity already exists, the system will move the task to the foreground display).
Life cycle of 5.Fragment
Fragment is the new api,fragment represented by AndroiD3.0 (activity fragment), fragment must be embedded in the activity, and the fragment life cycle is controlled by the lifecycle of the activity in which it resides.
Fragment can call the Getactivity () method to obtain its activity;
The activity can call the Fragmentmanager Findfragmentbyid () or Findfragmentbytag () method to obtain fragment;
During the activity run, the Add (), remove (), replace () method of the Fragmentmanager can be invoked to dynamically add, remove, and replace fragment.
1) Let's take a look at the class diagram of the various fragment base classes:
2) Let's take a look at the life cycle of the fragment and compare it to the life cycle of the activity:
The use of 6.Fragment
1) Create fragment
Creating fragment typically implements the following three methods:
OnCreate (), Oncreateview (), OnPause ()
In order to control the components displayed by fragment, it is often necessary to override the Oncreateview () method, which returns the view as the view component that the fragment displays.
Overriding the method, the View returned by the method will be used as the component displayed by fragment
@Override public
View Oncreateview (Layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) {
//load/res/layout/directory fragment.xml layout file
View view = Inflater.inflate ( R.layout.fragment, container, false);
TextView name = (TextView) View.findviewbyid (r.id.name));
...
return view;
}
2 Add fragment to the activity
There are two ways to add fragment to an activity:
1th: Specify the fragment implementation class in the layout file by using the Android:name attribute of the <fragment.../> element to add the fragment,<fragment.../> element.
2nd: Add fragment through the Add () method of the Fragmenttransaction object in Java code.
The Getfragmentmanager () method of the activity can return the BeginTransaction () of the Fragmentmanager,fragmentmanager object method to open and return the Fragmenttransaction object.
3 How do you dynamically add, update, and delete fragment in an activity?
First you need to add framelayout (set ID to FL) in the mainactivity layout file, and then simply create a two fragment (Myfragment and Twofragment) as follows:
public class Myfragment extends Fragment {
@Override public
View Oncreateview (Layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) {return
inflater.inflate (R.layout.fragment_my, container, false) ;
}
}
public class Twofragment extends Fragment {
@Override public
View Oncreateview (Layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) {return
inflater.inflate (R.layout.fragment_two, container, false);
}
The next step is to dynamically add, update, and delete fragment in Mainactivity, and the methods called in Mainactivity are as follows:
Set the default fragment
Fragmentmanager FM = Getfragmentmanager ();
Fragmenttransaction transaction = Fm.begintransaction ();
Myfragment = new Myfragment ();
Transaction.replace (R.ID.FL, myfragment);
Transaction.commit ();
If you click on a button to update fragment, the button click event is as follows:
@Override public
void OnClick (View v) {
fragmentmanager fm = Getfragmentmanager ();
Open fragment transaction
fragmenttransaction transaction = Fm.begintransaction ();
Twofragment = new Twofragment ();
Replaces the FL control transaction.replace with the current fragment layout
(R.ID.FL, twofragment);
Transaction.addtobackstack (); Add things to the back stack, allowing the user to return the key to the status
//Transaction Submission
Transaction.commit () before replacing fragment;
The above is on the Android activity and fragment data collation, follow-up continue to supplement the relevant information, thank you for your support of this site!