我正在为 Android 创建一个文件管理器应用程序,下面的两个类是执行该操作的大部分逻辑。我正在做的是在 ContentList 启动时,它将 ContentListFragment 添加到 ContestList xml 中的容器布局中。通过添加 ContentListFragment,我得到当前路径,然后调用 setFileDir(String S),它基本上获取传入路径中的文件,获取该位置的文件列表,初始化数组,然后初始化适配器。然后我设置适配器、设置操作栏 UI 和上下文操作栏。现在,每次按下 ContentListFragment 上的一个项目时,它都会使用所选项目的路径创建另一个 ContentListFragment。然后将之前的片段添加到后堆栈中。现在一切都很好,花花公子,
ContentListFragment 的 onCreate 中的 setRetainInstance(true) 是唯一可以防止整个应用程序在应用程序更改方向时强制关闭。但是,这样做会导致应用程序稍后在我稍后返回时强制关闭(这是我遇到的主要问题,不明白为什么)。
当在方向更改时重新创建 Activity 时,我尝试用新的 ContentListFragment 替换当前的 ContentListFragment(此代码不在下面),但应用程序强制在 setFileDir() 处也以 NullPointerException 关闭,因此其他一切都崩溃了。
如何保存片段状态,以便在方向改变时一切都保持与方向改变之前相同,并且当我在一段时间后返回它时没有它强制关闭?
以防万一,我的应用程序只是一个带有递归 ContentListFragments 的 Activity。
public class ContentList extends DrawerActivity implements ContentListFragment.listFragmentListener{
// instance variables
private FragmentManager fm;
private FragmentTransaction ft;
private String currentPath;
// Initializes variables.
// If Activity is started for the first time, a path to the storage is
// received.
// Else, if it's an orientation change, the path is just retained.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_list);
if (savedInstanceState == null) {
fm = getSupportFragmentManager();
ft = fm.beginTransaction();
File file = Environment.getExternalStorageDirectory();
currentPath = file.getPath();
ft.add(R.id.content_container_fragment_listview, new ContentListFragment());
ft.commit();
} else {
fm = getSupportFragmentManager();
currentPath = savedInstanceState.getString("PATH");
}
}
// Grabs the currentPath in case of orientation changes.
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString("PATH", currentPath);
}
文件存储系统
public class ContentListFragment extends SherlockListFragment {
// instance variables
private ArrayList<Item> items;
private ArrayList<Item> copy_move_queue_items;
private ItemAdapter adapter;
private listFragmentListener lfListener;
private String currentPath;
private MenuItem menuItemRename;
// instantiates variables using setFileDir() and the path from the container Activity.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
currentPath = ((ContentList) getActivity()).getCurrentPath();
setFileDir(currentPath);
}
// Gets a reference to Activity interface
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
lfListener = (listFragmentListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ "must implement listFragmentListener");
}
}
// Sets the UI of the listView and the ActionBar
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.content_list_fragment, container, false);
File file = Environment.getExternalStorageDirectory();
String rootPath = file.getPath();
// Sets Action Bar title to current directory, and creates an Up
// affordance if the root path doesn't equal the current Path
getSherlockActivity().getSupportActionBar().setTitle(null);
getSherlockActivity().getSupportActionBar().setLogo(R.drawable.ab_icon);
if (currentPath.equals(rootPath)) {
getSherlockActivity().getSupportActionBar()
.setDisplayHomeAsUpEnabled(false);
getSherlockActivity().getSupportActionBar()
.setHomeButtonEnabled(false);
} else {
getSherlockActivity().getSupportActionBar()
.setDisplayHomeAsUpEnabled(true);
// getSherlockActivity().getSupportActionBar().setTitle("/" +
// (currentPath.substring(currentPath.lastIndexOf("/") +1)));
}
return v;
}
// Sets the long click Contextual Action Mode to the ListView
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setActionMode();
setListAdapter(adapter);
}
// Gets the list of files at the path and adds them to the Item ArrayList,
// initializing the adapter as well
public void setFileDir(String path) {
File file = new File(path);
File[] files = file.listFiles();
Arrays.sort(files, new fileComparator());
items = new ArrayList<Item>();
for (File f : files) {
items.add(new Item(f));
}
adapter = new ItemAdapter(getActivity(), R.layout.content_list_item, items);
}