我到处搜索但找不到符合我要求的答案。 事情就是这样:我有一个片段,它显示了一个包含两个其他片段的视图寻呼机,这两个片段都使用asynctask从Parse.com数据库中获取信息。我使用asynctask的原因只是在应用程序从解析中获取数据时显示进度条。我的问题是:每当我将方向更改为横向(或反之亦然)时,“背景操作”方法,应用程序崩溃。我想做的是保持asynctask进程运行而不考虑方向的变化,这可能吗?
P.S:我尝试在parentFragment上加上“setRetainInstance(true)”,但它没有做到这一点。当我将“setRetainInstance(true)”放在视图寻呼机内的fragements上时,我会收到一个力量。
这是我的视图分页器上的片段1(NewsFragment):
public class NewsFragment extends Fragment {
(...)
public NewsFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View fragmentView = inflater.inflate(R.layout.fragment_news, container, false);
mListView = (ListView) fragmentView.findViewById(R.id.MyListView);
mListView.addFooterView(new View(getActivity()), null, false);
mListView.addHeaderView(new View(getActivity()), null, false);
new RemoteDataTask().execute();
(...)
return fragmentView;
}
// RemoteDataTask AsyncTask
private class RemoteDataTask extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... params) {
// Create the array
mProductsList = new ArrayList<Product>();
try{
ParseQuery<ParseObject> query = ParseQuery.getQuery("Products");
query.orderByDescending("createdAt");
query.setCachePolicy(ParseQuery.CachePolicy.NETWORK_ELSE_CACHE);
ob = query.find();
for (ParseObject parseObject : ob) {
// Locate images in flag column
String title = (String) parseObject.get("title");
String descrip = (String) parseObject.get("descrip");
preco = (String) parseObject.get("preco");
String data = (String) parseObject.get("data");
String detail = (String) parseObject.get("detail");
user = (String) parseObject.get("user");
imagefile = parseObject.getParseFile("foto");
if (imagefile!=null){
imageUrl = parseObject.getParseFile("foto").getUrl();
}
productId = parseObject.getObjectId();
product = new Product(title, descrip, preco, data, detail, user, imagefile, imageUrl, productId);
mProductsList.add(product);
}
} catch (ParseException e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
// Locate the listview in listview_main.xml
mListView.setAdapter(new MyAdapter());
loadpd = (ProgressBar) getView().findViewById(R.id.loadpd);
getView();
loadpd.setVisibility(View.INVISIBLE);
}
}(...)}
这是我在分页器上的片段2(UserProductsFragment)
public class UserProductsFragment extends Fragment {
(...)
public UserProductsFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
setHasOptionsMenu(true);
// Inflate the layout for this fragment
View fragmentView = inflater.inflate(R.layout.fragment_news_user, container, false);
mListView = (ListView) fragmentView.findViewById(R.id.MyUserListView);
mListView.addFooterView(new View(getActivity()), null, false);
mListView.addHeaderView(new View(getActivity()), null, false);
new RemoteDataTask().execute();
return fragmentView;
}
private class RemoteDataTask extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... params) {
// Create the array
if (currentUser != null) {
userid= currentUser.getObjectId();
mProductsList = new ArrayList<Product>();
try{
ParseQuery<ParseObject> query = ParseQuery.getQuery("Products");
query.orderByDescending("createdAt");
query.whereEqualTo("user", userid);
query.setCachePolicy(ParseQuery.CachePolicy.NETWORK_ELSE_CACHE);
ob = query.find();
for (ParseObject parseObject : ob) { // Locate images in flag column
String title = (String) parseObject.get("title");
String descrip = (String) parseObject.get("descrip");
preco = (String) parseObject.get("preco");
String data = (String) parseObject.get("data");
String detail = (String) parseObject.get("detail");
user = (String) parseObject.get("user");
imagefile = parseObject.getParseFile("foto");
if (imagefile!=null){
imageUrl = parseObject.getParseFile("foto").getUrl();
}
productId = parseObject.getObjectId();
product = new Product(title, descrip, preco, data, detail, user, imagefile, imageUrl, productId);
mProductsList.add(product);
}
} catch (ParseException e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return null;
}
return null;
}
@Override
protected void onPostExecute(Void result) {
// Locate the listview in listview_main.xml
mListView.setAdapter(new MyAdapter());
loadpd = (ProgressBar) getView().findViewById(R.id.loadpduser);
getView();
loadpd.setVisibility(View.INVISIBLE);
}
}
这是在视图寻呼机中保存这些片段的片段
public class MainActivity extends Fragment {
ViewPager viewPager;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_main, null);
viewPager = (ViewPager) view.findViewById(R.id.pager);
viewPager.setAdapter(new MyPageAdapter(getChildFragmentManager()));
((MaterialNavigationDrawer)this.getActivity()).getToolbar().setLogo(R.drawable.bhlogo);
((MaterialNavigationDrawer)this.getActivity()).getToolbar().setTitle("");
((MaterialNavigationDrawer) this.getActivity()).setSupportActionBar(((MaterialNavigationDrawer) this.getActivity()).getToolbar());
((MaterialNavigationDrawer) this.getActivity()).getSupportActionBar();
setHasOptionsMenu(true);
return view;
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_main, menu);
super.onCreateOptionsMenu(menu, inflater);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.refresh:
Intent intent = getActivity().getIntent();
getActivity().finish();
startActivity(intent);
return false;
}
return false;
}
public class MyPageAdapter extends FragmentPagerAdapter{
public MyPageAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
if (position == 0) {
return new NewsFragment();
}
else {
return new UserProductsFragment();
}
}
@Override
public int getCount() {
return 2;
}
@Override
public CharSequence getPageTitle(int position) {
if (position == 0) {
return "Todos os anúncios";
}
else {
return "Meus anúncios";
}
}
}
答案 0 :(得分:0)
Anshul Tyagi 建议的解决方案
&#34;在您的Manifest.xml中添加带有FragmentActivity标记的
android:configChanges="orientation|screenSize"
&#34;
仅适用于纵向和横向两者的布局文件相同的情况,仅表示使用单一布局。如果是这种情况,那么这是最好的解决方案。但是,如果没有,请尝试this,基本上是Headless Fragment
,如 Satyen 指出