Android 如何在我的片段中保存接口的状态?
我有一个Android 如何在我的片段中保存接口的状态?,android,interface,fragment,dialogfragment,Android,Interface,Fragment,Dialogfragment,我有一个活动,它使用ViewPager在ViewPager类中创建三个Fragments,将Fragments设置为setRetainInstace(true)当它们被创建时 在我的片段中,我显示了一些可编辑的信息。此Fragment启动对话框Fragment,以编辑信息 当用户不更改方向时,我可以更新信息并将结果发送回视图中的片段,但是,一旦我更改方向,并更新连接在对话框FragmentsonAttach()中的信息我的界面方法我得到一个NullPointerException 我不明白为什么
活动
,它使用ViewPager
在ViewPager
类中创建三个Fragment
s,将Fragment
s设置为setRetainInstace(true)代码>当它们被创建时
在我的片段中,我显示了一些可编辑的信息。此Fragment
启动对话框Fragment
,以编辑信息
当用户不更改方向时,我可以更新信息并将结果发送回视图中的片段,但是,一旦我更改方向,并更新连接在对话框Fragment
sonAttach()中的信息我的界面
方法我得到一个NullPointerException
我不明白为什么,因为每次启动新的对话框片段
时,总是调用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);
}
}我想您正在寻找创建的活动(Bundle)
,这是片段
相当于活动
类的onRestoreSavedInstanceState(Bundle)代码>
从文件中:
已在中添加ActivityCreated(Bundle savedInstanceState)上的公共无效
API 11级
当片段的活动已创建且
片段的视图层次结构已实例化。它可以用来做期末考试
在这些片段就位后初始化,例如检索
视图或恢复状态。对于使用
setRetainInstance(布尔值)以保留其实例,如此回调
告知片段何时与新活动完全关联
例子这在onCreateView(LayoutFlater、ViewGroup、,
捆绑)和onViewStateRestored(捆绑)之前。参数
savedInstanceState,如果片段是从
以前保存的状态,这是状态
当您的片段在方向更改中被销毁时,将其状态保存为活动的捆绑包中的名称-值对
,然后在需要重新创建时,在此方法中实例化一个新的接口
,并在新的片段
实例中设置相应的字段/检索包裹。如果您使用的是onAttach(Activity-Activity);,那么为什么不直接使用(CastToInterface)getActivty();?我的活动不是我的界面。接口在我的片段中声明,带有可编辑的数据。所以我在这个片段中创建了一个接口,然后在DialogFragment中实现它。我试图进行此更改,但我得到了ClassCastException,因为这试图将接口强制转换到我的活动中,而我的活动没有实现。你能发布你的片段代码吗?如果你想知道我在哪里附加接口,请添加我的DialogFragment@这是嵌套片段吗?在这种情况下,使用getParentFragment()获取对父片段的引用。如果每次创建新的DialogFragment时都调用onAttach()方法,这难道不重要吗?或者这是我实现接口的片段中的一个问题吗?不确定,我必须看看您是如何实现您的片段的。片段中有大量代码,我将尝试将其配对,以便我只共享您需要的内容。好的,即使调用了onAttach()之后,当方向更改时,我也会记录接口及其null。当我记录活动时,它不是空的。因此,我认为当方向变化不足以引起偏差时,Fragment类是空的,这只是猜测。如果您可以发布堆栈跟踪和活动,我将稍后再查看。