简体   繁体   中英

Go to previous activity in non-activity class

I got a problem I made a class for Alert Dialogs now if someone presses Ok it should go back to the previous activity but I don't know how to do this, because when i put in finsih(); it gives me an error here is my code:

package com.laurenswuyts.find.it;

import com.laurenswuyts.find.it.R;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;



public class AlertDialogManager {
    /**
     * Function to display simple Alert Dialog
     * @param context - application context
     * @param title - alert dialog title
     * @param message - alert message
     * @param status - success/failure (used to set icon)
     *               - pass null if you don't want icon
     * */
    @SuppressWarnings("deprecation")
    public void showAlertDialog(Context context, String title, String message,
            Boolean status) {
        AlertDialog alertDialog = new AlertDialog.Builder(context).create();

        // Setting Dialog Title
        alertDialog.setTitle(title);

        // Setting Dialog Message
        alertDialog.setMessage(message);

        if(status != null)
            // Setting alert dialog icon
            alertDialog.setIcon((status) ? R.drawable.success : R.drawable.fail);

        // Setting OK Button
        alertDialog.setButton("OK", new DialogInterface.OnClickListener() {

            public void onClick(final DialogInterface dialog, final int which) {

            }





        });

        // Showing Alert Message
        alertDialog.show();
    }
}

In the public void Onclick i tried to type finish(); but that didnt work.

Can anyone help me? Thanks in advance!

Regards,

You should add a property to your manager;

Context context;

Init it on your showAlertDialog() method.

Under your click;

((Activity) context).finish();

You could pass the click listener in from the calling activity:

DialogInterface.OnClickListener clickListener = new DialogInterface.OnClickListener() {
        public void onClick(final DialogInterface dialog, final int which) {
            finish();
        }
    });
AlertDialogManager manager = new AlertDialogManager();
manager.showAlertDialog(this, title, message, status, clickListener);

Then in your AlertDialogManager , you change the method like this:

public void showAlertDialog(Context context, String title, String message,
        Boolean status, DialogInterface.OnClickListener clickListener) {
    AlertDialog alertDialog = new AlertDialog.Builder(context).create();

    // Setting Dialog Title
    alertDialog.setTitle(title);

    // Setting Dialog Message
    alertDialog.setMessage(message);

    if(status != null)
        // Setting alert dialog icon
        alertDialog.setIcon((status) ? R.drawable.success : R.drawable.fail);

    // Setting OK Button
    alertDialog.setButton("OK", clickListener);

    // Showing Alert Message
    alertDialog.show();
}

This way the click behaviour is handled by the calling class, and the AlertDialogManager doesn't have any knowledge of what happens after OK is clicked.

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