Code Java
package net.chezdom.cours.activitylifecycle;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import java.text.DateFormat;
import java.util.Date;
public class MainActivity extends Activity implements View.OnClickListener {
private LinearLayout logbox;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
logbox=findViewById(R.id.logbox);
Button b=findViewById(R.id.btest);
b.setOnClickListener(this);
b=findViewById(R.id.bquit);
b.setOnClickListener(this);
findViewById(R.id.binfo).setOnClickListener(this);;
this.displayLog("onCreate");
}
private void displayLog(String txt) {
Toast.makeText(getApplicationContext(),"from: "+txt, Toast.LENGTH_SHORT).show();
String currentDateTimeString = DateFormat.getDateTimeInstance().format(new Date());
TextView textView = new TextView(this);
textView.setText(currentDateTimeString+": "+txt);
logbox.addView(textView);
}
@Override
public void onClick(View view) {
if (view.getId()==R.id.btest)
displayLog("Click on test button!");
else if (view.getId()==R.id.bquit) {
displayLog("Click on Quit button!");
this.finishAffinity();
}
else if (view.getId()==R.id.binfo) {
displayLog("Click on Info button!");
Intent intent=new Intent(this,LifeCycleInfoActivity.class);
startActivity(intent);
}
}
@Override
protected void onStart() {
super.onStart();
this.displayLog("onStart");
}
@Override
protected void onRestart() {
super.onRestart();
this.displayLog("onRestart");
}
@Override
protected void onResume() {
super.onResume();
this.displayLog("onResume");
}
@Override
protected void onPause() {
super.onPause();
this.displayLog("onPause");
}
@Override
protected void onStop() {
super.onStop();
this.displayLog("onStop");
}
@Override
protected void onDestroy() {
super.onDestroy();
this.displayLog("onDestroy");
}
}
Copie d’écran