我有一個Activity
,它使用ViewPager
在ViewPager
類中創建三個Fragment
s,當它們被創建時,我將Fragment
s設置爲setRetainInstace(true);
。如何保存我的片段中的接口狀態?
在我的Fragment
s之內,我顯示了一些可編輯的信息。此Fragment
啓動DialogFragment
來編輯信息。
當用戶未更改方向時,我可以更新信息並將結果發送回視圖中的片段,但是,一旦我更改了方向並更新了信息,我的Interface
已附加在DialogFragment
s onAttach()
方法我得到NullPointerException
。
我不明白爲什麼,因爲每次啓動新的DialogFragment
時總會調用onAttach()
方法。
我應該如何解決這個問題? 我可以保存界面的狀態嗎?如果是的話如何?
這是我DialogFragment代碼: 的GenericDialogFragment類僅用於更改外觀
public class Fragment1 extends GenericDialogFragment implements WebServiceResult{
// -------------------------------------------------------------------------
// Member Variables
//--------------------------------------------------------------------------
//Webservice Callback
private WSRequest mActiveRequest = null;
// The Current Context of the Application
private Context mClassContext = null;
// Interface reference for communication
private static CommunicateResults communicateResults = null;
// -------------------------------------------------------------------------
// New Instance Method
//--------------------------------------------------------------------------
public static Fragment1 newInstance(int userId, GenericObject [] objects, GenericGroup [] groups, Object currentObject){
// Initialize a new Fragment1
Fragment1 fragment = new Fragment1();
// Create a new Bundle
Bundle args = new Bundle();
fragment.setArguments(args);
// Return the Fragment1
return fragment;
}
// -------------------------------------------------------------------------
// Class Functions/Methods
//--------------------------------------------------------------------------
// States that the Interface is attached to the parent activity
@Override public void onAttach(Activity activity)
{
// Perform the Default Behavior
super.onAttach(activity);
Log.d("ONAttach()","On attach() is called");
// Try
try{
// Attach the interface to the activity
communicateResults = (CommunicateResults) ((MainActivity) getActivity()).findFragment(EditableFragment1.class);
}catch(Exception ex){
// Print the stack trace
ex.printStackTrace();
}
}
// States that the Dialog's View has been Created
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
// Return the Inflated XML Layout
return inflater.inflate(R.layout.fragment1, container, false);
}
// States that the fragment has been created, last chance to update the UI
@Override
public void onActivityCreated(Bundle savedInstanceState){
// Perform the Default behavior
super.onActivityCreated(savedInstanceState);
mClassContext = getActivity();
// Get the Arguments passed into the Fragment instance
savedStateData = getArguments();
// Reference all the UI Elements in the View
// Add listeners to the Button Widgets
// If the savedInstanceState is not null, get the current object
if(savedStateData != null){
// Get the object out of the state data
mCurrentObject = savedStateData.getParcelable(STATE_DATA_CURRENT_OBJECT);
}
}
//-----------------------------------------------------------------------------
// Webservice Callback methods
//-----------------------------------------------------------------------------
// States that the web service has succeeded
@Override public void webserviceSucceeded(WebServiceBase finishedService, Object responseData)
{
Log.d("EDIT Object", responseData.toString());
if(responseData != null){
if(mDeletingObject){
// Send Back to the object to remove
communicateResults.sendBackData(mCurrentObject, ACTION_DELETE);
}else{
JSONObject tempObject = (JSONObject) responseData;
try{
// Parse Data ...
}catch(Exception ex){
ex.printStackTrace();
// TODO: The Object was deleted from the Lest
}
// If we are creating a object, bundle the information to pass to the parent activity
if(mCreatingObject){
// Create a new Workout Object
mCurrentObject = new Object();
// Callback to Parent Activity to notify that data has changed
communicateResults.sendBackData(mCurrentObject, ACTION_CREATE);
// Else the Object was updated
}else{
// Create a new Object
mCurrentObject = new Object();
// Callback to Parent Activity to notify that data has changed
communicateResults.sendBackData(mCurrentObject, ACTION_UPDATE);
}
}
}
// Dismiss the fragment
}
// States that the web service has failed
@Override
public void webserviceFailed(WebServiceBase finishedService,
Object errorData) {
// Display the Error
displayErrorData(errorData);
}
}
聽起來像你的Activity是你的接口,如果你使用onAttach(Activity activity);那麼爲什麼不直接使用(CastToInterface)getActivty();? – Submersed
我的活動不是我的界面。該界面在我的Fragment中用可編輯的數據聲明。所以我在這個片段中創建一個接口,然後我在DialogFragment中實現它。我試圖做出這種改變,我得到了ClassCastException,因爲這會試圖將接口強制轉換爲我的Activity,其中未執行 –
難題。你能發佈你的片段代碼嗎? – Submersed