简体   繁体   中英

how to start activity's alert dialog from another activity

In one activity i use AlertDialog.Builder with LayoutInflater to take inputs from users. I show this dialog on a button click in this activity. Here is code:

buttonPlus.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        LayoutInflater li = LayoutInflater.from(InputDetail.this);
        View promptsView = li.inflate(R.layout.prompt_dialog, null);

        AlertDialog.Builder builder = new AlertDialog.Builder(InputDetail.this);

        builder.setTitle(Html.fromHtml("<font color='"+getResources().getColor(R.color.buttonColor)+"'>"+getResources().getString(R.string.addToAmount)+"</font>"));
        builder.setView(promptsView);
        builder.show();
    }
});

And I have a widget and a button on widget. With this button I start InputDetail activity:

Intent intentToInputDetails = new Intent(context, InputDetail.class);
PendingIntent pendingIntentToInputDetails = PendingIntent.getActivity(context, 0, intentToInputDetails, 0);

RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.inputs_widget);
views.setOnClickPendingIntent(R.id.button, pendingIntentToInputDetails);

So, my question is, after I start InputActivity how can I show the AlertDialog without waiting for the user to click a button?

Thanks in advance.

You can add data to your intent and then check that data in your newly started activity.

So for example:

Intent intentToInputDetails = new Intent(context, InputDetail.class);
// Also add extra data
intentToInputDetails.putExtra("shouldShowDialog", true);

PendingIntent pendingIntentToInputDetails = PendingIntent.getActivity(context, 0, intentToInputDetails, 0);

RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.inputs_widget);
views.setOnClickPendingIntent(R.id.button, pendingIntentToInputDetails)

And in your InputDetailActivity (for example in the onCreate()):

// The second argument is the default value which means that if the argument was not added to the intent, it will be false
boolean shouldShowDialog = getIntent().getBooleanExtra("shouldShowDialog", false);
if (shouldShowDialog)
{
    // Open dialog..
}

In your InputDetail Activity create a method that shows a dialogue as,

public void showDialogue(View v) {
    LayoutInflater li = LayoutInflater.from(InputDetail.this);
    View promptsView = li.inflate(R.layout.prompt_dialog, null);

    AlertDialog.Builder builder = new AlertDialog.Builder(InputDetail.this);

    builder.setTitle(Html.fromHtml("<font color='"+getResources().getColor(R.color.buttonColor)+"'>"+getResources().getString(R.string.addToAmount)+"</font>"));
    builder.setView(promptsView);
    builder.show();
}

Now call this method in OnClickListener of your buttonPlus as,

buttonPlus.setOnClickListener(new View.OnClickListener() {
 @Override
 public void onClick(View v) {
 showDialogue();
 }
});

And when this activity is called you can call this method in OnCreate() after getting the intent as @Hasslam...

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_add_new_goal);
    boolean shouldShowDialog = getIntent().getBooleanExtra("shouldShowDialog", false);
    if (shouldShowDialog)
    {
       showDialogue();
    }
}

But when you start this activity from the widget you have to put this boolean value in the intent object as...

Intent intentToInputDetails = new Intent(context, InputDetail.class);
intentToInputDetails.putExtra("shouldShowDialog", true);
PendingIntent pendingIntentToInputDetails = PendingIntent.getActivity(context, 0, intentToInputDetails, 0);
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.inputs_widget);
views.setOnClickPendingIntent(R.id.button, pendingIntentToInputDetails)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM