关于向后移动意图的问题

时间:2016-08-18 02:01:52

标签: java android

我是Android的初学者,所以我坚持使用intent方法。 我创建了三个java类,MainActivity给DetailActivity一个意图,在DetailActivity中,textView处理来自MainActivity的数据。 DetailActivity给出了TheAnswer的意图(所有这些都有片段)。问题是,当我点击TheAnswer左上角的后退图标时。 DetailActivity中的textView 为空。 那么当我从TheAnswer回来时,我该怎样做才能确保DetailActivity中textView的内容得到很好的保留?

以下内容是我写的代码。

MainActivityFragment

查看rootView = inflater.inflate(R.layout.fragment_main,container,false);

    inputSearch = (EditText) rootView.findViewById(R.id.inputSearch);
    ListView listView = (ListView) rootView.findViewById(R.id.list_questions);
    listView.setAdapter(mQuest);

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {

            String element = mQuest.getItem(position).substring(0,3);
            int pointer = 0;
            for(int i=0;i<3;i++)
                pointer = pointer * 10 + (int)(element.charAt(i)-'0');
            String data = "";
            StringBuffer sbuffer = new StringBuffer();
            InputStream is = getResources().openRawResource(
                    index[pointer-1]);
            BufferedReader reader = new BufferedReader(new InputStreamReader(is));
            if (is != null) {
                try {
                    while ((data = reader.readLine()) != null) {
                        sbuffer.append(data + "\n");
                    }
                    is.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }


            Intent intent = new Intent(getActivity(), DetailActivity.class)
                    .putExtra(Intent.EXTRA_TEXT,sbuffer.toString());
            startActivity(intent);

        }
    });

DetailActivityFragment(我遇到麻烦的课程)

公共类DetailActivityFragment扩展了Fragment {

private String forecastStr;
TextView text;

public DetailActivityFragment() {
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_detail, container, false);

    // The detail Activity called via intent.  Inspect the intent for forecast data.
    Intent intent = getActivity().getIntent();
    if (intent != null && intent.hasExtra(Intent.EXTRA_TEXT)) {
        forecastStr = intent.getStringExtra(Intent.EXTRA_TEXT);
        text = (TextView) rootView.findViewById(R.id.detail_text);
        text.setMovementMethod(new ScrollingMovementMethod());
        text.setText(forecastStr);
    }

    Button mButton = (Button) rootView.findViewById(R.id.bottle);
    mButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // here you set what you want to do when user clicks your button,
            // e.g. launch a new activity
            Intent intent = new Intent(getActivity(), TheAnswer.class);
            intent.putExtra(Intent.EXTRA_TEXT, forecastStr);
            startActivity(intent);
        }
    });

    return rootView;
}

}

最后,TheAnswerFragment

公共类TheAnswerFragment扩展了Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_the_answer, container, false);
    Intent intent = getActivity().getIntent();

    if (intent != null && intent.hasExtra(Intent.EXTRA_TEXT)) {
        String message = intent.getStringExtra(Intent.EXTRA_TEXT);

        ImageView image = (ImageView) rootView.findViewById(R.id.image);
        image.setImageResource(R.drawable.aa);
    }

    return rootView;
}

}

1 个答案:

答案 0 :(得分:0)

关于存储和恢复实例状态的全部内容。

在这里查看Android文档:

https://developer.android.com/training/basics/activity-lifecycle/recreating.html

如果你想让它更容易/更清洁,你可以选择使用这个

https://github.com/frankiesardo/icepick

P.S。片段需要注意,并确保你看一下静态Fragment createInstance()模式,这样你就可以将创建参数存储在附加到片段参数的包中。