这是我的问题:在选择i调用finish()关闭活动后,我启动了一个活动来选择日期。下一步我重新打开相同的活动,我发现活动没有调用oncreate方法。有没有人可以告诉我为什么?
这是清单文件和样式中的活动。
<activity android:name=".activity.CalendarViewActivity"
android:theme="@style/calendarview_activity"/>
<style name="calendarview_activity" parent="Theme.AppCompat.Dialog">
<item name="android:windowBackground">@drawable/calendarview_act_bg</item>
<item name="colorAccent">@color/blue_toolbar_title</item>
<item name="android:textColor">@color/colorBlack</item>
<item name="android:windowShowWallpaper">true</item>
<item name="windowNoTitle">true</item>
</style>
这是活动代码
public class CalendarViewActivity extends BaseActivity
implements View.OnClickListener, OnDateSelectedListener, OnMonthChangedListener {
private MaterialCalendarView mMaterialCV;
DatabaseReference mTimesheetDailyStatusRef;
private CalendarDay mMonthDate;
private TextView tvYear;
private TextView tvDay;
private static final int SUBMITTED = 2;
private static final int UN_SUBMITTED = 1;
private static final int RESULT_CODE = 200;
private ValueEventListener mStatusListener;
private DatabaseReference statusRootRef;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_calendarview_activity);
initView();
initData();
}
private void initData() {
statusRootRef = FirebaseDatabase.getInstance().getReference().child(Constants
.COLLECTION_ENGINEER_ROOT).child(Constants.COLLECTION_TIMESHEETS_STATUS).child
(getUid());
mTimesheetDailyStatusRef = statusRootRef
.child(String.valueOf(Calendar.getInstance().get(Calendar.YEAR)))
.child(String.valueOf(Calendar.getInstance().get(Calendar.MONTH)));
CalendarDay calendarDay = CalendarDay.from(Calendar.getInstance());
setStatus(calendarDay);
}
@Override
protected void onResume() {
super.onResume();
}
private void initView() {
setSize();
mMaterialCV = (MaterialCalendarView) findViewById(R.id.calendarview_timesheet);
mMaterialCV.setWeekDayTextAppearance(getResColor(R.color.colorBlack));
mMaterialCV.setHeaderTextAppearance(getResColor(R.color.colorBlack));
mMaterialCV.setOnDateChangedListener(this);
mMaterialCV.setOnMonthChangedListener(this);
mMaterialCV.setCurrentDate(Calendar.getInstance());
Intent intent = getIntent();
mMonthDate = intent.getParcelableExtra(Constants.KEY_SELECTED_DATE);
if (mMonthDate != null) {
mMaterialCV.setSelectedDate(mMonthDate);
mMaterialCV.setCurrentDate(mMonthDate);
} else {
setCurrentDate();
}
tvDay = (TextView) findViewById(R.id.calendarview_day);
tvYear = (TextView) findViewById(R.id.calendarview_year);
setSelectDataToTop(mMonthDate);
TextView tvOK = (TextView) findViewById(R.id.choose_day_ok);
TextView tvCancel = (TextView) findViewById(R.id.choose_day_cancel);
tvCancel.setTypeface(fontMedium);
tvOK.setTypeface(fontMedium);
tvCancel.setOnClickListener(this);
tvOK.setOnClickListener(this);
}
private void setCurrentDate() {
Calendar c = Calendar.getInstance();
mMonthDate = CalendarDay.from(c);
}
private void setSize() {
WindowManager m = getWindowManager();
Display d = m.getDefaultDisplay();
WindowManager.LayoutParams p = getWindow().getAttributes();
p.height = (int) (d.getHeight() * 0.85);
p.width = (int) (d.getWidth() * 0.9);
p.alpha = 1.0f;
p.dimAmount = 0.0f;
getWindow().setAttributes(p);
getWindow().setGravity(Gravity.TOP);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.choose_day_cancel:
finish();
break;
case R.id.choose_day_ok:
Intent backIntent = new Intent();
backIntent.putExtra(Constants.KEY_SELECTED_DATE, mMonthDate);
setResult(RESULT_CODE, backIntent);
finish();
break;
default:
break;
}
}
private void setSelectDataToTop(CalendarDay mMonthDate) {
tvYear.setText(String.valueOf(mMonthDate.getYear()));
int dayOfWeek = mMonthDate.getCalendar().get(Calendar.DAY_OF_WEEK);
String monthAndDay = TimeFormatUtil.getDate(mMonthDate.getCalendar().getTimeInMillis());
tvDay.setText(TimeFormatUtil.getWeekSimple(dayOfWeek) + ", " + monthAndDay);
}
@Override
public void onDateSelected(@NonNull MaterialCalendarView widget, @NonNull CalendarDay date,
boolean selected) {
mMonthDate = date;
setSelectDataToTop(date);
mMaterialCV.invalidateDecorators();
}
@Override
public void onMonthChanged(MaterialCalendarView widget, CalendarDay date) {
mTimesheetDailyStatusRef.removeEventListener(mStatusListener);
mTimesheetDailyStatusRef = null;
mStatusListener = null;
mMonthDate = date;
setStatus(mMonthDate);
}
private void setStatus(final CalendarDay calendarDay) {
mTimesheetDailyStatusRef = statusRootRef.child(String.valueOf(calendarDay.getYear())).child
(String.valueOf(calendarDay.getMonth()));
mStatusListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
ArrayList<CalendarStatusDay> days = new ArrayList<>();
for (DataSnapshot child : dataSnapshot.getChildren()) {
CalendarStatusDay day = child.getValue(CalendarStatusDay.class);
days.add(day);
}
if (isFinishing()) {
return;
}
Calendar calendar = Calendar.getInstance();
ArrayList<CalendarDay> unSubmittedDays = new ArrayList<>();
unSubmittedDays.clear();
ArrayList<CalendarDay> submittedDays = new ArrayList<>();
submittedDays.clear();
for (CalendarStatusDay day : days) {
if (day.getStatus() == SUBMITTED) {
calendar.set(calendarDay.getYear(), calendarDay.getMonth(),
day.getDayOfmonth());
submittedDays.add(CalendarDay.from(calendar));
} else if (day.getStatus() == UN_SUBMITTED) {
calendar.set(calendarDay.getYear(), calendarDay.getMonth(),
day.getDayOfmonth());
unSubmittedDays.add(CalendarDay.from(calendar));
}
}
mMaterialCV.addDecorators(
new EventDecorator(getDrawable(R.drawable.submitted_selector),
submittedDays));
mMaterialCV.addDecorator(
new EventDecorator(getDrawable(R.drawable.unsubmitted_selector),
unSubmittedDays));
mMaterialCV.addDecorators();
unSubmittedDays.clear();
unSubmittedDays = null;
submittedDays.clear();
submittedDays = null;
days.clear();
days = null;
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
};
mTimesheetDailyStatusRef.addValueEventListener(mStatusListener);
}
}
答案 0 :(得分:0)
因为活动已在后台运行。如果您正在寻找动作回调,则可以使用startActivityForResult
方法并在调用活动的onActivityResult
中获得回调。
如果您确实要打开同一活动的新实例,请查找launchMode
参数并将其添加到清单参考this
答案 1 :(得分:0)
如果您使用startActivityForResult
获取SELECTED DATE,那么您需要覆盖onActivityResult
中的Actvity
方法。因为当setResult
如下所示代码控制转到{之前onActivityResult
中的{1}}方法。
Actvity
回调方法从其他活动
获取消息Intent backIntent = new Intent();
backIntent.putExtra(Constants.KEY_SELECTED_DATE, mMonthDate);
setResult(RESULT_CODE, backIntent);
finish();