我有兩個例子活動的一個叫activityone和其他activitytwoAndroid的活動隨時撥打onRestart,而不是停留在停止
的activitytwo只是有一個按鈕,再次調用活動之一,但在顯示activityone,爲某種原因activitytwo總是保持onrestart()而不是停止
這是活動2的代碼(活性的一種具有相同的代碼)
package course.labs.activitylab;
import android.app.Activity;
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;
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 Integer mCreate = 0;
private Integer mRestart = 0;
private Integer mStart = 0;
private Integer mResume = 0;
// 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) findViewById(R.id.create);
mTvRestart = (TextView) findViewById(R.id.restart);
mTvStart = (TextView) findViewById(R.id.start);
mTvResume = (TextView) 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();
Intent tmpIntent = new Intent(getApplicationContext(), ActivityOne.class);
// Launch the Activity using the intent
startActivity(tmpIntent);
}
});
// 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
}
// TODO: Emit LogCat message
Log.i(TAG, "Entered the onCreate() method");
// TODO:
// Update the appropriate count variable
// Update the user interface via the displayCounts() method
mCreate++;
displayCounts();
}
// Lifecycle callback methods overrides
@Override
public void onStart() {
super.onStart();
// TODO: Emit LogCat message
Log.i(TAG, "Entered the onStart() method");
// TODO:
// Update the appropriate count variable
// Update the user interface
mStart++;
displayCounts();
}
@Override
public void onResume() {
super.onResume();
// TODO: Emit LogCat message
Log.i(TAG, "Entered the onResume() method");
// TODO:
// Update the appropriate count variable
// Update the user interface
mResume++;
displayCounts();
}
@Override
public void onPause() {
super.onPause();
// TODO: Emit LogCat message
Log.i(TAG, "Entered the onPause() method");
}
@Override
public void onStop() {
super.onStop();
// TODO: Emit LogCat message
Log.i(TAG, "Entered the onStop() method");
}
@Override
public void onRestart() {
super.onRestart();
// TODO: Emit LogCat message
Log.i(TAG, "Entered the onRestart() method");
// TODO:
// Update the appropriate count variable
// Update the user interface
mRestart++;
displayCounts();
}
@Override
public void onDestroy() {
super.onDestroy();
// TODO: Emit LogCat message
Log.i(TAG, "Entered the onRestart() 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
}
// 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);
}
}
這是日誌
//Enter to the app for first time
02-05 03:36:48.783: I/Lab-ActivityOne(1591): Entered the onCreate() method
02-05 03:36:48.783: I/Lab-ActivityOne(1591): Entered the onStart() method
02-05 03:36:48.783: I/Lab-ActivityOne(1591): Entered the onResume() method
//Click the start activity 2 button
02-05 03:36:58.413: I/Lab-ActivityOne(1591): Entered the onPause() method
02-05 03:36:58.713: I/Lab-ActivityTwo(1591): Entered the onCreate() method
02-05 03:36:58.733: I/Lab-ActivityTwo(1591): Entered the onStart() method
02-05 03:36:58.733: I/Lab-ActivityTwo(1591): Entered the onResume() method
02-05 03:36:59.093: I/Choreographer(1591): Skipped 46 frames! The application may be doing too much work on its main thread.
02-05 03:36:59.933: I/Lab-ActivityOne(1591): Entered the onStop() method
02-05 03:36:59.953: I/Lab-ActivityOne(1591): Entered the onDestroy() method
//Click Close Activity 2
02-05 03:37:06.923: I/Lab-ActivityTwo(1591): Entered the onPause() method
02-05 03:37:07.203: I/Lab-ActivityOne(1591): Entered the onCreate() method
02-05 03:37:07.203: I/Lab-ActivityOne(1591): Entered the onStart() method
02-05 03:37:07.223: I/Lab-ActivityOne(1591): Entered the onResume() method
02-05 03:37:07.623: I/Choreographer(1591): Skipped 68 frames! The application may be doing too much work on its main thread.
02-05 03:37:08.513: I/Lab-ActivityTwo(1591): Entered the onStop() method
02-05 03:37:08.523: I/Lab-ActivityTwo(1591): Entered the onRestart() method
此時活動1再次顯示,但onRestart的兩個,而不是隻是繼續停止活動(),因爲是不可見的了嗎?