调用finish()时Intent没有完成

时间:2016-05-02 03:21:56

标签: android android-intent timepicker

我正在制作一个闹钟,这个课程是快速选择小时和分钟,它会将其发回给我使用setForresult()的意图,但出于某种原因我按下按钮设置它重新创建活动的时间,我必须再次输入它,但第二次它做了它应该做的。

package com.bignerdranch.android.alarm;

import android.app.AlarmManager;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TimePicker;

public class setAlarmTimes extends AppCompatActivity {
AlarmManager manager;
private TimePicker myTimePicker;
private static final String TAG = "MYTAG";
private static Button sendVals;
int hour,minute;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_set_alarm_times);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    Log.i(TAG,"in ONCREATE");
    myTimePicker = (TimePicker) findViewById(R.id.timePicker);
    sendVals = (Button)findViewById(R.id.setAlarmValuesButton);

    sendVals.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent sent = new Intent(setAlarmTimes.this,setAlarmList.class);
            hour = myTimePicker.getCurrentHour();
            minute = myTimePicker.getCurrentMinute();
            sent.putExtra("HOUR",hour);
            sent.putExtra("MINUTE",minute);
            setResult(setAlarmList.REQUEST_CODE, sent);
            Log.i(TAG, "IMPORTANT VALUES _____" +hour + minute);
            finish();
            Log.i(TAG,"shouldnt ovvur");
        }
    });





    Log.i(TAG,"Hour and min is " + hour+ ":"+ minute);




}

}



package com.bignerdranch.android.alarm;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;

public class setAlarmList extends AppCompatActivity {
private static Button addAlarm;
private static Button removeAlarm;
public static final int REQUEST_CODE= 1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_set_alarm_list);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    addAlarm = (Button) findViewById(R.id.myAddButton);
    removeAlarm = (Button) findViewById(R.id.myRemoveButton);

    addAlarm.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent getTimes = new      Intent(setAlarmList.this,setAlarmTimes.class);
            startActivity(getTimes);
            startActivityForResult(getTimes,REQUEST_CODE);
        }
    });


}

}

2 个答案:

答案 0 :(得分:1)

Onclicklistener闹钟中(见下文)

addAlarm.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent getTimes = new      Intent(setAlarmList.this,setAlarmTimes.class);
        startActivity(getTimes); // here remove it
        startActivityForResult(getTimes,REQUEST_CODE); // here retain it
    }
});

你开始两次活动,你应该只使用startActivityForResult。删除startActivity(getTimes)

答案 1 :(得分:0)

使用以下代码:

addAlarm.setOnClickListener(new View.OnClickListener() { 
@Override 
public void onClick(View v) {
    Intent getTimes = new Intent(setAlarmList.this,setAlarmTimes.class);
    startActivityForResult(getTimes,REQUEST_CODE);  
} 
});