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

原创 2017年01月02日 22:14:01
/**
 * 
 */
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());
}
};

};

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

相关文章推荐

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

/**  *   */ package com.cn.xm.yunluhealth.ui.set; import java.util.ArrayList; import java.util.List...

1Activity+多个Fragment模式下进行横竖屏切换的问题及解决方式

1Activity+多Fragment的模式。相信大多数Android app开发人员都不会感到陌生。很常见的界面,比如一些平板的设置界面。就是左边一排按钮,右边区域专门用来放左边按钮点击之后需要展示...

Activity显示切换多个Fragment

  • 2017年05月25日 20:20
  • 20.13MB
  • 下载

底部切换标签 Activity+子fragment

  • 2017年04月04日 13:58
  • 4.46MB
  • 下载

Android 实现同个Activity中存在多个Fragment多次切换之后依次返回(二)(自维护栈的简单使用)

继上篇博文说到了实现实现同个Activity中存在多个Fragment多次切换之后依次返回之后,就一直想自己维护一个Fragment的栈来实现同样的效果甚至比依次返回的效果更佳,能够随意回退到任意Fr...

Fragment,Activity切换动画demo

  • 2014年12月23日 15:33
  • 658KB
  • 下载

fragment,activity之随意切换

  • 2016年03月22日 11:03
  • 1.46MB
  • 下载

android使用viewPager和Fragment实现滑动切换activity!

这个效果在现在的软件中非常的常见;故

底部切换标签(方式二)Activity+Fragment

  • 2017年04月04日 12:59
  • 21.92MB
  • 下载

Android - 数据库自动更新,横竖屏切换,系统控件样式,Fragment添加到Activity,Fragment生命周期,缓存,获取/清除缓存

Android - 数据库自动更新,横竖屏切换,系统控件样式,Fragment添加到Activity,Fragment生命周期,缓存,获取/清除缓存...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:activity使用fragment transaction.commit()进行fragment切换,activity被系统回收后再点应用就无法切换
举报原因:
原因补充:

(最多只允许输入30个字)