OnCreate()在方向改变时不被调用

时间:2014-09-04 14:28:42

标签: android oncreate

我正在关注Android的教程(我对它很新!),无论何时更改方向,都会调用onCreate()。我按照步骤来描述这个变化,写了一个小日志,遇到了一个奇怪的问题......它不是写的!我不知道问题是什么> _>

有没有人看到可能导致它不写的原因,或者可以解释为什么它不会被调用?

以下是我的代码中的代码段。 setText会被调用,所以并不是说它没有超过那个点。只是因为某些原因,无论我多久换一次方向,标记"Test"下的"abc"日志都不会被调用

编辑:以上情况并非如此。更改方向时,不会运行setText。我修改了下面的代码来显示我的实验,让onCreate将文本更改为dateTime。它在应用程序加载时自行设置,然后无论我重新定位屏幕多少次,它都不会再次改变时间。它一直保持在11:01:55。

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

    if(savedInstanceState != null)
        Log.d("ProteinTracker", savedInstanceState.getString("abc"));

    Calendar c = Calendar.getInstance();
    android.text.format.DateFormat df = new android.text.format.DateFormat();
    TextView textView = (TextView)findViewById(R.id.mainActivityTextView);
    textView.setText(df.format("yyyy-MM-dd hh:mm:ss", new java.util.Date()));

 }

@Override
protected void onSaveInstanceState(Bundle outState) {
    outState.putString("abc", "test");
    super.onSaveInstanceState(outState);
}

对于这个noobish问题感到抱歉,谢谢!

编辑:根据要求,这是此活动的活动元素。它没有从我自动生成的eclipse中修改

    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

0 个答案:

没有答案
相关问题