1

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.

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?

I tried this as well:

String message = "message";

finish();

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

but I get this error:

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

Michael Yaworski
  • 13,410
  • 19
  • 69
  • 97

2 Answers2

1

You need to use startActivityForResult() method to launch second activity. Then, once second activity is closed, onActivityResult() method of the first activity gets called. There you can open a dialog. Here is code example.

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
            ...
        }
    }
}
sergej shafarenka
  • 20,071
  • 7
  • 67
  • 86
  • this is just a request id which you receive back when `onActivityResult()` is called. I updated example that is shows how request id and extras are used. – sergej shafarenka Aug 30 '13 at 12:22
0

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.


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

First Activity:

Put this at the top of first activity class:

public static final int REQ_SECOND_ACTIVITY = 100;

This is how to start the second activity from the first activity:

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

Finally, add this method to your 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();        
    }
}

This method was put together in combination from users beworker on this question and letroll over at that question.

Community
  • 1
  • 1
Michael Yaworski
  • 13,410
  • 19
  • 69
  • 97