我有一个活动,第二个活动。在第二个活动,我需要关闭的活动和的然后的第一活动再次显示警报给用户返回。
I have an activity and a second activity. In the second activity, I need to close the activity and then display an alert to the user back in the first activity again.
下面是当前code我使用弹出警报,并关闭活动:
Here is the current code I use to bring up the alert and close the activity:
String message = "message"; AlertDialog alertDialog = new AlertDialog.Builder(SecondActivity.this).create(); alertDialog.setMessage(message); alertDialog.show(); alertDialog.setOnDismissListener(new OnDismissListener() { public void onDismiss(final DialogInterface dialog) { finish(); } });
所以,我怎样才能让这个第二活性完成,再一次回到了第一个活动,显示一个警告用户?
So how can I make it so that the second activity finishes, and then once back on the first activity, display an alert to the user?
我想这个问题,以及:
String message = "message"; finish(); AlertDialog alertDialog = new AlertDialog.Builder(FirstActivity.class).create(); alertDialog.setMessage(message); alertDialog.show();
但我得到这个错误:
but I get this error:
构造AlertDialog.Builder(类< FirstActivity>)的未定义
推荐答案我需要使用 startActivityForResult 方法来启动从第一个第二个活动,然后通过在结果到第一个活动从次活动。
I needed to start the second activity from the first one by using the startActivityForResult method, and then pass the result to the first activity from the second activity.
Intent data = new Intent(); data.putExtra("reference for the message", message); // give the intent the String value to be passed to other activity setResult(Activity.RESULT_OK, data); finish();
在顶部将这个第一个活动课:
public static final int REQ_SECOND_ACTIVITY = 100;这是如何启动次活动从第一个活动:
startActivityForResult(new Intent(FirstActivity.this, SecondActivity.class), REQ_SECOND_ACTIVITY);
最后,此方法添加到您的第一个活动课:
// this method gets called when the second activity is closed @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (resultCode == RESULT_OK && requestCode == REQ_SECOND_ACTIVITY) { String message = data.getStringExtra("reference for the message"); AlertDialog alertDialog = new AlertDialog.Builder(FirstActivity.this).create(); alertDialog.setMessage(message); alertDialog.show(); } }这个方法放在一起组合从用户 beworker 在这个问题上和的letroll~~MD~~aux 的过那个question.
This method was put together in combination from users beworker on this question and letroll over at that question.
更多推荐
如何显示警报不同的活动
发布评论