実際の回答になる前に、コードをいくつか改善してみたいです。 まず、(あなたが一般的にコンテキストを必要とする場合または)Activityからテントを作成するときにgetBaseContext()
をコールする必要はありませんが、あなただけのthis
を使用することができます。
intent = new Intent(this, NewTestActivity.class);
第二に、アンドロイドは、活動を扱うのが得意ですfinish()
で最初のアクティビティを手動で終了する必要はありません。 Androidは自動的に最初のアクティビティを一時停止または停止し、戻ったときに戻します。
第3に、あなたのケースでは、の代わりにstartActivityForResult()
の代わりに、以下で説明する理由があります。
これはあなたのコードを見て、次のように行います:
は
private static final int MY_REQUEST_CODE = 33487689; //put this at the top of your Activity-class, with any unique value.
intent = new Intent(this, NewTestActivity.class);
startActivityForResult(intent, MY_REQUEST_CODE);
は今、startActivityForResult()
は、活動を開始し、その新しい活動からの結果を待ちます。あなたは新しい活動にfinsih()
を呼び出すと、あなたが今閉じている新しいActivtyから供給されたデータで、最初Activitys onActivityResult()
-methodになってしまいます。このすべてのために
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode != MY_REQUEST_CODE) return; //We got a result from another request, so for this example we can return.
if(resultCode != RESULT_OK) return; //The user aborted the action, so we won't get any data.
//After the above if-statements we know that the activity that gives us a result was requested with the correct request code, and it's action was successful, we can begin extracting the data, which is in the data-intent:
Item item = (Item) data.getSerializableExtra("customData"); //casts the data object to the custom Item-class. This can be any class, as long as it is serializable. There are many other kinds of data that can be put into an intent, but for this example a serializable was used.
itemList.add(item); //This is the list that was specified in onCreate()
//If you use an Adapter, this is the place to call notifyDataSetChanged();
}
が動作するように、我々はいくつかを行う必要があります第二の活動で物事:アイテムが作成されたとき、我々は結果を設定する必要があり :
//We begin by packing our item in an Intent (the Item class is an example that is expected to implement Serializable)
Item theCreatedItem; //This is what was created in the activity
Intent data = new Intent();
data.putSerializable(theCreatedItem);
setResult(RESULT_OK, data);
finish();
前述したように、これは、項目の最初のActivitys onActivityResult()
-methodに返す必要があります。
onResume()メソッドにコードがありますか?通常、後続のアクティビティ呼び出しに対してonResumeが呼び出されます。 – kosa
いいえ、リストをロードするアクションはonCreate()メソッド内にあります。 – AuTi
onResume()にも同じものを追加してみてください。 – kosa