0

我有一个活动 1,它将意图发送到活动 2,如下所示:

活动一

periodDatetv.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                Intent i = new Intent(Settings.this, SettingsPeriodDate.class);
                startActivityForResult(i, 100);
            }
        });

活动二

@Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        todayCalendar = new GregorianCalendar();
        pickedDateCalendar = new GregorianCalendar();
        final Dialog dialog = new Dialog(SettingsPeriodDate.this);
        dialog.setContentView(R.layout.perioddatesettings);

        datePicker = (DatePicker) dialog.findViewById(R.id.DataPickerDate);

        dialog.show();

        Button okDialogButton = (Button) dialog
                .findViewById(R.id.dialogButtonOK);

        // if button is clicked, close the custom dialog
        okDialogButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

                Calendar c = Calendar.getInstance();
                int currentYear = c.get(Calendar.YEAR);
                int currentMonth = c.get(Calendar.MONTH) + 1;
                int currentDay = c.get(Calendar.DAY_OF_MONTH);

                Day = datePicker.getDayOfMonth();
                Month = datePicker.getMonth() + 1;
                Year = datePicker.getYear();

                todayCalendar.set(currentYear, currentMonth, currentDay);
                pickedDateCalendar.set(Year, Month, Day);

                System.err.println("Choseon date = " + Day + "/" + Month + "/"
                        + Year);
                System.err.println("Today date = " + currentDay + "/"
                        + currentMonth + "/" + currentYear);

                int Days = daysBetween(todayCalendar.getTime(),
                        pickedDateCalendar.getTime());

                System.err.println("Daaaaays === " + Days);

                if (pickedDateCalendar.after(todayCalendar) || Days >= 1) {
                    System.err.println("Inside if pop it");
                    PopIt("Error ", "Please check the date again");

                }

                else {

                    periodDateSharedPreferences(Year, Month, Day);

                }
                dialog.dismiss();
                System.err.println("befor back");
                Intent saved2 = new Intent(SettingsPeriodDate.this,
                        Settings.class);
                startActivityForResult(saved2, 100);


            }

        });

        Button cancelDialogButton = (Button) dialog
                .findViewById(R.id.dialogButtonCancel);
        // if button is clicked, close the custom dialog
        cancelDialogButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                Intent i = new Intent(SettingsPeriodDate.this,Settings.class);
                startActivity(i);
            }
        });

    }

    public int daysBetween(Date d1, Date d2) {
        return (int) ((d2.getTime() - d1.getTime()) / (1000 * 60 * 60 * 24));
    }

    public void periodDateSharedPreferences(int calculatedPeriodYear,
            int calculatedPeriodMonth, int calculatedPeriodDay) {
        SharedPreferences periodDatePreferences = PreferenceManager
                .getDefaultSharedPreferences(this);
        SharedPreferences.Editor editor = periodDatePreferences.edit();
        editor.putInt("periodChosenDay", calculatedPeriodDay);
        editor.putInt("periodChosenMonth", calculatedPeriodMonth);
        editor.putInt("periodChosenYear", calculatedPeriodYear);
        editor.commit();

        System.err.println("periodChosenDay preferences"
                + periodDatePreferences.getInt("periodChosenDay", 0));
        System.err.println("periodChosenMonth preferences"
                + periodDatePreferences.getInt("periodChosenMonth", 0));
        System.err.println("periodChosenYear preferences"
                + periodDatePreferences.getInt("periodChosenYear", 0));
        Toast.makeText(SettingsPeriodDate.this, "The date was saved",
                Toast.LENGTH_LONG).show();

        finish();
    }

    public void PopIt(String title, String message) {

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Error");
        builder.setMessage("Please check again");
        builder.setCancelable(true);

        final AlertDialog dlg = builder.create();

        dlg.show();

        final Timer t = new Timer();
        t.schedule(new TimerTask() {
            public void run() {
                dlg.dismiss();
                t.cancel();
            }
        }, 100000);
    }

我在活动 1中得到如下结果:

public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        switch (requestCode) {
        case 100:
            System.err.println("after intent back 111111=== ");

             if (resultCode == Activity.RESULT_OK) {
            System.err.println("after intent back if=== ");

            int day = data.getIntExtra("Day", 0);
            int month = data.getIntExtra("Month", 0);
            int year = data.getIntExtra("Year", 0);
            System.err.println("after intent back =///== " + day + "-" + month
                    + "-" + year);
            Toast.makeText(Settings.this,day+"?"+month+"?"+year,Toast.LENGTH_SHORT).show();
            periodDatetv.setText(day + "/" + month + "/" + year);
            break;

         }

        }
    }

但它从不运行 onActivityResult 方法!我的代码有什么问题?请告诉我..提前谢谢

4

4 回答 4

2

在第二个活动中使用setResult(RESULT_OK);before方法finish()

从您想要的第二个活动应该将一些结果返回到第一个活动

于 2013-01-16T08:16:01.890 回答
2

setResult()您在活动 2 中注释掉了您的呼叫。

如果您使用 开始活动startActivityForResult(),则您开始的活动(在您的情况下为活动 2)需要在setResult()每次即将完成时调用。

看这个例子:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.blablabla)

    //we do this here to make sure result is set even if user leaves
    //activity, for example, by pressing back/home buttons
    //result we set here will be overridden by any latter calls if they occur
    setResult(RESULT_CANCELED);         

    //do some processing here
    Intent intent=new Intent();
    //now let's send the result back
    intent.putExtra(result);
    setResult(RESULT_OK, intent);
    finish();
}

private void another_method() {
    //do some processing here too
    Intent intent=new Intent();
    //sending the result back
    intent.putExtra(result);
    //need to call setResult() here too
    setResult(RESULT_OK, intent);
    finish();
}
于 2013-01-16T08:21:04.167 回答
1

这里的问题是你SettingsPeriodDate从开始Settings,然后SettingsSettingsPeriodDate.

您不应该再次启动设置。就在你的工作setResult()完成时finish()SettingsPeriodDate

于 2013-01-16T08:43:58.987 回答
0

在完成活动之前打电话setResult(RESULT_OK)。我在你的代码中找不到它。您不再设置结果,而是再次调用该活动。所以它会创建新的活动。不要以活动 1 的意图调用新活动。

于 2013-01-16T08:22:59.297 回答