1

我在计数程序中有一个问题,它显示了 onCreate、onStart 等方法被调用了多少次,当我从 Activity1 转到 Activity2 然后返回 Activity1 时,它似乎工作正常,但是当我切换方向时,只有mResume 增加。因此,例如,当我启动应用程序并切换方向 3 次时,结果如下所示:

onCreate() calls:1  
onStart() calls:1  
onResume() calls:4  
onRestart() calls:1

另一个奇怪的事情是,当我从 onRestoreInstanceState 中注释掉 mResume、mRestart 和 mStart 时,突然 2 个变量得到更新 mStart 和 mResume。

public void onRestoreInstanceState(Bundle savedInstanceState) {
    // Always call the superclass so it can restore the view hierarchy
    super.onRestoreInstanceState(savedInstanceState);

    Log.i(TAG, "Entered the onRestoreInstanceState() method");

    // Restore state members from saved instance
    //mResume = savedInstanceState.getInt(RESUME_KEY);
    //mRestart = savedInstanceState.getInt(RESTART_KEY);
    //mStart = savedInstanceState.getInt(START_KEY);
    mCreate = savedInstanceState.getInt(CREATE_KEY);

这是完整的代码

public class ActivityOne extends Activity {    
    // Use these as keys when you're saving state between reconfigurations
    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-ActivityOne";

    // Lifecycle counters

    // TODO:
    // Create variables named
    // mCreate, mRestart, mStart and mResume
    // to count calls to onCreate(), onRestart(), onStart() and
    // onResume(). These variables should not be defined as static.

    // You will need to increment these variables' values when their
    // corresponding lifecycle methods get called.
    int mCreate;
    int mRestart;
    int mStart;
    int mResume;

    // TODO: Create variables for each of the TextViews
    // named mTvCreate, mTvRestart, mTvStart, mTvResume.
    // for displaying the current count of each counter variable

    TextView mTvCreate;
    TextView mTvRestart;
    TextView mTvResume;
    TextView mTvStart;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_one);

        // 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 launchActivityTwoButton = (Button) findViewById(R.id.bLaunchActivityTwo);
        launchActivityTwoButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO:
                // Launch Activity Two
                // Hint: use Context's startActivity() method

                // Create an intent stating which Activity you would like to
                // start
                Intent intent = new Intent(v.getContext(), ActivityTwo.class);

                // Launch the Activity using the intent
                startActivity(intent);

            }
        });

        // Has previous state been saved?
        if (savedInstanceState != null) {

            // TODO:
            // Restore value of counters from saved state
            // Only need 4 lines of code, one for every count variable
            mCreate = savedInstanceState.getInt(CREATE_KEY);
            mRestart = savedInstanceState.getInt(RESTART_KEY);
            mStart = savedInstanceState.getInt(START_KEY);
            mResume = savedInstanceState.getInt(RESUME_KEY);

        }

        // 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 += 1;
        displayCounts();

    }

    // Lifecycle callback overrides

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

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

        // TODO:
        // Update the appropriate count variable
        // Update the user interface
        mStart += 1;
        displayCounts();

    }

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

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

        // TODO:
        // Update the appropriate count variable
        // Update the user interface
        mResume += 1;
        displayCounts();

    }

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

        // Emit LogCat message
        Log.i(TAG, "Entered the onPause() method");
    }

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

        // Emit LogCat message
        Log.i(TAG, "Entered the onStop() method");
    }

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

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

        // TODO:
        // Update the appropriate count variable
        // Update the user interface
        mRestart += 1;
        displayCounts();

    }

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

        // Emit LogCat message
        Log.i(TAG, "Entered the onDestroy() method");
    }

    @Override
    public void onSaveInstanceState(Bundle savedInstanceState) {
        super.onSaveInstanceState(savedInstanceState);

        Log.i(TAG, "Entered the onSaveInstanceState() method");
        // TODO:
        // Save state information with a collection of key-value pairs
        // 4 lines of code, one for every count variable
        savedInstanceState.putInt(CREATE_KEY, mCreate);
        savedInstanceState.putInt(RESTART_KEY, mRestart);
        savedInstanceState.putInt(START_KEY, mStart);
        savedInstanceState.putInt(RESUME_KEY, mResume);


    }

    public void onRestoreInstanceState(Bundle savedInstanceState) {
        // Always call the superclass so it can restore the view hierarchy
        super.onRestoreInstanceState(savedInstanceState);

        Log.i(TAG, "Entered the onRestoreInstanceState() method");

        // Restore state members from saved instance
        mResume = savedInstanceState.getInt(RESUME_KEY);
        mRestart = savedInstanceState.getInt(RESTART_KEY);
        mStart = savedInstanceState.getInt(START_KEY);
        mCreate = savedInstanceState.getInt(CREATE_KEY);

        //displayCounts();
    }

    // Updates the displayed counters
    // This method expects that the counters and TextView variables use the
    // names
    // specified above
    public void displayCounts() {

        mTvCreate.setText("onCreate() calls: " + mCreate);
        mTvStart.setText("onStart() calls: " + mStart);
        mTvResume.setText("onResume() calls: " + mResume);
        mTvRestart.setText("onRestart() calls: " + mRestart);

    }
}

这是xml

<LinearLayout   xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView android:id="@+id/create"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/onCreate" />

    <TextView android:id="@+id/start"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/onStart" />

    <TextView android:id="@+id/resume"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/onResume" />

    <TextView android:id="@+id/restart"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/onRestart" />

    <Button android:id="@+id/bLaunchActivityTwo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button" />

</LinearLayout>
4

1 回答 1

1

您从对象中获取mCreate、和的值两次,分别是 inmStart和in 。这意味着在两次检索值之间对计数所做的任何更改都将被忽略。mRestartmResumeBundleonCreateonRestoreInstanceState

如果它被调用,则在onRestoreInstanceState之后被调用onCreate,因此您不应该恢复 in 中的值,onRestoreInstanceState因为它们已经在 中恢复了onCreate

于 2015-03-01T15:06:11.323 回答