2013-10-11 179 views
1

保存簡單的變量,這是我的問題,當我創建android系統,例如一個片段讓我們說:在Android的碎片

GastosSectionFragment gastosFragment = new GastosSectionFragment(); 
Fragment.gastosFragment.darUserID(userKey); 

我發送一個簡單的整數該活動,沒有任何問題。例如讓我說我發送數字1.它將被存儲在該類的屬性中,因爲我創建了該片段。 一切都會正常工作,我收到號碼。但是當我旋轉手機時,變量「userKEY將丟失,它將轉換爲cero」當我水平旋轉手機時,我無法保存該號碼,如何保存該變量的數據。

這裏我把我怎麼收到的片段裏面的「用戶鑰」的代碼,

public long darUserID(long userKey) { 
    // TODO Auto-generated method stub 
    return user_id = userKey; 
} 

並與onCreate方法的屬性。

public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 
    View rootView = inflater.inflate(R.layout.fragment_section_gastos, 
      container, false); 

    idStatico = darUserID(user_id); 

而且我無法保存那個小號碼。這不是一個FragmentActivity,這個類只是一個片段。第一次罰款,旋轉屏幕丟失了我以前收到的號碼。片段沒有OnSaveBundleInstances。

+0

我會嘗試集裝箱活動中存放。 –

回答

1

片段可以保存user_id捆綁在onSaveInstanceState方法和onCreateView方法中的方向更改後恢復。檢查此fragment guide,特別是在示例中顯示保存碎片狀態。

活動代碼保持不變(我只是改變了方法名)和你的片段應該是這樣的:

class GastosSectionFragment extends Fragment { 

     private static final String BUNDLE_USER_ID = "BUNDLE_USER_ID"; 

     private long user_id; 

     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     View rootView = inflater.inflate(R.layout.fragment_section_gastos, container, false); 

     if (savedInstanceState != null) 
      user_id = savedInstanceState.getLong(BUNDLE_USER_ID); 

     // now is your user_id restored after orientation change, 
     // so you can do some stuff with it    

     return rootView; 
     } 

     public void onSaveInstanceState(Bundle outState) { 
     super.onSaveInstanceState(outState); 
     outState.putLong(BUNDLE_USER_ID, user_id); 
     } 

     public long setUserID(long userKey) { 
     this.user_id = userKey; 
     } 
    } 
+0

哇,我不知道,實際上我們可以做到這一點,看到你的答案後,很難說這一點,但這對我來說不起作用,但你給出了這個想法。謝謝。我會發布我如何解決這個問題。 – OscarLeif

0

KOSO的答案後,我這是怎麼解決這個問題,希望它可以幫助別人。

當我第一次創建Fragment時一切正常,發送userKey,一切都很好。

GastosSectionFragment gastosFragment = new GastosSectionFragment(); 
Fragment.gastosFragment.darUserID(userKey); 

現在,爲了解決這個問題,當手機被旋轉時,例如一個片段到水平它將調用重寫方法OnCreateView()。每次旋轉手機時,都會調用此方法。

public View onCreateView(LayoutInflater inflater, ViewGroup container, 
    Bundle savedInstanceState) 
{ 
View rootView = inflater.inflate(R.layout.fragment_section_gastos, 
    container, false); 
//This is not null when we rotate the screen, first we save the Bundle SavedInstance 
//After do that we put a integer, in this case the userKey 
//then we recover the user key, and everything will work fine. 
if (savedInstanceState != null) 
{ 
    this.savedInstanceState = savedInstanceState; 
    savedInstanceState.putInt("userKey", (int) idStatico); 
    idStatico = savedInstanceState.getInt("userKey"); 
} 
//this will be null for the first time, only the first time this will bse used. 
if (savedInstanceState == null) 
{ 
    idStatico = darUserID(user_id); 
} 
//Do the rest of the work 

} 
1
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setRetainInstance(true); 
} 

add方法重寫,你變節能狀態