片段更改时会丢失加载的图像

时间:2018-04-08 15:56:54

标签: android

我创建了一个简单的应用程序,几个片段设置回家并共享。在家庭活动中,我曾经从不同活动的网址加载图片。图像正在成功加载,但如果将片段更改为设置或共享,则返回到主页,加载的图像将丢失,编辑文本将消失。下面是家庭碎片代码

public class HomePage extends Fragment {
    public static Button btn;
    public static EditText userinput;
    public static ImageView wall;
    public static ProgressBar pb;

    public HomePage() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragmet

        View view= inflater.inflate(R.layout.fragment_home_page, container, false);
        userinput= (EditText) view.findViewById(R.id.editText); 
        wall= (ImageView) view.findViewById(R.id.imageview);
        pb= (ProgressBar) view.findViewById(R.id.progressBar);
        pb.setVisibility(View.GONE);
        btn = (Button) view.findViewById(R.id.button);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SyncImage process= new SyncImage();
                process.execute();
            }
        });
        return view;
    }

} 

这是SyncImage活动代码

public class SyncImage extends AsyncTask<Void,Void,Void> {
    private String piclink;
    @Override
    protected Void doInBackground(Void... voids) {
        try {
            URL idurl = new URL("mylink.com"+HomePage.userinput.getText());
            HttpURLConnection idConnection= (HttpURLConnection) idurl.openConnection();
            InputStream idStream= idConnection.getInputStream();
            BufferedReader idReader=new BufferedReader(new InputStreamReader(idStream));
            String line="";
            if (line !=null){
                line=idReader.readLine();
                JSONObject file= new JSONObject(line);
                piclink=file.getString("image");
            }
        }
        catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        HomePage.wall.setVisibility(View.GONE);
        HomePage.pb.setVisibility(View.VISIBLE);
    }

    @Override
    protected void onPostExecute(Void aVoid) {
        super.onPostExecute(aVoid);
        Picasso.get().load(this.piclink).into(HomePage.wall);
        HomePage.pb.setVisibility(View.GONE);
        HomePage.wall.setVisibility(View.VISIBLE);
    }
} 

2 个答案:

答案 0 :(得分:0)

据我了解,您正在将图片加载到ImageView wall片段中的HomePage,并在将片段切换到另一张片段后松开此图像,是吗?如果是这样,您需要在Activity中继续引用您的图片,并在图片返回时将图片加载到HomeFragment

P.S。你可以这样做:

  • AsyncTask开始您的SyncImage MainActivity

  • 等待piclink

  • 收到piclink后,在HomeFragment中显示MainActivity代码:

    getFragmentManager().beginTransaction().replace(R.id.fragment, HomePage.newInstance(piclink)).commit(); //or getSupportFragmentManager()
    

其中HomePage片段应包含此代码:

    public class HomePage extends Fragment {
        //Other your stuff...

        private static final String PICLINK = "PICLINK";
        private String piclink;

        public static HomePage newInstance(String piclink) {
            HomePage fragment = new HomePage();
            Bundle args = new Bundle();
            args.putString(PICLINK, piclink);
            fragment.setArguments(args);
            return fragment;
        }

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            if (getArguments() != null)
                piclink = getArguments().getString(PICLINK);
        }

        @Override
        public void onActivityCreated(Bundle savedInstanceState) {//To restore piclink after screen rotation
            super.onActivityCreated(savedInstanceState);
            if (savedInstanceState != null)
                piclink = savedInstanceState.getString(PICLINK);
        }

        @Override
        public void onSaveInstanceState(Bundle outState) {//To save piclink on screen rotation
            super.onSaveInstanceState(outState);
            outState.putString(PICLINK, piclink);
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.fragment_home_page, container, false);
            //Other views initialization...
            ImageView wall = (ImageView) view.findViewById(R.id.imageview);
            if (piclink != null)
                Picasso.get().load(piclink).into(wall);
            return rootView;
        }
    }

答案 1 :(得分:0)

  1. 永远不要有静态视图。
  2. 如果有方向状态更改,您可以将图像网址保存在onSaveInstanceState
  3. 保留对活动/片段的弱引用。
  4. 使用async loader