1
每次進入主要活動時,下面的代碼將被執行並正常工作(這意味着indexStatus.setText(msg.obj.toString());
總是顯示最新的味精)。Android:TextView setText僅在第一時間運作
但是,如果我按back
鍵切換出來,然後在同一個屏幕開關(reBuildIndex
將再次運行),將Log.d(TAG, msg.obj.toString());
做工精細,打印出最新的味精,但indexStatus.setText
不起作用,最新的味精呢不按我的預期顯示。
這是怎麼回事?任何建議?
void rebuildIndex(boolean reCreate) {
final TextView indexStatus = (TextView) this.findViewById(R.id.index_status);
indexStatus.setVisibility(View.INVISIBLE);
indexStatus.setText(R.string.rebuild_index_progress_title);
final Handler statusHandler = new Handler(Looper.getMainLooper(), new Handler.Callback() {
@Override
public boolean handleMessage(Message msg) {
if (msg.obj == null) {
indexStatus.setVisibility(View.GONE);
} else if (msg.obj.equals("show")) { // TODO quick demo, shouldn't use literal text.
indexStatus.setVisibility(View.VISIBLE);
} else {
Log.d(TAG, msg.obj.toString());
if (indexStatus.getVisibility() != View.VISIBLE) {
indexStatus.setVisibility(View.VISIBLE);
}
indexStatus.setText(msg.obj.toString());
indexStatus.invalidate();
}
return false;
}
});
Indexer.rebuildIndexIfNecessary(statusHandler, reCreate);
}
UPDATE:
我在第三個條件setText
後添加一個indexStatus.invalidate();
,也不管用。
更新: 錯誤味精在原來的帖子,我說按home
,但實際上它的back
切換出來。 home
按鈕開/關工作正常。
更新: 試圖將處理程序作爲全局變量,但沒有奏效。現在我禁用了back
事件。
這很有趣。我可否知道當它沒有按預期運作時你看到了什麼(信息)? – LightYearsBehind
你能再給出一些關於'rebuildIndex()'被再次調用的信息嗎?它來自'onResume()'嗎?什麼實際觸發方法以及觸發'Handler'的'handleMessage()'是什麼? –
''rebuildIndex'從activity的'onStart'被調用,並且這個消息是一個普通的文字'String' – MrROY