SetResult()没有把我带回第一个活动

时间:2017-05-20 12:32:19

标签: android startactivityforresult

所以我使用startActivityForResult()和SetResult来返回数据,但是当使用set result()和finish()时,活动就会死掉而不会回到第一个......

在MainActivity类中,startActivityForResult是一个警报活动,因为我的应用程序是警报/提醒,在我的课程中我有另一个类是适配器

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
 startActivity(new Intent(MainActivity.this, AlarmChooser.class));
}
 public class AlarmAdapter extends ArrayAdapter<MyAlarm> {

    Activity activity;
    int layoutResource;
    MyAlarm alarm;
    ArrayList<MyAlarm> mData = new ArrayList<>();

            public AlarmAdapter(Activity act, int resource, ArrayList<MyAlarm> data) {
        super(act, resource, data);
        activity = act;
        layoutResource = resource;
        mData = data;
        notifyDataSetChanged();
    }

     @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View row = convertView;
        ViewHolder holder = null;
        if(row==null || row.getTag()==null){
            LayoutInflater inflater = LayoutInflater.from(activity);
            row = inflater.inflate(layoutResource,null);
            holder=new ViewHolder();

            holder.mTitle=(TextView)row.findViewById(R.id.name);
            holder.mDate=(TextView)row.findViewById(R.id.dateText);
            holder.mTime=(TextView)row.findViewById(R.id.timeText);

            row.setTag(holder);
        }else{
            holder = (ViewHolder)row.getTag();
        }



        holder.myAlarm = getItem(position);
        holder.mTitle.setText(holder.myAlarm.getTitle());
        holder.mDate.setText(holder.myAlarm.getMonth()+" "+holder.myAlarm.getDay()+", "+holder.myAlarm.getYear());






        final ViewHolder finalHolder = holder;
        holder.mTitle.setOnClickListener(new View.OnClickListener() {



            public void onClick(View view) {
  Cursor cursor = dba.getnameofAlarm(title);
                if (cursor.moveToFirst()) {
                    do {
                        String yearData = (cursor.getString(cursor.getColumnIndex(Constants.COLUMN_YEAR)));
                        String monthData=(cursor.getString(cursor.getColumnIndex(Constants.COLUMN_MONTH)));
                        String dayData=(cursor.getString(cursor.getColumnIndex(Constants.COLUMN_DAY)));
                        String hourData=(cursor.getString(cursor.getColumnIndex(Constants.COLUMN_HOUR)));
                        String minuteData=(cursor.getString(cursor.getColumnIndex(Constants.COLUMN_MINUTE)));
                        String titleData=(cursor.getString(cursor.getColumnIndex(Constants.COLUMN_NAME)));
                        String typeData = cursor.getString(cursor.getColumnIndex(Constants.COLUMN_TYPE));
                        Log.e("YEAR", yearData);
                        Log.e("MONTH",monthData);
                        Log.e("DAY",dayData);
                        Log.e("HOUR",hourData);
                        Log.e("MINUTE",minuteData);
                        Log.e("TITLE",titleData);
                        Log.e("TYPE",typeData);



                            Class next = null;

                            switch (typeData) {
                                case "TimeDateRem":
                                    next = TimeDateRem.class;
                                    break;
                        case "BirthdayRem":
                            next = BirthdayRem.class;
                            break;
                        case "HolidayReminderKuwait":
                            next = HolidayReminderKuwait.class;
                            break;
                        case "HolidayReminderLebanon":
                            next = HolidayReminderLebanon.class;
                            break;
                        case "HolidayReminderSaudiArabia":
                            next = HolidayReminderSaudiArabia.class;
                            break;
                        case "HolidayReminderUAE":
                            next = HolidayReminderUAE.class;
                            break;
                            }

                            if (next != null) {
                                Intent return_intent = new Intent(MainActivity.this, next);
                                return_intent.putExtra("alarm_title", titleData);
                                return_intent.putExtra("alarm_month", monthData);
                                return_intent.putExtra("alarm_year", yearData);
                                return_intent.putExtra("alarm_day", dayData);
                                return_intent.putExtra("alarm_hour", hourData);
                                return_intent.putExtra("alarm_minute", minuteData);
                                   next_intent.putExtra("alarm_content", contenttt);
                                return_intent.putExtra("FROM_ACTIVITY", "MainActivity");
                                setResult(RESULT_OK, return_intent);

    finish();
                            }



                    } while (cursor.moveToNext());
                    cursor.close();
                }


            }

        });

        return row;

}
    }

    class ViewHolder{


        MyAlarm myAlarm;
        TextView mTitle;
        TextView mDate;
        TextView mTime;
        TextView mContent;
        TextView mID;
    }
}

这是我的TimeDateRem,其中有OnActivityResult

  private void setAlarm(Calendar targetCal) {



    info.setText("\n\n***\n"
            + "Alarm is set @ " + targetCal.getTime() + "\n"
            + "***\n");

    Intent intent = new Intent(getBaseContext(), AlarmReceiver.class);
    pendingIntent = PendingIntent.getBroadcast(getBaseContext(), RQS_1, intent, 0);
    alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    alarmManager.set(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(), pendingIntent);


  //notification


    Intent alarmIIntent = new Intent(this, AlarmReceiver.class);
    PendingIntent pIntent = PendingIntent.getBroadcast(this, 0, alarmIIntent, PendingIntent.FLAG_UPDATE_CURRENT);


    AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
    alarmManager.set(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(), pIntent);


    Intent intent_ForResult = new Intent(TimeDateRem.this, MainActivity.class);

    intent_ForResult.putExtra("title", title.getText().toString().trim());
    intent_ForResult.putExtra("day", pickerDate.getDayOfMonth());
    intent_ForResult.putExtra("month", pickerDate.getMonth());
    intent_ForResult.putExtra("year", pickerDate.getYear());
    intent_ForResult.putExtra("hour", pickerTime.getCurrentHour());
    intent_ForResult.putExtra("minute", pickerTime.getCurrentMinute());
    intent_ForResult.putExtra("type", "TimeDateRem");

    startActivityForResult(intent_ForResult, REQUEST);
  }
        @Override
       protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (requestCode == REQUEST) {
        if (resultCode == RESULT_OK) {
            Bundle extras = data.getExtras();
            String previousActivity = extras.getString("FROM_ACTIVITY");


            if (previousActivity.equals("MainActivity")) {

                String titles = extras.getString("alarm_title");
                int year_intent = extras.getInt("alarm_year");
                int month_intent = extras.getInt("alarm_month");
                int day_intent = extras.getInt("alarm_day");
                int minute_intent = extras.getInt("alarm_minute");
                int hour_intent = extras.getInt("alarm_hour");


                String year = String.valueOf(year_intent);
                String month = String.valueOf(month_intent);
                String day = String.valueOf(day_intent);
                String minute = String.valueOf(minute_intent);
                String hour = String.valueOf(hour_intent);


                title.setText(titles);
                pickerTime.setCurrentHour(Integer.parseInt(hour));
                pickerTime.setCurrentMinute(Integer.parseInt(minute));
                pickerDate.init(
                        Integer.parseInt(year),
                        Integer.parseInt(month),
                        Integer.parseInt(day),
                        null
                );



            }
        }
    }
}

有人可以帮我解释为什么setResult不会把我送回我的主要活动吗?

1 个答案:

答案 0 :(得分:1)

尝试使用以下内容:

activity.setResult(RESULT_OK, return_intent);
activity.finish();