我正在为Android创建一个文件管理器应用程序,下面的两个类是执行此操作的大部分逻辑。我正在做的是在启动ContentList时,它将ContentListFragment添加到ContestList xml中的容器布局中。通过添加ContentListFragment,我得到当前路径,然后调用setFileDir(String S),它基本上获取传入路径中的File,获取该位置的Files列表,初始化数组,然后初始化适配器。然后我设置适配器,设置Action Bar UI和上下文Action Bar。现在,每次按下ContentListFragment上的项目时,它都会使用所选项目的路径创建另一个ContentListFragment。然后将之前的片段添加到后栈。现在这一切都很好和花花公子,问题出现时,方向发生。
在ContentListFragment的onCreate中,setRetainInstance(true)是唯一能够在应用程序更改方向时阻止整个应用程序关闭的东西。但是,以这种方式这样做会导致应用程序在我稍后返回它之后强制关闭(这是我遇到的主要问题,并且无法理解为什么)。
我尝试在方向更改时重新创建Activity时替换当前的ContentListFragment(此代码不在下面),但app force也会在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);
}
dfsas
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);
}
答案 0 :(得分:12)
将此用于方向上片段的保存状态。
onCreate(Bundle save)
{
super.onCreate(save);
setRetainInstance(true);
}
答案 1 :(得分:7)
这对我有用
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
onCreate(savedInstanceState);
}
并在您的活动代码
中的清单文件中
android:configChanges="orientation|screenSize"
答案 2 :(得分:0)
只是张贴这个,因为这是工作和采访中面临的主要问题:) 在API 13之前,我们使用了 onRetainNonConfigurationInstance(),这是不推荐使用的(此方法与现在移入片段的Activity相关)现在我们有一个新方法,称为 setRetainInstance()< / strong>(片段类的方法部分)即使在创建活动之后也会保存片段的状态。