2015-07-20 36 views
0

我有一個關於從MainActivity傳遞變量到我的片段的問題。將變量傳遞給已創建的片段並重新加載

在我的MainActivity我有一個按鈕,它會創建並顯示一個片段分別重新加載,如果已經創造了它。 (I am using this code to reload the fragment) 我通過Bundle -class傳遞了2個雙變量。這在我第一次創建片段時起作用,但是當我想重新加載片段時,如何更新變量?在我的代碼中,我沒有創建一個新的Fragment,而是分離並重新附加它。這是可能的,或者我需要完全不同的東西嗎?

MainActivity

public void loadWeatherFragment(){ 

    longitude = this.getLongitude(); 
    latitude = this.getLatitude(); 
    FragmentManager fm = getFragmentManager(); 
    Fragment currentFragment = this.getFragmentManager().findFragmentById(R.id.fragment_container); 


    //If the fragment already exists, reload it 
    if (currentFragment instanceof FragmentWeather) { 
     FragmentTransaction fragTransaction = this.getFragmentManager().beginTransaction(); 
     fragTransaction.detach(currentFragment); 
     fragTransaction.attach(currentFragment); 
     fragTransaction.commit(); 
    } 
    //Else create the fragment 
    else{ 
     FragmentWeather mFragmentWeather = new FragmentWeather(); 
     Bundle args = new Bundle(); 
     args.putDouble("longitude", longitude); 
     args.putDouble("latitude", latitude); 
     mFragmentWeather.setArguments(args); 
     FragmentTransaction ft = fm.beginTransaction(); 
     ft.add(R.id.fragment_container, mFragmentWeather); 
     ft.commit(); 
    } 
} 

FragmentWeather

public class FragmentWeather extends Fragment { 


    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 
     View view = inflater.inflate(R.layout.fragment_weather, container, false); 

     Double latitude = this.getArguments().getDouble("latitude"); 
     Double longitude = this.getArguments().getDouble("longitude"); 

    } 

} 

回答

1

保存在您的Fragment類中的變量,如下面的例子。另外請注意我是如何通過在onCreateView()中創建變量latitudelongitude來簡單地爲它們保存一個值。現在,您可以隨時在片段內訪問它們。

公共類FragmentWeather擴展片段{

private Double latitude; 
private Double longitude; 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 
    View view = inflater.inflate(R.layout.fragment_weather, container, false); 

    latitude = this.getArguments().getDouble("latitude"); 
    longitude = this.getArguments().getDouble("longitude"); 

} 
} 

如果需要更新從其他類這些變量,創建變量的getter和setter(許多IDE也可以爲你做這個,在Windows Android Studio中Alt鍵+插入是熱鍵)。然後只要一個班級能看到你的Fragment,它也可以訪問這些變量。定義

public Double getLongitude() 
{ 
    return this.longitude; 
} 

public void setLongitude(double val) 
{ 
    this.longitude = val; 
} 

,並使用它們

FragmentWeather fragment = new FragmentWeather(); 
double longitude = 0.0; 
fragment.setLongitude(longitude); 
double latitude = fragment.getLatitude(); 
+0

嘿。首先,我不明白你的意思。但現在我明白了。如果我在片段中創建setter方法,我可以從我的MainActivity調用此方法來更新我的數據。 'currentFragment.setLatitude(99.99)'工程!謝謝! –

+0

太棒了!如果有人在將來讀它,我會編輯答案更清楚。 – milez

0

是否有任何理由,爲什麼片段具有被卸載,然後reloded?

最簡單的辦法就是給片段的公共API,可以從活動被稱爲:

public class FragmentWeather extends Fragment { 
    //... 
    // public api to be used by owning activity 
    public setLatLon(double latitude, double longitude) { 
     // do what is neccessary to show lat/lon 
    } 
} 

public class MainActivity ... { 
    public void loadWeatherFragment(){ 

     Fragment currentFragment = this.getFragmentManager().findFragmentById(R.id.fragment_container); 

     //If the fragment already exists, reload it 
     if (currentFragment instanceof FragmentWeather) { 
      ((FragmentWeather) currentFragment).setLatLon(...) 
     } 
    }  
}