我一直在片段中获取一些旧数据,我基本上想要从Scratch构建自己。 这让我疯了;)
以下是相关的代码位:在活动中,创建Fragment时:
...
Log.w(LOG_TAG, "remoteFrag is NOT available");
// Create fragment
int savePos=mDataManager.getPosition();
mDataManager.setPosition(position);
Bundle args = new Bundle();
remoteFrag = new RemoteFragment();
args.putSerializable(ExtraXMLParser, mDataManager);
Log.w(LOG_TAG, "mDataManager is " + ((mDataManager == null)? "not ":"") + "set");
remoteFrag.setArguments(args);
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_container, remoteFrag);
if(mDataManager.getPosition()!=savePos)
transaction.addToBackStack(null);
// Commit the transaction
transaction.commit();
...
我想要实现的目标:在活动中,我想要重新启动(基本上,如果我在本地Wifi网络中,App的行为会有所不同,菜单会有所不同,并且可以使用不同的选项。启动画面探测此环境类型并适当初始化DataManager。
...
Log.w(LOG_TAG,"terminating...");
leaveRemoteView();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.remove(remoteFrag);
remoteFrag=null;
mDataManager=null;
Intent intent = getBaseContext().getPackageManager().getLaunchIntentForPackage(getBaseContext().getPackageName() );
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
...
在remoteFrag中我有:
public class RemoteFragment extends Fragment implements DataManager.ProcessXMLData {
RemoteDetail remoteBottom = null;
private View thisView;
private ViewGroup thisContainer;
static private DataManager _dataManager = null;
static private boolean _dataManagerRegistered = false;
final private static String LOG_TAG = "RemoteFragment";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// If activity recreated (such as from screen rotate), restore
// the previous article selection set by onSaveInstanceState().
// This is primarily necessary when in the two-pane layout.
Log.w(LOG_TAG, "creating RemoteFragment");
if(_dataManager == null) {
try {
_dataManager = (DataManager) getArguments().getSerializable(RemoteDisplay.ExtraXMLParser);
}
catch (NullPointerException e) {
_dataManager = null;
Log.e(LOG_TAG , "mXmlParser not passed as arguments");
}
}
else
Log.d(LOG_TAG, "dataManager was not null");
thisContainer = container;
...
如果我强制应用程序使用上面的第二位代码重新启动,并且我使用第一和第三部分启动Fragment,我会收到以下LogCat:
03-21 14:32:37.930: D/ViewRootImpl(12915): ViewPostImeInputStage ACTION_DOWN
03-21 14:32:38.090: D/RemoteDisplay(12915): selection is 0
03-21 14:32:38.090: W/RemoteDisplay(12915): remoteFrag is NOT available
03-21 14:32:38.090: W/RemoteDisplay(12915): mDataManager is set
03-21 14:32:38.110: W/RemoteFragment(12915): creating RemoteFragment
03-21 14:32:38.110: D/RemoteFragment(12915): dataManager was not null
03-21 14:32:38.110: D/RemoteFragment(12915): will inflate now...
03-21 14:32:38.130: D/RemoteFragment(12915): finshed create view
令人困惑的消息是“dataManager not null”。 这意味着片段没有尝试从作为参数传递的活动接收变量_dataManager,并且在使用旧值调用OnCreateView之前,在类顶部的初始化被覆盖。
我可以在哪里以及如何避免这种情况?我设法根据需要设置片段的唯一方法是从最近的活动中完全删除应用程序。
对不起,如果结果有点长......;)
答案 0 :(得分:0)
我刚刚发现了一个非常明显的解决方案。 我只是为Fragment添加了一个额外的自定义构造函数来初始化我的变量:
RemoteFragment() {
_dataManager=null;
_dataManagerRegistered = false;
}
我仍然有点疑惑为什么没有使用自定义构造函数。