如何显示警报不同的活动

编程入门 行业动态 更新时间:2024-10-24 06:39:07
本文介绍了如何显示警报不同的活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个活动,第二个活动。在第二个活动,我需要关闭的活动和的然后的第一活动再次显示警报给用户返回。

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.

更多推荐

如何显示警报不同的活动

本文发布于:2023-11-27 03:57:14,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1636482.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:警报

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!