If you think the Android project 2014-Droid-code listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
Java Source Code
package cs518.sample.activityLifecycle;
//www.java2s.comimport android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
/*
* In Activity2 we use the instanceStateBundle to maintain the counter information
* In MyActivityLifeCycleActivity.java we do not save any state.
* note the run time differences, when/is the counter reset
*
*/publicclass MyActivityLifeCycleActivity extends Activity {
protectedint counter = 0;
publicstaticfinal String TAG = "LIFECYC";
/** Called when the activity is first created. */
@Override
protectedvoid onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG, "onCreate() ");
setContentView(R.layout.main);
Button killButton = (Button) findViewById(R.id.killButton);
killButton.setOnClickListener(new OnClickListener() {
publicvoid onClick(View v) {
Log.d(TAG, "finish()");
finish();
}
});
Button countButton = (Button) findViewById(R.id.countButton);
countButton.setOnClickListener(new OnClickListener() {
publicvoid onClick(View v) {
Log.d(TAG, "counting");
counter++;
updateCounter();
}
});
Button activityButton = (Button) findViewById(R.id.activityButton);
activityButton.setOnClickListener(new OnClickListener() {
publicvoid onClick(View v) {
Log.d(TAG, "fire intent");
Intent launchOtherScreen = new Intent(getApplicationContext(),
Activity2.class);
startActivity(launchOtherScreen);
}
});
} //onCreate()
@Override
protectedvoid onStart() {
super.onStart();
Log.d(TAG, "onStart()");
}
@Override
protectedvoid onResume() {
super.onResume();
Log.d(TAG, "onResume()");
}
@Override
protectedvoid onPause() {
super.onPause();
Log.d(TAG, "onPause()");
}
@Override
protectedvoid onStop() {
super.onStop();
Log.d(TAG, "onStop()");
}
@Override
protectedvoid onDestroy() {
super.onDestroy();
Log.d(TAG, "onDestroy()");
}
@Override
protectedvoid onRestart() {
super.onRestart();
Log.d(TAG, "onRestart()");
}
privatevoid updateCounter() {
TextView textView = (TextView) findViewById(R.id.counterValue);
textView.setText("Actual counter: " + counter);
}
} //MyActivityLifecycle