1

我有一个活动和第二个活动。在第二个活动中,我需要关闭该活动,然后再次在第一个活动中向用户显示警报。

这是我用来显示警报和关闭活动的当前代码:

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();
    }
});

那么我怎样才能使第二个活动finishes,然后回到第一个活动,向用户显示警报?

我也试过这个:

String message = "message";

finish();

AlertDialog alertDialog = new AlertDialog.Builder(FirstActivity.class).create();
alertDialog.setMessage(message);
alertDialog.show();

但我收到此错误:

The constructor AlertDialog.Builder(Class<FirstActivity>) is undefined

4

2 回答 2

1

您需要使用startActivityForResult()方法来启动第二个活动。然后,一旦第二个活动关闭,onActivityResult()第一个活动的方法就会被调用。在那里您可以打开一个对话框。这是代码示例。

class SecondActivity extends Actvitiy {

    // for instance you close your activity here
    public void onClick(View view) {
        // you have an int to get back
        int resultValue = ...;

        // store your variables into resulting intent
        Intent data = new Intent();
        data.putExtra(FirstActivity.EXT_RESULT, resultValue);

        // store result with data
        setResult(RESULT_OK, data);

        // finish activity
        finish();
    }
}

class FirstActivity extends Activity {

    public static final String EXT_RESULT = "result-value";
    public static final int REQ_SECOND_ACTIVITY = 100;

    // call this method to launch second activity
    private void launchSecondActicity() {
        startActivityForResult(
            new Intent(this, SecondActivity.class), REQ_SECOND_ACTIVITY);
    }

    @Override
    public onActivityResult(int requestCode, int resultCode, Intent data) {
        // this gets called when second activity is closed
        if (resultCode == RESULT_OK && requestCode == REQ_SECOND_ACTIVITY) {

            // read your value
            int resultValue = data.getIntExtra(EXT_RESULT, 0);

            // open your dialog here
            ...
        }
    }
}
于 2013-08-30T06:52:58.237 回答
0

我需要使用该方法从第一个活动开始第二个活动,startActivityForResult然后result将.first activitysecond 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();

第一个活动:

把它放在顶部first activity class

public static final int REQ_SECOND_ACTIVITY = 100;

这是second activity从以下开始的方法first activity

startActivityForResult(new Intent(FirstActivity.this, SecondActivity.class), REQ_SECOND_ACTIVITY);

最后,将此方法添加到您的first activity class

// 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();        
    }
}

这个方法是由用户在这个问题上结合起来的,然后在那个问题上滚动

于 2013-08-31T03:45:10.940 回答