gpt4 book ai didi

Android Activity 总是调用 onRestart 而不是停留在 Stop

转载 作者:搜寻专家 更新时间:2023-11-01 08:52:49 25 4
gpt4 key购买 nike

我有两个示例 Activity ,一个名为 activityone,另一个为 activitytwo

activitytwo 只有一个按钮可以再次调用 activityone ,但是当显示 activityone 时,由于某种原因 activitytwo 总是停留在 onrestart() 而不是 stop

这是 Activity 2 的代码( Activity 一具有相同的代码)

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

此时 Activity 1 再次显示,但 Activity 二 onRestart 而不是仅仅保持在 Stop() 因为不再可见?

最佳答案

public void onDestroy() { super.onDestroy();

// TODO: Emit LogCat message
Log.i(TAG, "Entered the onRestart() method");

你需要将它记录为onDestroy

关于Android Activity 总是调用 onRestart 而不是停留在 Stop,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21572549/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com