我有两个例子活动,一个叫做activityone
,另一个叫做activitytwo
。activitytwo
只有一个按钮可以再次调用activityone
,但是当显示activityone
时,由于某种原因,activitytwo
始终保持onrestart()
而不是停止
这是活动2的代码(活动1具有相同的代码)
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,但活动2将重新启动,而不是仅保持在stop()上,因为不再可见?
最佳答案
public void ondestroy(){
超级OnDestroy();
// TODO: Emit LogCat message
Log.i(TAG, "Entered the onRestart() method");
你需要把它记录为Ondestroy
关于android - Android Activity始终调用onRestart而不是停留在Stop上,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21572549/