等待对话框单击以重新启动 activity

Wait for dialog click to restart an activity

大家好,在此先感谢您的帮助。

我有两个活动。我将 activity 中的对话从 activity 中调用为两个。 问题是,如果按下肯定按钮,我希望对话框重新启动 activity,但我不知道该怎么做。

这是我的代码...

Activity一个:

    @Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings)
    {
        Funciones Funciones1 = new Funciones();

        Funciones1.MuestraDialogo(CluefichaActivity.this);

        return true;
    }

    return super.onOptionsItemSelected(item);
}

Activity二:

public class Funciones extends Activity

{ private static final int DIALOGO = 1;

private AlertDialog.Builder ventana;

Activity miActividad;





@Override
protected Dialog onCreateDialog(int id)
{
    switch (id)
    {
        case DIALOGO:
            ventana = new AlertDialog.Builder(miActividad);

            ventana.setIcon(miActividad.getResources().getIdentifier("my_icon", "drawable", miActividad.getPackageName()));
            ventana.setTitle("title");
            ventana.setMessage("message");

            ventana.setPositiveButton("OK", new DialogInterface.OnClickListener()
                {
                    public void onClick(DialogInterface dialog, int boton)
                    {
                            Intent intent = miActividad.getIntent();
                            miActividad.finish();
                            startActivity(intent);
                    }
                });
            }

            ventana.setNegativeButton("CANCEL", new DialogInterface.OnClickListener()
            {
                public void onClick(DialogInterface dialog, int boton)
                {
                }
            });

            return ventana.create();
    }

    return null;
}

... ...

public void MuestraDialogo (Activity actividad)
{
    miActividad = actividad;

    showDialog(DIALOGO);
}

当我 运行 它...

我得到这个错误
Process: com.myproject.projectname, PID: 1841
java.lang.NullPointerException
        at android.app.Activity.startActivityForResult(Activity.java:3511)
        at android.app.Activity.startActivityForResult(Activity.java:3472)
        at android.app.Activity.startActivity(Activity.java:3714)
        at android.app.Activity.startActivity(Activity.java:3682)
        at com.myproject.projectname.Funciones.onClick(Funciones.java:130)
        at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:167)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:157)
        at android.app.ActivityThread.main(ActivityThread.java:5356)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
        at dalvik.system.NativeStart.main(Native Method)

其中 "Funciones.java:130" 是这行代码:"startActivity(intent);"

有人可以帮忙吗?谢谢!

您需要从您的子 Activity 对话框向主 activity 发送一个广播 (LocalBroadcast) 并处理该广播以发起终止:

在你的主 Activity 中说 MainActividad

static final String BROADCAST_ACTIVITY_CLOSE = "com.example.ACTIVITY_CLOSE_BROADCAST";

private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        if(intent.getAction().equals(BROADCAST_ACTIVITY_CLOSE)) {
            terminateCleanly();
            finish();
        }
    }
};

然后在 onCreate:

    LocalBroadcastManager broadcastManager = LocalBroadcastManager.getInstance(this);
    IntentFilter intentFilter = new IntentFilter();
    intentFilter.addAction(BROADCAST_ACTIVITY_CLOSE);
    broadcastManager.registerReceiver(broadcastReceiver, intentFilter);

节省更新您的清单,我认为更干净。

你的对话框调用这个:

                Intent RTReturn = new Intent(MainActividad.BROADCAST_ACTIVITY_CLOSE);
                LocalBroadcastManager.getInstance(this).sendBroadcast(RTReturn);

您遇到的问题是,一旦您调用 finish,您的旧 activity 的意图就为空。虽然约翰的回答是一种更简洁的终止方式,但您遇到的问题是再次启动 activity 。

尝试替换

Intent intent = miActividad.getIntent();
startActivity(intent);

Intent newIntent = new Intent(Funciones.this, CluefichaActivity.class);
startActivity(newIntent);