返回时活动缺少数据

时间:2013-08-06 15:08:27

标签: android

所以我的主要活动包含一个列表视图,并有一个地图,其中包含列表的所有数据。单击某个项目后,您将进入详细信息显示。当我按下后退按钮从详细活动返回主活动时,如果我设置了一个断点,我的地图键仍然完好无损,但是对象中的所有字符串都是“”,而整数是-1。这是我的主要活动:

public class MainActivity extends Activity {
    private Map<String, Stunt> stunts = new LinkedHashMap<String, Stunt>();
    private StuntsDao stuntsDao;
    private ListAdapter listAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        stuntsDao = new StuntsDao(getApplicationContext());     

        stunts = stuntsDao.getAllStunts();

        listAdapter = new ListAdapter(this, R.layout.list_layout, new ArrayList<Stunt>(stunts.values()));
        ListView listView = (ListView)findViewById(R.id.listView);

        listView.setAdapter(listAdapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                String stuntName = (String)((ViewHolder)view.getTag()).stuntName.getText();
                Intent myIntent = new Intent(getApplicationContext(), StuntDetails.class);
                getStuntDetailsIfNeeded(stuntName);
                Stunt stunt = stunts.get(stuntName);
                myIntent.putExtra("STUNT", stunt);
                startActivity(myIntent);
            }
        });
    }
...

}

为什么地图中的对象基本上是空的?

1 个答案:

答案 0 :(得分:1)

单击后退时不会调用

onCreate()。您需要在onStart或onResume上再次初始化数据。请参阅:http://developer.android.com/training/basics/activity-lifecycle/starting.html