从SecondActivity按Back按钮时,第一个Activity被销毁

时间:2013-12-11 15:42:07

标签: android android-orientation android-configchanges

我正在实施在线视频播放的Android应用,并收到错误消息。为简单起见,我在这里制作了一个示例应用程序。有两个活动:MainActivity和SecondActivity。

MainActivity

public class MainActivity extends Activity {
    public static String TAG = "MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.e(TAG, "onCreate");
        setContentView(R.layout.activity_main);

        ((Button)findViewById(R.id.btn)).setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent intent = new Intent(getApplicationContext(), SecondActivity.class);
                startActivity(intent);
            }
        });
    }

    @Override
    protected void onStart() {
        // TODO Auto-generated method stub
        super.onStart();
        Log.e(TAG, "onStart");
    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        Log.e(TAG, "onResume");
    }

    @Override
    protected void onStop() {
        // TODO Auto-generated method stub
        super.onStop();
        Log.e(TAG, "onStop");
    }

    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        Log.e(TAG, "onDestroy");
    }
}

SecondActivity

public class SecondActivity extends Activity {
    public static String TAG = "SecondActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
    }

    @Override
    protected void onStart() {
        // TODO Auto-generated method stub
        super.onStart();
        Log.e(TAG, "onStart");
    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        Log.e(TAG, "onResume");
    }

    @Override
    protected void onStop() {
        // TODO Auto-generated method stub
        super.onStop();
        Log.e(TAG, "onStop");
    }

    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        Log.e(TAG, "onDestroy");
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        // TODO Auto-generated method stub
        super.onConfigurationChanged(newConfig);
        Log.e(TAG, "onConfigurationChanged");
    }
}

AndroidManifiest.xml    

    <activity
        android:name="com.example.androidtest.MainActivity"
        android:screenOrientation="portrait"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name="com.example.androidtest.SecondActivity"
        android:label="@string/title_activity_second" 
        android:configChanges="orientation|keyboardHidden|screenSize"
        android:screenOrientation="portrait">
    </activity>

MainActivity中有一个开始SecondActivity的按钮。我的方案是将设备置于横向模式,然后单击按钮以启动SecondActivity(以便SecondActivity自动旋转到横向模式)。然后立即按“返回”按钮返回MainActivity。但是MainActivity被摧毁了。这是我的日志:

12-11 22:22:20.940: E/MainActivity(400): onCreate
12-11 22:22:20.960: E/MainActivity(400): onStart
12-11 22:22:20.960: E/MainActivity(400): onResume
12-11 22:22:24.524: E/SecondActivity(400): onStart
12-11 22:22:24.524: E/SecondActivity(400): onResume
12-11 22:22:24.544: E/SecondActivity(400): onConfigurationChanged
12-11 22:22:24.634: E/MainActivity(400): onStop
12-11 22:22:24.634: E/MainActivity(400): onDestroy
12-11 22:22:34.133: E/SecondActivity(400): onStop
12-11 22:22:34.133: E/SecondActivity(400): onDestroy

MainActivity被销毁,当前活动为SecondActivity。如果我再按一次,SeconActivity将被销毁。 任何人都可以解释为什么MainActivity被破坏或者这是Android OS的错误。 提前谢谢。
P / S :出现这种情况,将设备置​​于横向,然后单击按钮启动SecondActivity并立即按返回。
OTHER案件工作正常。

1 个答案:

答案 0 :(得分:0)

您的MainActivity正在轮换时重新创建。为了防止在设备旋转时重新创建它,你将需要与SecondActivity相同的configChanges:android:configChanges =“orientation | keyboardHidden | screenSize”