计算活动生命周期状态时的错误结果

时间:2014-03-06 17:01:13

标签: android android-lifecycle

我创建了两个活动。我为我的第一个活动做了相同的代码。但那工作正常。但是当我切换到第二个活动时,计数器值没有增加,但是logcat正在显示所有状态。并且logcat中存在一个问题,显示“错误打开跟踪文件”。

onCreate调用,onResume调用和onCreate始终显示值1,onRestart始终显示0.这对于activity_one是否正常

package course.labs.activitylab;

import android.app.Activity;
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 int mCreate ;
    private int mRestart ;
    private int mStart ;
    private int mResume ;



    // 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) this.findViewById(R.id.create);
        mTvRestart = (TextView) this.findViewById(R.id.restart);
        mTvStart = (TextView) this.findViewById(R.id.start);
        mTvResume = (TextView) this.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();


            }
        });

        // 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
            super.onRestoreInstanceState(savedInstanceState);
            mCreate = savedInstanceState.getInt(CREATE_KEY);
            mRestart = savedInstanceState.getInt(RESTART_KEY);
            mStart = savedInstanceState.getInt(START_KEY);
            mResume = savedInstanceState.getInt(RESUME_KEY);


        }

        // TODO: Emit LogCat message
        Log.i(TAG, "I am in onCreate method");


        // TODO:
        // Update the appropriate count variable
        // Update the user interface via the displayCounts() method

        mCreate += 1;
        displayCounts();



    }

    // Lifecycle callback methods overrides

    @Override
    public void onStart() {
        super.onStart();

        // TODO: Emit LogCat message
        Log.i(TAG, "I am in onStart method");

        // TODO:
        // Update the appropriate count variable
        // Update the user interface

        mStart += 1;
        displayCounts();


    }

    @Override
    public void onResume() {
        super.onResume();

        // TODO: Emit LogCat message
        Log.i(TAG, "I am in onStart method");

        // TODO:
        // Update the appropriate count variable
        // Update the user interface

        mResume += 1;
        displayCounts();


    }

    @Override
    public void onPause() {
        super.onPause();

        // TODO: Emit LogCat message
        Log.i(TAG, "I am in onPause method");


    }

    @Override
    public void onStop() {
        super.onStop();

        // TODO: Emit LogCat message
        Log.i(TAG, "I am in onStop method");


    }

    @Override
    public void onRestart() {
        super.onRestart();

        // TODO: Emit LogCat message
        Log.i(TAG, "I am in onRestart method");

        // TODO:
        // Update the appropriate count variable
        // Update the user interface


        mRestart += 1;
        displayCounts();


    }

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

        // TODO: Emit LogCat message
        Log.i(TAG, "I am in onDestroy 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

        super.onSaveInstanceState(savedInstanceState);
        savedInstanceState.putInt(CREATE_KEY, mCreate);
        savedInstanceState.putInt(START_KEY, mStart);
        savedInstanceState.putInt(RESUME_KEY, mResume);
        savedInstanceState.putInt(RESTART_KEY, mRestart);

        //savedInstanceState.getInt(CREATE_KEY);
        //savedInstanceState.getInt(RESUME_KEY);
        //savedInstanceState.getInt(RESTART_KEY);
        //savedInstanceState.getInt(START_KEY);




    }

    // 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);

    }
}

logcat显示正确的结果:

03-06 23:26:38.179: I/Lab-ActivityTwo(30424): I am in onPause method
03-06 23:26:38.189: I/Lab-ActivityOne(30424): I am in onRestart method
03-06 23:26:38.189: I/Lab-ActivityOne(30424): I am in onStart method
03-06 23:26:38.199: I/Lab-ActivityOne(30424): I am in onResume method
03-06 23:26:38.430: I/Lab-ActivityTwo(30424): I am in onStop method
03-06 23:26:38.430: I/Lab-ActivityTwo(30424): I am in onDestroy method
03-06 23:26:38.770: I/Lab-ActivityOne(30424): I am in onPause method
03-06 23:26:38.900: I/Lab-ActivityOne(30424): I am in onStop method
03-06 23:26:38.900: I/Lab-ActivityOne(30424): I am in onDestroy method
03-06 23:26:46.688: I/Lab-ActivityOne(30424): Oncreate1
03-06 23:26:46.688: I/Lab-ActivityOne(30424): I am in onStart method
03-06 23:26:46.688: I/Lab-ActivityOne(30424): I am in onResume method
03-06 23:26:52.233: I/Lab-ActivityOne(30424): I am in onPause method
03-06 23:26:52.283: I/Lab-ActivityTwo(30424): I am in onCreate method
03-06 23:26:52.283: I/Lab-ActivityTwo(30424): I am in onStart method
03-06 23:26:52.283: I/Lab-ActivityTwo(30424): I am in onStart method
03-06 23:26:52.593: I/Lab-ActivityOne(30424): I am in onStop method
03-06 23:27:04.115: I/Lab-ActivityTwo(30424): I am in onPause method
03-06 23:27:04.155: I/Lab-ActivityOne(30424): I am in onRestart method
03-06 23:27:04.155: I/Lab-ActivityOne(30424): I am in onStart method
03-06 23:27:04.155: I/Lab-ActivityOne(30424): I am in onResume method
03-06 23:27:04.385: I/Lab-ActivityTwo(30424): I am in onStop method
03-06 23:27:04.395: I/Lab-ActivityTwo(30424): I am in onDestroy method
03-06 23:27:06.277: I/Lab-ActivityOne(30424): I am in onPause method
03-06 23:27:06.337: I/Lab-ActivityTwo(30424): I am in onCreate method
03-06 23:27:06.337: I/Lab-ActivityTwo(30424): I am in onStart method
03-06 23:27:06.337: I/Lab-ActivityTwo(30424): I am in onStart method
03-06 23:27:06.617: I/Lab-ActivityOne(30424): I am in onStop method
03-06 23:27:14.825: I/Lab-ActivityTwo(30424): I am in onPause method
03-06 23:27:14.855: I/Lab-ActivityOne(30424): I am in onRestart method
03-06 23:27:14.855: I/Lab-ActivityOne(30424): I am in onStart method
03-06 23:27:14.855: I/Lab-ActivityOne(30424): I am in onResume method
03-06 23:27:15.095: I/Lab-ActivityTwo(30424): I am in onStop method
03-06 23:27:15.095: I/Lab-ActivityTwo(30424): I am in onDestroy method

1 个答案:

答案 0 :(得分:0)

通过调用onClick()中的finish()结束ActivityTwo,永远不会调用onSaveInstanceState()。您可以在该方法中添加日志语句以进行验证。