簡體   English   中英

旋轉屏幕后刷新活動

[英]Refresh a activity after a screen rotation

我使用Android 2.3.3。 旋轉屏幕后,刷新活動視圖時出現問題。

我的活動有一個變量(計數器),一個TextView(顯示計數器值),一個按鈕(增加計數器並顯示它)和TimerTask(增加計數器並顯示它)。 它可以正常工作。 我的TextView在Button或TimerTask的每個事件之后顯示一個新值。

在我旋轉手機之前,它一直有效。 TimerTask的事件不再刷新我的TextView。 按鈕和旋轉屏幕仍可以修改我的視圖。 我的TimerTask仍然增加了我的變量,但屏幕上沒有任何變化。 我檢查了一下,TimerTask仍然運行並執行。

這不是我真正的項目,只是我的錯誤所在。

我唯一的活動:

package com.test;

import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

public class TestRefreshActivity extends Activity {
    static TimerTask mTimerTask=null;
    static Timer t=new Timer();
    final Handler handler = new Handler();
    static int counter=0;

    //-------------------------------------------------
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        //create and run the timer
        if(mTimerTask==null){
            Log.d("Test","new TimerTask");
            mTimerTask = new TimerTask() {
                public void run() {
                    handler.post(new Runnable(){
                        public void run(){
                            counter++;
                            refreshCounter();
                            Log.d("TIMER", "TimerTask run");
                        }
                    });
                }
            };
            t.schedule(mTimerTask, 500, 3000); 
        }
        Log.d("Test","--onCreate--");
        refreshCounter();
    }

    //-------------------------------------------------
    @Override
    public void onBackPressed() {
        super.onBackPressed();
        mTimerTask.cancel();
        TestRefreshActivity.this.finish();
    }
    //-------------------------------------------------
    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d("Test","--onDestroy--");
    }

    //-------------------------------------------------
    public void onBtnClick(View view){
        counter++;
        refreshCounter();
    }

    //-------------------------------------------------
    //the only function which refreshes the TextView
    private void refreshCounter(){
        Runnable run = new Runnable(){
            public void run(){
                TextView textView = (TextView)findViewById(R.id.textView1);
                textView.setText("counter="+counter);
                textView.invalidate();
            }
        };
        synchronized(run){
            TestRefreshActivity.this.runOnUiThread(run);
        }
    }
}

我唯一的看法是:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="onBtnClick"
        android:text="Button" />

</LinearLayout> 

我不明白為什么旋轉后我的TimerTask無法更改視圖。 我知道,輪換破壞了我的活動以重新創建它,但是只有靜態變量可以存活。

謝謝你的幫助。

問候,

android:configChanges="keyboardHidden|orientation" -准則不鼓勵的解決方案。 它只會為您節省屏幕旋轉的時間,但是當您的應用程序在后台運行時,其活動將被android殺死后,您的應用程序仍將無法使用。

看你做什么。 您提供了對靜態計時器處理程序的引用。 旋轉手機時,將重新創建活動(意味着一個活動被銷毀,而另一個活動被創建)。 您的活動被銷毀了,但是靜態成員仍然存在,並且他們仍然持有對已死活動的處理程序的引用。

然后使用新的處理程序創建一個新的活動,但是您的timertask對此一無所知。

因此,您有一個靜態計時器來更新無效的處理程序,而活動則不執行任何操作。

我將刪除靜態變量,並將當前計時器值保存在束中的onSaveInstanceState中。

或者(甚至對您來說更容易)在TimerTask中為處理程序創建一個setter,並在執行此檢查if(mTimerTask==null){並查看該計時器是否已實例化后,在其中設置一個新的處理程序( mTimerTask.setHandler(yourActivityHandler) ),因此當創建新活動時,您的timertask將發布在新的處理程序中。

打開您的android清單文件,並按以下所示更改Activity標簽

 <activity android:name=".aaa"
                  android:configChanges="keyboardHidden|orientation"
                  android:label="@string/app_name"></activity>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM