點擊添加按鈕以後-----彈出一個新的activity--------在新的activity將數據輸入保存以後,關閉當前的activity回到以前的activity刷新內容ide
實現:使用onActivityResultthis
啓動頁: btnButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(MainActivity.this, SecondActivity.class); String str=txtEditText.getText().toString(); intent.putExtra("fromMain", str); // startActivity(intent); // 啓動須要監聽返回值的Activity,並設置請求碼:requestCode startActivityForResult(intent, 1); }
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// 當otherActivity中返回數據的時候,會響應此方法
// requestCode和resultCode必須與請求startActivityForResult()和返回setResult()的時候傳入的值一致。
if (requestCode == 1 && resultCode == SecondActivity.RESULT_CODE) {
Bundle bundle=data.getExtras();
String strResult = bundle.getString("result");
Log.i(TAG,"onActivityResult: "+ strResult);
Toast.makeText(MainActivity.this, strResult, Toast.LENGTH_LONG).show();
}
}spa
新頁面關閉時code
btnBack.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { Intent intent = new Intent(); intent.putExtra("result", txtContent.getText().toString()); setResult(RESULT_CODE, intent);// 設置resultCode,onActivityResult()中能獲取到 finish(); } });
兩個activity經過intent來通信blog