我有两个示例活动,一个名为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 onRestart而不是仅保持Stop()因为不再可见?
答案 0 :(得分:1)
public void onDestroy(){ super.onDestroy();
// TODO: Emit LogCat message
Log.i(TAG, "Entered the onRestart() method");
您需要将其记录为onDestroy
答案 1 :(得分:1)
如果要在单击关闭按钮时停止ActivityTwo
,请使用完成()。
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
ActivityTwo.this.finish();
}
});
答案 2 :(得分:1)
onDestroy()方法的log.i中的错字。应该是“输入 onDestroy()方法”
@Override
public void onDestroy() {
super.onDestroy();
// TODO: Emit LogCat message
Log.i(TAG, "Entered the onDestroy() method");
}
Coursera是最好的!