Activity life cycle, intent-filter,

1. Page layout 2. Realize the
function of Activity
1. Define and initialize all the objects that need to be operated <2. Define the Activity class <3. Configure <4. Rewrite onCreate() and load the layout 2. Start the interface 2 <1. Create an Intent object (explicit) <2. Carry additional data through the Intent <3. Start the Activity <4. Get the Intent object <5. Read additional data <6. Display it in EditText 4. The implementation generally returns 1. When the Second interface is displayed, the MainActivity interface is actually still there, but it is covered 2. Close the current page: finish(); 1) The interface creates an object from "death"-->"run" -->onCreate()-->onStart()-->onResume()--> visible and operable (running state) 2) interface From "Run"-->"Dead" onPause()-->onStop()-->onDestory()--> The Activity object becomes a garbage object (dead state) 3) The interface changes from "Run" --> "Stop"





























onPause()-->onStop()-->Invisible but present 4)Interface from "Stop"-->"Run" onRestart-->onStart()-->onResume() 5)Interface from "Run"- -> "Pause" onPuse() 6) Interface from "Pause" --> "Run"








onResume ()



intent-filter intent filter


public class MainActivity extends Activity implements OnClickListener {
private EditText et_main_msg;
private Button btn_main_start1;
private Button btn_main_start2;


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


//初始化视图对象
et_main_msg = (EditText) findViewById(R.id.et_main_msg);
btn_main_start1 = (Button) findViewById(R.id.btn_main_start1);
btn_main_start2 = (Button) findViewById(R.id.btn_main_start2);
//设置点击监听
btn_main_start1.setOnClickListener(this);
btn_main_start2.setOnClickListener(this);
}


@Override
public void onClick(View v) {//v is the (user-operated) view object where this event occurred
// switch (v.getId()) {
// case R.id.btn_main_start1:
// Toast.makeText(this , "normal start", Toast.LENGTH_SHORT).show();
// break;
// case R.id.btn_main_start2:
// Toast.makeText(this, "start with callback", Toast.LENGTH_SHORT).show() ;
// break;
// }
if (v == btn_main_start1) {
// Toast.makeText(this, "normal start", Toast.LENGTH_SHORT).show();
// Create an Intent object (show)
Intent intent = new Intent(this,SecondActivity.class);
// carry extra data through Intent
String message = et_main_msg.getText().toString().trim();
intent.putExtra("MSG", message);
// Start Activity
startActivity(intent);
}else if(v == btn_main_start2){
// Toast.makeText(this, "start with callback", Toast.LENGTH_SHORT).show ();
// Create an Intent object (display)
Intent intent = new Intent(this,SecondActivity.class);
// Carry extra data through Intent
String message = et_main_msg.getText().toString().trim();
intent. putExtra("MSG", message);
// Start Activity with callback
int requestCode = 2;
startActivityForResult(intent, requestCode);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//Judgment Code
if (requestCode == 2 && resultCode == 3) {
//Get data from data
String result = data.getStringExtra("RESULT");
//Prompt
et_main_msg.setText(result);
}
}

}



public class SecondActivity extends Activity{
private EditText et_second_msg;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.secondactivity);


et_second_msg = (EditText) findViewById(R.id.et_second_msg);
// Get the Intent object
Intent intent = getIntent();
// Read extra data
String message = intent.getStringExtra("MSG");
// Display to EditText
et_second_msg.setText(message);
}
public void back1(View v ) {
//Close the current page
finish();
}
public void back2(View v){
/*Save a result*/
int resultCode = 3;
// Prepare an Intent object with extra data
Intent data = new Intent();
String result = et_second_msg.getText().toString().trim();
data.putExtra("RESULT", result);
//Set the result
setResult( resultCode,data);
//Close the current interface
finish();
}
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325895870&siteId=291194637