There is such a fragment:

public class FotosFragment extends Fragment { private final String LOG_TAG = "FotosFragment"; private static final int TAKE_PICTURE = 1; private static final String FOTO_ABSOLUT_PATH = "foto_absolut_path"; private static final String FOTO_TYPE = "foto_type"; private static final String FOTO_NUMBER = "foto_number"; // типы файлов public static final int FOTO_FILE = 0; public static final int FOTO_ADD = 1; public static final int FOTO_NO = 2; private String mFotoAbsolutPath; private int mFotoType = 0; private int mFotoNumber = 0; private static Uri mFotoTempUri; // static - чтобы ссылка на фото не терялась при повороте экрана private ImageView ivFoto; private ProgressBar pbLoadFoto; private View mView; private ViewGroup mViewGroup; private LayoutInflater mInflater; private OnFotoListener mListener; private Context mContext; private LoadFotoTask mLoadFotoTask; public FotosFragment() {} public static FotosFragment newInstance(String fotoAbsolutPath, int fotoType, int fotoNumber) { FotosFragment fragment = new FotosFragment(); Bundle args = new Bundle(); args.putString(FOTO_ABSOLUT_PATH, fotoAbsolutPath); args.putInt(FOTO_TYPE, fotoType); args.putInt(FOTO_NUMBER, fotoNumber); fragment.setArguments(args); return fragment; } private Bitmap getBitmapSmall(File file){ DisplayMetrics metrics = new DisplayMetrics(); getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics); if (file.exists()) { Bitmap bmOriginal = BitmapFactory.decodeFile(file.getAbsolutePath()); // Вычисляем ширину и высоту изображения int width = bmOriginal.getWidth(); int height = bmOriginal.getHeight(); float ratioWidth = width / metrics.widthPixels; float ratioHeight = height / metrics.heightPixels; float ratio = ratioHeight < ratioWidth?ratioHeight:ratioWidth; int halfWidth = (int) (width / ratio); int halfHeight = (int) (height / ratio); Bitmap bmHalf = Bitmap.createScaledBitmap(bmOriginal, halfWidth, halfHeight, false); bmOriginal.recycle(); return bmHalf; } return null; } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (getArguments() != null) { mFotoAbsolutPath = getArguments().getString(FOTO_ABSOLUT_PATH); mFotoType = getArguments().getInt(FOTO_TYPE); mFotoNumber = getArguments().getInt(FOTO_NUMBER); } } View.OnClickListener mOnClickListener = new View.OnClickListener() { @Override public void onClick(View view) { if (mFotoType == FOTO_ADD) { if (mListener != null) { ThingContent thingContent = ThingContent.get(mContext); Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); File file = thingContent.getFotoDir(); if (!file.exists()) file.mkdirs(); file = new File(file.getAbsolutePath(), "IMG_" + Thing .dateToString(Calendar.getInstance().getTime(), Thing.DATE_FORMAT_FILE) + ".jpg"); mFotoTempUri = Uri.fromFile(file); cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, mFotoTempUri); startActivityForResult(cameraIntent, TAKE_PICTURE); } } else if (mFotoType == FOTO_FILE){ startActivity(ViewFotoActivity.newIntent(mContext, mFotoAbsolutPath)); } } }; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { mViewGroup = container; mInflater = inflater; mView = updateUI(); return mView; } private View updateUI(){ View view; switch (mFotoType) { case FOTO_FILE: { view = mInflater.inflate(R.layout.fragment_fotos, null); ivFoto = (ImageView) view.findViewById(R.id.ivFoto); pbLoadFoto = (ProgressBar) view.findViewById(R.id.pbLoadFoto); mLoadFotoTask = new LoadFotoTask(); mLoadFotoTask.execute(mFotoAbsolutPath); break; } case FOTO_ADD: { view = mInflater.inflate(R.layout.fragment_fotos_add, null); break; } case FOTO_NO: { view = mInflater.inflate(R.layout.fragment_fotos_add, null); break; } default: { view = mInflater.inflate(R.layout.fragment_fotos_add, null); } } ivFoto = (ImageView) view.findViewById(R.id.ivFoto); view.setOnClickListener(mOnClickListener); return view; } @Override public void onAttach(Context context) { super.onAttach(context); Log.d(LOG_TAG, "onAttach " + this.toString()); mContext = context; if (context instanceof OnFotoListener) mListener = (OnFotoListener) context; } @Override public void onDetach() { super.onDetach(); Log.d(LOG_TAG, "onDetach " + this.toString()); mListener = null; if (mLoadFotoTask != null) { mLoadFotoTask.cancel(true); Log.d(LOG_TAG, "Отмена потока"); } } @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { Log.d(LOG_TAG, "onActivityResult " + this.toString()); if ((requestCode == TAKE_PICTURE) && (resultCode == Activity.RESULT_OK)) { mFotoType = FOTO_FILE; mFotoAbsolutPath = mFotoTempUri.getEncodedPath(); int index; if (mViewGroup != null) { if (mView != null) { index = mViewGroup.indexOfChild(mView); mViewGroup.removeViewInLayout(mView); mView = updateUI(); mViewGroup.addView(mView, index); } } mListener.onClickFoto(mFotoAbsolutPath, mFotoNumber); } else mFotoTempUri = null; } public interface OnFotoListener { void onClickFoto(String fotoAbsolutPath, int position); } private class LoadFotoTask extends AsyncTask<String, Void, Void>{ private Bitmap mFotoBitmap; @Override protected void onPreExecute() { pbLoadFoto.setVisibility(View.VISIBLE); super.onPreExecute(); } @Override protected void onPostExecute(Void aVoid) { try { pbLoadFoto.setVisibility(View.GONE); Animation anim = AnimationUtils.loadAnimation(mContext, R.anim.alpha_foto); ivFoto.startAnimation(anim); ivFoto.setImageBitmap(mFotoBitmap); } catch (Resources.NotFoundException e) { e.printStackTrace(); } super.onPostExecute(aVoid); } @Override protected Void doInBackground(String... strings) { File fileFoto = new File(strings[0]); if (fileFoto.exists()) mFotoBitmap = getBitmapSmall(fileFoto); return null; } } } 

In the onActivityResult method onActivityResult try to change the view , it does not issue errors, but displays a white screen. When a view is created in onCreateView , then everything is fine.

PS This snippet is used for viewPager pages viewPager

Here is my adapter:

 private class AdapterPagerFoto extends FragmentStatePagerAdapter{ private List<String> mListFoto; private boolean mIsEdit; // listFoto - список полных путей к файлам фото // isEdit - true - режим редактирование и добавления // false - режим просмотра public AdapterPagerFoto(FragmentManager fm, List<String> listFoto, boolean isEdit) { super(fm); mListFoto = listFoto; mIsEdit = isEdit; } @Override public Fragment getItem(int position) { Fragment fragment; if (mIsEdit){ if ((getCount()-1) == position){ fragment = FotosFragment.newInstance("", FotosFragment.FOTO_ADD, position); } else { fragment = FotosFragment.newInstance(mListFoto.get(position), FotosFragment.FOTO_FILE, position); } } else { if (mListFoto.size() == 0){ fragment = FotosFragment.newInstance("", FotosFragment.FOTO_NO, position); } else { fragment = FotosFragment.newInstance(mListFoto.get(position), FotosFragment.FOTO_FILE, position); } } return fragment; } @Override public int getCount() { int result = 1; if (mIsEdit){ result = mListFoto.size() + 1; }else { if (mListFoto.size() > 0) result = mListFoto.size(); } return result; } @Override public int getItemPosition(Object object) { return POSITION_NONE; } 
  • you are trying to do something very, very strange. Instead use just a different fragment, and not try to replace the View of this fragment. - Vladyslav Matviienko
  • Yes, everything seems to be working and the fragments are replaced. Only now, when it switches to a new activation, to take a photo, and turn the screen on a new activation, then when you switch back, the fragment is not replaced with a new one. If the screen does not turn over, then everything is fine. And when you don’t switch to a new activation, you just turn the device over, also the rules. - Frozik6k
  • And a new element is added to the mListFoto , and the call to the notifyDataSetChanged method occurs. - Frozik6k
  • Well, or maybe there is a way to restart the fragment from the inside. - Frozik6k

1 answer 1

After much torment, I did not figure out how to change the view. Problem solved crutch. I started the notifyDataSetChanged() method after 300 milliseconds, and it all worked.

 mHandler = new WeakHandler(); mHandler.postDelayed(new Runnable() { @Override public void run() { mPagerAdapter.notifyDataSetChanged(); mPagerFoto.setCurrentItem(positionFoto); } }, 300);