0

我的应用程序 android 有问题。我是新手,我正在学习。我有 2 个活动,首先调用第二个活动,并在此活动中添加字符串,我想在第一个活动中看到这个新字符串,我尝试使用此代码

protected void onStart() {
    Bundle bundle = getIntent().getExtras();
    paises.add(bundle.getString("Pais"));
    Log.i("pais", bundle.getString("Pais"));
    habitantes.add(bundle.getString("Habitantes"));

    super.onStart();
}

我尝试使用 OnCreate、OnResume 并关闭我的应用程序,有人可以帮助我吗?

在第二个活动中,我输入了这段代码

Intent i = new Intent(this, MainActivity.class);
i.putExtra("Pais", pa);
i.putExtra("Habitantes", ha);

编辑:是一个简单的应用程序,在第一个活动中我有一个arrayList,我想在另一个活动中添加一个字符串到这个arrayLista,但是当我转向第一个活动时,arraylist 不更新:S

4

3 回答 3

1

尝试这个:

Intent i = new Intent(this, MainActivity.class);
Bundle args = new Bundle();
args.putString("Pais", pa);
args.putString("Habitantes", ha);
i.putExtras(args);
于 2014-10-25T10:58:47.863 回答
1

你必须先打电话super.onStart()

void onStart(Bundle savedState) {
   super.onStart(savedState);
   // your code here .....
}

onResume和_onCreate

于 2014-10-25T11:00:31.473 回答
0

它有效,ty Alex 和 Bene,正确的形式是与你的混合 ^^

第一个活动 onCreate

 if (getIntent().getExtras() != null){
        Bundle bundle = getIntent().getExtras();
        paises.add(bundle.getString("Pais"));
        habitantes.add(bundle.getString("Habitantes"));
    }

第二个活动

Intent i = new Intent(this, MainActivity.class);
Bundle args = new Bundle();
args.putString("Pais", pa);
args.putString("Habitantes", ha);
i.putExtras(args);
startActivity(i)
于 2014-10-25T12:21:51.217 回答