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

标签 android android-activity android-lifecycle

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

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

这是 Activity 2 的代码( Activity 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 

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

最佳答案

公共(public)无效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/

相关文章:

android - 如何将 android 应用程序流量转换为代理和解密 TLS 流量?

android - 按下主页按钮时如何清除 Activity 历史堆栈?

android - 如何在android中调整Tabhost的高度

android - 我如何通过蓝牙打印机打印线性布局上的表单数据集

java - 每次应用程序来自 android 中的后台时启动第一个屏幕

android - 如何检查是否正在从哪个音乐应用程序播放音乐?

java - 澄清 Android Activity onPause 和 onRestart

android - 背景简历上的密码

Android 启动应用程序等待异步代码执行完成后再打开 Activity

java - 使用 c2dm 时出现 SERVICE_NOT_AVAILABLE 错误