我創建了兩個活動。我爲我的第一個活動完成了相同的代碼。但這工作正常。但是當我切換到第二個活動時,計數器值不會增加,但logcat顯示所有狀態都是正確的。並且在logcat中顯示「打開跟蹤文件時出錯」。計算活動生命週期狀態時出現錯誤結果
中的onCreate電話,來電的onResume和的onCreate始終顯示值1和onRestart始終顯示爲0何在它可以正確處理activity_one
package course.labs.activitylab;
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;
import android.widget.TextView;
public class ActivityTwo extends Activity {
private static final String RESTART_KEY = "restart";
private static final String RESUME_KEY = "resume";
private static final String START_KEY = "start";
private static final String CREATE_KEY = "create";
// String for LogCat documentation
private final static String TAG = "Lab-ActivityTwo";
// Lifecycle counters
// TODO:
// Create counter variables for onCreate(), onRestart(), onStart() and
// onResume(), called mCreate, etc.
// You will need to increment these variables' values when their
// corresponding lifecycle methods get called
private int mCreate ;
private int mRestart ;
private int mStart ;
private int mResume ;
// TODO: Create variables for each of the TextViews, called
// mTvCreate, etc.
private TextView mTvCreate ;
private TextView mTvRestart;
private TextView mTvStart;
private TextView mTvResume;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_two);
// TODO: Assign the appropriate TextViews to the TextView variables
// Hint: Access the TextView by calling Activity's findViewById()
// textView1 = (TextView) findViewById(R.id.textView1);
mTvCreate = (TextView) this.findViewById(R.id.create);
mTvRestart = (TextView) this.findViewById(R.id.restart);
mTvStart = (TextView) this.findViewById(R.id.start);
mTvResume = (TextView) this.findViewById(R.id.resume);
Button closeButton = (Button) findViewById(R.id.bClose);
closeButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO:
// This function closes Activity Two
// Hint: use Context's finish() method
finish();
}
});
// Check for previously saved state
if (savedInstanceState != null) {
// TODO:
// Restore value of counters from saved state
// Only need 4 lines of code, one for every count variable
super.onRestoreInstanceState(savedInstanceState);
mCreate = savedInstanceState.getInt(CREATE_KEY);
mRestart = savedInstanceState.getInt(RESTART_KEY);
mStart = savedInstanceState.getInt(START_KEY);
mResume = savedInstanceState.getInt(RESUME_KEY);
}
// TODO: Emit LogCat message
Log.i(TAG, "I am in onCreate method");
// TODO:
// Update the appropriate count variable
// Update the user interface via the displayCounts() method
mCreate += 1;
displayCounts();
}
// Lifecycle callback methods overrides
@Override
public void onStart() {
super.onStart();
// TODO: Emit LogCat message
Log.i(TAG, "I am in onStart method");
// TODO:
// Update the appropriate count variable
// Update the user interface
mStart += 1;
displayCounts();
}
@Override
public void onResume() {
super.onResume();
// TODO: Emit LogCat message
Log.i(TAG, "I am in onStart method");
// TODO:
// Update the appropriate count variable
// Update the user interface
mResume += 1;
displayCounts();
}
@Override
public void onPause() {
super.onPause();
// TODO: Emit LogCat message
Log.i(TAG, "I am in onPause method");
}
@Override
public void onStop() {
super.onStop();
// TODO: Emit LogCat message
Log.i(TAG, "I am in onStop method");
}
@Override
public void onRestart() {
super.onRestart();
// TODO: Emit LogCat message
Log.i(TAG, "I am in onRestart method");
// TODO:
// Update the appropriate count variable
// Update the user interface
mRestart += 1;
displayCounts();
}
@Override
public void onDestroy() {
super.onDestroy();
// TODO: Emit LogCat message
Log.i(TAG, "I am in onDestroy method");
}
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
// TODO:
// Save counter state information with a collection of key-value pairs
// 4 lines of code, one for every count variable
super.onSaveInstanceState(savedInstanceState);
savedInstanceState.putInt(CREATE_KEY, mCreate);
savedInstanceState.putInt(START_KEY, mStart);
savedInstanceState.putInt(RESUME_KEY, mResume);
savedInstanceState.putInt(RESTART_KEY, mRestart);
//savedInstanceState.getInt(CREATE_KEY);
//savedInstanceState.getInt(RESUME_KEY);
//savedInstanceState.getInt(RESTART_KEY);
//savedInstanceState.getInt(START_KEY);
}
// Updates the displayed counters
public void displayCounts() {
mTvCreate.setText("onCreate() calls: " + mCreate);
mTvStart.setText("onStart() calls: " + mStart);
mTvResume.setText("onResume() calls: " + mResume);
mTvRestart.setText("onRestart() calls: " + mRestart);
}
}
的logcat的顯示正確結果:
03-06 23:26:38.179: I/Lab-ActivityTwo(30424): I am in onPause method
03-06 23:26:38.189: I/Lab-ActivityOne(30424): I am in onRestart method
03-06 23:26:38.189: I/Lab-ActivityOne(30424): I am in onStart method
03-06 23:26:38.199: I/Lab-ActivityOne(30424): I am in onResume method
03-06 23:26:38.430: I/Lab-ActivityTwo(30424): I am in onStop method
03-06 23:26:38.430: I/Lab-ActivityTwo(30424): I am in onDestroy method
03-06 23:26:38.770: I/Lab-ActivityOne(30424): I am in onPause method
03-06 23:26:38.900: I/Lab-ActivityOne(30424): I am in onStop method
03-06 23:26:38.900: I/Lab-ActivityOne(30424): I am in onDestroy method
03-06 23:26:46.688: I/Lab-ActivityOne(30424): Oncreate1
03-06 23:26:46.688: I/Lab-ActivityOne(30424): I am in onStart method
03-06 23:26:46.688: I/Lab-ActivityOne(30424): I am in onResume method
03-06 23:26:52.233: I/Lab-ActivityOne(30424): I am in onPause method
03-06 23:26:52.283: I/Lab-ActivityTwo(30424): I am in onCreate method
03-06 23:26:52.283: I/Lab-ActivityTwo(30424): I am in onStart method
03-06 23:26:52.283: I/Lab-ActivityTwo(30424): I am in onStart method
03-06 23:26:52.593: I/Lab-ActivityOne(30424): I am in onStop method
03-06 23:27:04.115: I/Lab-ActivityTwo(30424): I am in onPause method
03-06 23:27:04.155: I/Lab-ActivityOne(30424): I am in onRestart method
03-06 23:27:04.155: I/Lab-ActivityOne(30424): I am in onStart method
03-06 23:27:04.155: I/Lab-ActivityOne(30424): I am in onResume method
03-06 23:27:04.385: I/Lab-ActivityTwo(30424): I am in onStop method
03-06 23:27:04.395: I/Lab-ActivityTwo(30424): I am in onDestroy method
03-06 23:27:06.277: I/Lab-ActivityOne(30424): I am in onPause method
03-06 23:27:06.337: I/Lab-ActivityTwo(30424): I am in onCreate method
03-06 23:27:06.337: I/Lab-ActivityTwo(30424): I am in onStart method
03-06 23:27:06.337: I/Lab-ActivityTwo(30424): I am in onStart method
03-06 23:27:06.617: I/Lab-ActivityOne(30424): I am in onStop method
03-06 23:27:14.825: I/Lab-ActivityTwo(30424): I am in onPause method
03-06 23:27:14.855: I/Lab-ActivityOne(30424): I am in onRestart method
03-06 23:27:14.855: I/Lab-ActivityOne(30424): I am in onStart method
03-06 23:27:14.855: I/Lab-ActivityOne(30424): I am in onResume method
03-06 23:27:15.095: I/Lab-ActivityTwo(30424): I am in onStop method
03-06 23:27:15.095: I/Lab-ActivityTwo(30424): I am in onDestroy method
你能告訴我們你期望的輸出和你實際得到的輸出嗎? – Merlevede
它總是顯示onCreate類,onStart調用和onResume調用1和onRestart調用0的值,但是當我切換回activity_one並返回到activity_second時,值應該增加。 – Anurag
我沒有看到任何代碼來打開你的第二個活動。你如何開始第二次活動? – Merlevede