关闭

activity使用fragment transaction.commit()进行fragment切换,activity被系统回收后再点应用就无法切换

84人阅读 评论(0) 收藏 举报
/**
 * 
 */
package com.cn.xm.yunluhealth.ui.set;

import java.util.ArrayList;
import java.util.List;

import net.tsz.afinal.http.AjaxCallBack;
import android.app.Dialog;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.cn.xm.yunluhealth.BaseActivity;
import com.cn.xm.yunluhealth.callback.TokenCallback;
import com.cn.xm.yunluhealth.entity.DTime;
import com.cn.xm.yunluhealth.entity.wrapper.EntityWrapper;
import com.cn.xm.yunluhealth.ui.set.fragment.FridayFragment;
import com.cn.xm.yunluhealth.ui.set.fragment.MondayFragment;
import com.cn.xm.yunluhealth.ui.set.fragment.SaturdayFragment;
import com.cn.xm.yunluhealth.ui.set.fragment.SundayFragment;
import com.cn.xm.yunluhealth.ui.set.fragment.ThursdayFragment;
import com.cn.xm.yunluhealth.ui.set.fragment.TuesdayFragment;
import com.cn.xm.yunluhealth.ui.set.fragment.WednesdayFragment;
import com.cn.xm.yunluhealth.util.Apis;
import com.cn.xm.yunluhealth.util.Common;
import com.cn.xm.yunluhealth.util.NetUtil;
import com.cn.xm.yunluhealth.util.UIHelper;
import com.cn.xm.yunluhealth.util.UserUtils;
import com.cn.xm.yunluhealthd.R;
import com.google.gson.Gson;

/**
 * 设置接诊时间
 * 
 * @author dpy
 * 
 */
public class SetHelpTimeActivity extends BaseActivity implements
OnClickListener {
private LinearLayout llMonday;
private TextView tvMonday;
private ImageView ivMonday;
private LinearLayout llTuesday;
private TextView tvTuesday;
private ImageView ivTuesday;
private LinearLayout llWednesday;
private TextView tvWednesday;
private ImageView ivWednesday;
private LinearLayout llThursday;
private TextView tvThursday;
private ImageView ivThursday;
private LinearLayout llFriday;
private TextView tvFriday;
private ImageView ivFriday;
private LinearLayout llSaturday;
private TextView tvSaturday;
private ImageView ivSaturday;
private LinearLayout llSunday;
private TextView tvSunday;
private ImageView ivSunday;
private MondayFragment mondayFragment;
private TuesdayFragment tuesdayFragment;
private WednesdayFragment wednesdayFragment;
private List<Fragment> fragments = new ArrayList<Fragment>();
private FragmentManager fragmentManager;
private FragmentTransaction transaction;
private ThursdayFragment thursdayFragment;
private FridayFragment fridayFragment;
private SaturdayFragment saturdayFragment;
private SundayFragment sundayFragment;

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setCenterTitle("设置与帮助");
enableBackButton(R.drawable.regist_back);
setContentView(R.layout.set_help_time);
mBtnTitleRight.setVisibility(View.VISIBLE);
mBtnTitleRight.setText(" 提交 ");
mBtnTitleRight.setBackgroundResource(R.drawable.btn_commit);
mBtnTitleRight.setOnClickListener(this);// 未实现
initViews();
}

private void initViews() {
// TODO Auto-generated method stub
llMonday = (LinearLayout) findViewById(R.id.llMonday);
tvMonday = (TextView) findViewById(R.id.tvMonday);
llMonday.setSelected(true);
ivMonday = (ImageView) findViewById(R.id.ivMonday);
ivMonday.setVisibility(View.VISIBLE);
llTuesday = (LinearLayout) findViewById(R.id.llTuesday);
tvTuesday = (TextView) findViewById(R.id.tvTuesday);
ivTuesday = (ImageView) findViewById(R.id.ivTuesday);
llWednesday = (LinearLayout) findViewById(R.id.llWednesday);
tvWednesday = (TextView) findViewById(R.id.tvWednesday);
ivWednesday = (ImageView) findViewById(R.id.ivWednesday);
llThursday = (LinearLayout) findViewById(R.id.llThursday);
tvThursday = (TextView) findViewById(R.id.tvThursday);
ivThursday = (ImageView) findViewById(R.id.ivThursday);
llFriday = (LinearLayout) findViewById(R.id.llFriday);
tvFriday = (TextView) findViewById(R.id.tvFriday);
ivFriday = (ImageView) findViewById(R.id.ivFriday);
llSaturday = (LinearLayout) findViewById(R.id.llSaturday);
tvSaturday = (TextView) findViewById(R.id.tvSaturday);
ivSaturday = (ImageView) findViewById(R.id.ivSaturday);
llSunday = (LinearLayout) findViewById(R.id.llSunday);
tvSunday = (TextView) findViewById(R.id.tvSunday);
ivSunday = (ImageView) findViewById(R.id.ivSunday);

llMonday.setOnClickListener(this);
llTuesday.setOnClickListener(this);
llWednesday.setOnClickListener(this);
llThursday.setOnClickListener(this);
llFriday.setOnClickListener(this);
llSaturday.setOnClickListener(this);
llSunday.setOnClickListener(this);
// 添加fragment
mondayFragment = new MondayFragment();
tuesdayFragment = new TuesdayFragment();
wednesdayFragment = new WednesdayFragment();
thursdayFragment = new ThursdayFragment();
fridayFragment = new FridayFragment();
saturdayFragment = new SaturdayFragment();
sundayFragment = new SundayFragment();
fragments.add(mondayFragment);
fragments.add(tuesdayFragment);
fragments.add(wednesdayFragment);
fragments.add(thursdayFragment);
fragments.add(fridayFragment);
fragments.add(saturdayFragment);
fragments.add(sundayFragment);
fragmentManager = this.getSupportFragmentManager();
transaction = fragmentManager.beginTransaction();
transaction.add(R.id.container, mondayFragment);
transaction.add(R.id.container, tuesdayFragment);
transaction.add(R.id.container, wednesdayFragment);
transaction.add(R.id.container, thursdayFragment);
transaction.add(R.id.container, fridayFragment);
transaction.add(R.id.container, saturdayFragment);
transaction.add(R.id.container, sundayFragment);
transaction.commit();
selectOrderTime = new ArrayList<DTime>();
}

/**
 * 更改Fragment对象
 * 
 * 
 * 
 * 
 * @param index
 */
private void switchFragment(int index) {
transaction = fragmentManager.beginTransaction();
transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
for (int i = 0; i < fragments.size(); i++) {
if (index == i) {
transaction.show(fragments.get(index));
} else {
transaction.hide(fragments.get(i));
}
}
transaction.commit();
}

private ArrayList<DTime> selectOrderTime;
private Dialog waitingDialog;

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

switch (v.getId()) {
case R.id.llMonday:
switchFragment(0);
tvMonday.setSelected(true);
ivMonday.setVisibility(View.VISIBLE);
tvTuesday.setSelected(false);
ivTuesday.setVisibility(View.INVISIBLE);
tvWednesday.setSelected(false);
ivWednesday.setVisibility(View.INVISIBLE);
tvThursday.setSelected(false);
ivThursday.setVisibility(View.INVISIBLE);
tvFriday.setSelected(false);
ivFriday.setVisibility(View.INVISIBLE);
tvSaturday.setSelected(false);
ivSaturday.setVisibility(View.INVISIBLE);
tvSunday.setSelected(false);
ivSunday.setVisibility(View.INVISIBLE);

break;

case R.id.btn_title_right:
commit();
break;
default:
break;
}
}

private void commit() {
if (!NetUtil.hasNetWrok(mContext)) {
// 保存数据到数据库。。。
return;
}
waitingDialog = UIHelper.getWaitingDialog(mContext, "正在修改...");
waitingDialog.show();
// 设置预约时间
selectOrderTime.addAll(mondayFragment.getSelectOrderTime());
selectOrderTime.addAll(tuesdayFragment.getSelectOrderTime());
selectOrderTime.addAll(wednesdayFragment.getSelectOrderTime());
selectOrderTime.addAll(thursdayFragment.getSelectOrderTime());
selectOrderTime.addAll(fridayFragment.getSelectOrderTime());
selectOrderTime.addAll(saturdayFragment.getSelectOrderTime());
selectOrderTime.addAll(sundayFragment.getSelectOrderTime());
UserUtils.getToken(mContext, new TokenCallback() {

@Override
public void getToken(String token, String doctorid) {
if (!token.equals("false")) {
Apis.reservationTime(ajaxCallBack, token, doctorid,
new Gson().toJson(selectOrderTime));
} else {
waitingDialog.dismiss();
}

}
});
}

AjaxCallBack<String> ajaxCallBack = new AjaxCallBack<String>() {
@Override
public void onFailure(Throwable t, int errorNo, String strMsg) {
// TODO Auto-generated method stub
super.onFailure(t, errorNo, strMsg);
Common.print(strMsg);
waitingDialog.dismiss();
}

@Override
public void onSuccess(String t) {
// TODO Auto-generated method stub
super.onSuccess(t);
Common.print(t);
waitingDialog.dismiss();
EntityWrapper wrapper = new Gson().fromJson(t, EntityWrapper.class);
if (wrapper.getStatus().equals("1")) {
UIHelper.showToast(mActivity, "设置成功");
mActivity.finish();
} else {
UIHelper.showToast(mActivity, wrapper.getMsg());
}
};

};

}
,点击无反应,求大神指导
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:1527次
    • 积分:148
    • 等级:
    • 排名:千里之外
    • 原创:14篇
    • 转载:0篇
    • 译文:0篇
    • 评论:0条
    文章存档