2013-02-22 71 views
7

我有一個XML佈局2個SearchViews:兩個SearchViews在一個活動和屏幕旋轉

<RelativeLayout 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"> 

    <SearchView 
     android:id="@+id/my_first_custom_view" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" > 
    </SearchView> 

    <SearchView 
     android:id="@+id/my_second_custom_view" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@id/my_first_custom_view" > 
    </SearchView> 

</RelativeLayout> 

而且我通過膨脹的setContentView這個佈局我的MainActivity()。然後,我爲對方調用方法 setQuery()。

一切都很好,直到屏幕旋轉。 當我旋轉屏幕時,每個searchView都有文本「World」,而不是「Hello」和「World」。

public class MainActivity extends Activity { 

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

     SearchView firstSearchView = (SearchView)  findViewById(R.id.my_first_custom_view); 
     SearchView secondSearchView = (SearchView) findViewById(R.id.my_second_custom_view); 

     firstSearchView.setQuery("Hello!", false); 
     secondSearchView.setQuery("World", false); 
    } 
} 

有人可以解釋發生了什麼問題嗎?

+0

這是您的searchviews得到的唯一參考是在onCreate?我假設沒有..如果不是,那麼你可能想發佈相關的代碼 – JoxTraex 2013-02-22 13:14:22

+1

我試過你的代碼,並有與你一樣的行爲...所以一切重現該問題是在 – ben75 2013-02-22 13:16:06

+0

是的,這是唯一的一個參考。我不再使用它了。 – lukjar 2013-02-22 13:18:34

回答

11

SearchView使用它作爲內容上看是由於膨脹的佈局文件。因此,活動佈局中使用的所有SearchViews(如您的情況)都將具有相同ID的內容和視圖。當Android嘗試保存狀態以處理配置更改時,它會看到來自SearchViewsEditTexts具有相同的ID,並且它將爲它們恢復相同的狀態。

來處理這個問題的最簡單方法是使用ActivityonSaveInstanceStateonRestoreInstanceState這樣的:

@Override 
protected void onSaveInstanceState(Bundle outState) { 
    super.onSaveInstanceState(outState); 
    // state for the first SearchView 
    outState.putString("sv1", firstSearchView.getQuery().toString()); 
    // state for the second SearchView 
    outState.putString("sv2", secondSearchView.getQuery().toString()); 
} 

@Override 
protected void onRestoreInstanceState(Bundle savedInstanceState) { 
    super.onRestoreInstanceState(savedInstanceState); 
    // properly set the state to balance Android's own restore mechanism 
    firstSearchView.setQuery(savedInstanceState.getString("sv1"), false); 
    secondSearchView.setQuery(savedInstanceState.getString("sv2"), false); 
} 

也有看看這個related question

+0

謝謝。有用 ;)。在這種情況下非常重要的是在onRestoreInstanceState()方法中恢復實例。當我在onCreate中這樣做時,它不起作用。 onCreate中的值「Hello」和「World」可能會覆蓋onRestore方法。 – lukjar 2013-03-02 13:39:44

-1

緩解此問題的一種方法是捕獲您的活動的方向事件更改,然後再次在您的兩個搜索視圖上設置查詢。

+0

你能寫更多的東西嗎? – lukjar 2013-02-22 13:23:50

+0

它不是太複雜,Google如何傾聽您活動中的方向變化,然後重新配置您的兩個搜索視圖。 – JoxTraex 2013-02-22 13:29:14

-2

加入到這個活動中體現在你有兩個SearchView

android:configChanges="keyboardHidden|orientation|screenSize" 
+0

@ loocash1989檢查我的答案,並通知我這是否有幫助。 – Rahil2952 2013-02-28 12:40:59

+1

好吧......這不是問題的答案。這是一種解決方法,但它有一些缺點。 – ben75 2013-02-28 12:45:26

+0

這對我來說並不是很好的解決方案,但我承認它也可以。我正在尋找SearchView中的bug。此外我的活動應該在屏幕旋轉後重新創建,我不希望更改其默認行爲。感謝您的回答。我認爲這可能對某人有幫助。 – lukjar 2013-03-02 13:59:43