首页 > 代码库 > 一个新的Activity跳转到带有Framgment的Activity页面

一个新的Activity跳转到带有Framgment的Activity页面

思路是:新的Activity跳转到带有Fragment的Activity中,同时带有标志性的标志位。

实例代码:

  1 package com.cn.gordon.exhibition.walk.activity;  2   3 import java.io.IOException;  4 import java.util.List;  5 import android.annotation.SuppressLint;  6 import android.app.Activity;  7 import android.content.Context;  8 import android.content.Intent;  9 import android.graphics.Bitmap; 10 import android.graphics.drawable.BitmapDrawable; 11 import android.graphics.drawable.Drawable; 12 import android.hardware.Sensor; 13 import android.hardware.SensorEvent; 14 import android.hardware.SensorEventListener; 15 import android.hardware.SensorManager; 16 import android.os.Bundle; 17 import android.os.Handler; 18 import android.os.Message; 19 import android.view.View; 20 import android.view.View.OnClickListener; 21 import android.widget.Button; 22 import android.widget.TextView; 23 import android.widget.Toast; 24 import com.cn.gordon.exhibition.walk.application.GlobalVaries; 25 import com.cn.gordon.exhibition.walk.db.server.BluetoothService; 26 import com.cn.gordon.exhibition.walk.db.server.ExhibitionHallService; 27 import com.cn.gordon.exhibition.walk.db.server.InformationDBService; 28 import com.cn.gordon.exhibition.walk.entity.BluetoothDataBase; 29 import com.cn.gordon.exhibition.walk.entity.ExhibitionsHallDataBase; 30 import com.cn.gordon.exhibition.walk.popup.PromptDialogPopupWindow; 31 import com.cn.gordon.exhibition.walk.utils.AsyncImageLoader; 32 import com.cn.gordon.exhibition.walk.utils.Utils; 33 import com.cn.gordon.exhibition.walk.utils.WalkMainUtils; 34 import com.cn.gordon.exhibition.walk.utils.AsyncImageLoader.ImageCallback; 35 import com.cn.gordon.exhibition.walk.view.MapView; 36  37 public class WalkHallActivity extends Activity   38 {   39     private SensorManager manager; 40     private float degree = 0,myPosition_x=0,myPosition_y=0; 41     private MapView mapview; 42     private Button btnBack,btnQuerry,btnTitle; 43     private TextView textDigit; 44      45     private WalkMainUtils walkUtils; 46     private GlobalVaries global; 47      48     private String sensingMac=""; 49      50     private AsyncImageLoader asyncImageLoader; 51     private Handler handler ; 52     private Runnable freshRunnable; 53     private PromptDialogPopupWindow pDialog; 54     private ExhibitionsHallDataBase eh; 55     private String hallCode=""; 56     private ExhibitionHallService ehService = new ExhibitionHallService(WalkHallActivity.this); 57      58     private SensorListener listener = new SensorListener(); 59                         60     @Override   61     protected void onCreate(Bundle savedInstanceState)   62     {   63         super.onCreate(savedInstanceState);   64         setContentView(R.layout.mymap);  65          66         hallCode = getIntent().getStringExtra("hallCode"); 67          68         mapview = (MapView)findViewById(R.id.mymap); 69         mapview.setFocusable(false); 70         manager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); 71          72         init(); 73         setAction(); 74          75         @SuppressWarnings("deprecation") 76         Sensor sensor = manager.getDefaultSensor(Sensor.TYPE_ORIENTATION); 77           manager.registerListener(listener, sensor, 78                 SensorManager.SENSOR_DELAY_GAME); 79         myHandler.sendEmptyMessage(0); 80 //        cutdownTime = new MyCount(60*1000, 1000);   81         initView(); 82 //        getNetInfo(); 83          84     } 85  86     private void init() { 87         btnBack =  (Button) findViewById(R.id.btn_back); 88         btnQuerry =  (Button) findViewById(R.id.btn_submenu_information); 89         btnTitle = (Button) findViewById(R.id.title_information); 90         textDigit = (TextView) findViewById(R.id.titlebar_information); 91         textDigit.setVisibility(View.GONE); 92         walkUtils = new WalkMainUtils(WalkHallActivity.this);     93         walkUtils.setInitLayoutColor(); 94          95         asyncImageLoader= new AsyncImageLoader(); 96         global = (GlobalVaries)getApplication(); 97     } 98      99     private void setAction() {100         mapview.setClickLinster(new MapView.OnMyClickLinster() {101             102             @Override103             public void onClick() {104                 if (walkUtils.isTBShowing()) {105                     walkUtils.dismissPopupWindow(mapview);106                 }else {107                     walkUtils.showPopupWindow(mapview);108                 }109                 110             }111         });112     113     114         btnBack.setOnClickListener(new OnClickListener() {115         116         @Override117         public void onClick(View v) {118             myHandler.removeMessages(0);119             finish();120         }121     });122     123     btnQuerry.setOnClickListener(new OnClickListener() {124         125         @Override126         public void onClick(View v) {127             walkUtils.dismissPopupWindow(v);128             Intent in = new Intent(WalkHallActivity.this,CheckoutResultActivity.class);129             startActivity(in);130         }131     });132     133     textDigit.setOnClickListener(new OnClickListener() {134         135         @Override136         public void onClick(View v) {137 //            textDigit.setVisibility(View.GONE);138             walkUtils.dismissPopupWindow(v);139         }140     });141     142     btnTitle.setOnClickListener(new OnClickListener() {143         144         @SuppressLint("CommitTransaction")145         @Override146         public void onClick(View v) {147             walkUtils.dismissPopupWindow(v);148             Intent in = new Intent(WalkHallActivity.this,WalkMainActivity.class);149             Bundle bund = new Bundle();150             bund.putInt("Flag", 1);151             in.putExtras(bund);152             startActivity(in);153             finish();154         }155     });156     157   }158     159     private void setTitleCount(){160         if (global.isUserLogin()) {161             InformationDBService iService = new InformationDBService(WalkHallActivity.this);162             int Count=iService.queryUnreadInfoCount(global.getUserHashCode(),global.getExhibitionHashCode());163             if(Count > 0){164                 textDigit.setVisibility(View.VISIBLE);165                 textDigit.setText(Count+"");166             }else{167                 textDigit.setVisibility(View.GONE);168             }169         }170     }171 172     private final class SensorListener implements SensorEventListener {173 174         @SuppressLint("HandlerLeak")175         @Override176         public void onSensorChanged(SensorEvent event) {177             degree = event.values[0];// 存放了方向值178             179         }180 181         @Override182         public void onAccuracyChanged(Sensor sensor, int accuracy) {183 184         }185 186     }187       188     @SuppressLint("HandlerLeak")189     private Handler myHandler = new Handler(){190         @Override191         public void handleMessage(Message msg) {192             super.handleMessage(msg);193             194             if(mapview != null){195                 if (mapview.hasMap()) {196 //                    System.out.println("mapview.hasMap()--->yes");197                     if (global.getOnMyRangeMacList()!=null&&global.getOnMyRangeMacList().size()>0&&!sensingMac.equals(global.getOnMyRangeMacList().get(0))) {198                         sensingMac = global.getOnMyRangeMacList().get(0);199                         BluetoothDataBase bBean = new BluetoothDataBase();200                         BluetoothService bService = new BluetoothService(WalkHallActivity.this);201                         bBean = bService.queryBlueBeanByMac(sensingMac, global.getExhibitionHashCode());202                         List<BluetoothDataBase> listBeans = bService.queryBlueBeanByExhibitionCode(global.getExhibitionHashCode());203                         if (listBeans!=null&&bBean!=null) {204                             if (listBeans.contains(bBean)) {205                                 if (bBean.getExhibitionRoomCode().equals(hallCode)) {206                                     myPosition_x = (float) bBean.getX();207                                     myPosition_y = (float) bBean.getY();208                                     mapview.setDegree(degree,myPosition_x,myPosition_y);//定位后再显示我的位置,并且我现在在这个展厅209                                 }else {210                                     //地图切换211                                     212                                     if (pDialog==null) {213                                         214                                         pDialog = new PromptDialogPopupWindow(WalkHallActivity.this, 4, "WalkHallActivity", "逛逛展会检测到您不在此展厅,是否切换到您所在的展厅?");215                                         int okCancel = pDialog.showPopupWindow(mapview);216                                         if (okCancel==1) {217 //                                            String newHallCode = bBean.getExhibitionRoomCode();218 //                                            eh = ehService.queryHallByHashCodes(newHallCode, global.getExhibitionHashCode());219 //                                            if (eh!=null&&eh.getImageLocalPosition()!=null) {220 //                                                Bitmap bitMap = Utils.getBitmapFromSD(eh.getImageLocalPosition());221 //                                                if (bitMap!=null) {222 //                                                    mapview.setMap(bitMap);223 //                                                }else {224 //                                                    getMapFromNet();225 //                                                }226 //                                                hallCode = newHallCode;227 //                                            }228                                             Intent in = new Intent(WalkHallActivity.this,WalkHallActivity.class);229                                             Bundle bund = new Bundle();230                                             bund.putString("hallCode", bBean.getExhibitionRoomCode());231                                             in.putExtras(bund);232                                             startActivity(in);233                                             finish();234                                         }235                                     }236                                 }237                             }238                             239                         }240                         241                     }else {242 //                        System.out.println("else----->myPosition_x="+myPosition_x+"myPosition_y="+myPosition_y);243                     }244                     245                     246                 }else {247                     System.out.println("mapview.hasMap()--->no");248                     249 //                    eh = global.getSelectedHall();250                     eh = ehService.queryHallByHashCodes(hallCode, global.getExhibitionHashCode());251                     if (eh!=null&&eh.getImageLocalPosition()!=null) {252                         Bitmap bitMap = Utils.getBitmapFromSD(eh.getImageLocalPosition());253                         if (bitMap!=null) {254                             mapview.setMap(bitMap);255                             System.out.println("图片==========>1");256                         }else {257                             getMapFromNet();258                             System.out.println("图片==========>2");259                         }260                     261                 }else {262                     getMapFromNet();263                     System.out.println("图片==========>3");264                     System.out.println("global.getSelectedHall() = null");265                 }266             }267                 268             }269             myHandler.sendEmptyMessageDelayed(0, 1000);270         }271     };272     273     private void getMapFromNet(){274         String urlStr = "" ;275         if (eh!=null) {276             urlStr = eh.getImageNetPosition();277         }278         asyncImageLoader.loadDrawable(urlStr, new ImageCallback(){279                 @Override280                 public void onl oaded(Drawable imageDrawable) {281                     if(imageDrawable != null){282                         BitmapDrawable draw = (BitmapDrawable) imageDrawable;283                         final Bitmap bitmap = draw.getBitmap();284                         if (bitmap!=null) {285                            mapview.setMap(bitmap);286                      }287                         try {  288                             if (bitmap!=null) {289                                 Toast.makeText(WalkHallActivity.this, "图片下载成功", Toast.LENGTH_SHORT).show();290                                 Utils.savePicture(bitmap, eh.getHallHashCode()+".jpg",Utils.ALBUM_PATH,WalkHallActivity.this);291                                 292                                 ehService.setHallLocalPosition(eh.getHallHashCode(), Utils.ALBUM_PATH+java.io.File.separator+eh.getHallHashCode()+".jpg");293                                 }else{294                                     295                                     Toast.makeText(WalkHallActivity.this, "图片下载失败", Toast.LENGTH_SHORT).show();296                                 }297                         } catch (IOException e) {  298                             e.printStackTrace();  299                         }  300                     }301                 }302 303                 @Override304                 public void one rror(Exception e) {305                     306                 }307            });308     }309     310     311     312     @Override313     protected void onPause() {314         if (handler!=null) {315             System.out.println("handler.removeCallbacks(freshRunnable, 1000);");316             handler.removeCallbacks(freshRunnable);317         }318         super.onPause();319     }320 321     public void onResume() {322         if (handler!=null) {323             System.out.println("handler.postDelayed(freshRunnable, 1000);");324             handler.postDelayed(freshRunnable, 1000);325         }326         setTitleCount();327         super.onResume();328     };329     330     public void onStop() {331         super.onStop();332         if (handler!=null) {333             handler.removeCallbacks(freshRunnable);334         }335     };336     337     338     339     public void initView(){340         if (global.isUserLogin()) {341             handler = new Handler();342             freshRunnable = new Runnable() {343                 @Override344                 public void run() {345                     InformationDBService iService = new InformationDBService(WalkHallActivity.this);346                     int Count=iService.queryUnreadInfoCount(global.getUserHashCode(),global.getExhibitionHashCode());347                     if(Count > 0){348                         textDigit.setVisibility(View.VISIBLE);349                         textDigit.setText(Count+"");350                     }else{351                         textDigit.setVisibility(View.GONE);352                     }353                     handler.postDelayed(this, 60000);//一分钟刷新一次页面354                 }355             };356             handler.postDelayed(freshRunnable, 2000);357             358             359         }360         361     }362 363 }364     


另一个带有Fragment的Activity实例代码:

 1 package com.cn.gordon.exhibition.walk.activity; 2  3 import java.io.File; 4  5 import android.annotation.SuppressLint; 6 import android.annotation.TargetApi; 7 import android.app.Activity; 8 import android.bluetooth.BluetoothAdapter; 9 import android.bluetooth.BluetoothManager;10 import android.content.Context;11 import android.content.Intent;12 import android.content.pm.PackageManager;13 import android.os.Bundle;14 import android.support.v4.app.Fragment;15 import android.support.v4.app.FragmentActivity;16 import android.support.v4.app.FragmentTransaction;17 import android.view.KeyEvent;18 import android.widget.Toast;19 20 import com.cn.gordon.exhibition.walk.application.GlobalVaries;21 import com.cn.gordon.exhibition.walk.db.service.InfoAndArrangeService;22 import com.cn.gordon.exhibition.walk.db.service.PositionBLEService;23 import com.cn.gordon.exhibition.walk.fragment.InformationFragment;24 import com.cn.gordon.exhibition.walk.fragment.SelectHallFragment;25 import com.cn.gordon.exhibition.walk.fragment.VisitPlanFragment;26 import com.cn.gordon.exhibition.walk.utils.DrawerLayoutUtils;27 import com.cn.gordon.exhibition.walk.utils.Utils;28 29 public class WalkMainActivity extends FragmentActivity  30 {  31     private static final int REQUEST_ENABLE_BT = 1234;32     private GlobalVaries global ;33     private DrawerLayoutUtils drawerUtil;34     private Intent bleServiceIntent,infoAndArrangeIntent;35     36 //    private static String ALBUM_PATH = Utils.getSDPath() + java.io.File.separator +  "exhibition_walk"; 37     38 //    private AsyncImageLoader asyncImageLoader;39     40 //    private ExhibitionsHallDataBase eh;41                        42     @Override  43     protected void onCreate(Bundle savedInstanceState)  44     {  45         super.onCreate(savedInstanceState);  46         setContentView(R.layout.activity_main);  47         48         File dirFile = new File(Utils.ALBUM_PATH);  49         if(!dirFile.exists()){  50             dirFile.mkdirs();  51         } 52         53         drawerUtil = new DrawerLayoutUtils(this);54         global =(GlobalVaries) getApplication();55         global.setDrawerUtil(drawerUtil);56         57         Bundle bundle=this.getIntent().getExtras();58         if(bundle !=null && bundle.getInt("Flag") ==0 ){59             //跳转到参展计划60             FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); 61             Fragment fragment = new VisitPlanFragment();62             ft.replace(R.id.fragment_layout, fragment);  63             ft.commit();64         }else if (bundle !=null && bundle.getInt("Flag") ==1 ) {65              FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); 66               Fragment fragment = new InformationFragment();67               ft.replace(R.id.fragment_layout, fragment);  68              ft.commit();69         }else{70             //默认跳转到主页面71             FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); 72             Fragment fragment = new SelectHallFragment(); 73             ft.replace(R.id.fragment_layout, fragment);  74             ft.commit();  75         }76         77         init();78     }  79     80         81 82 }83     


然后就是在Activity归属中的Fragment 了,代码是:

package com.cn.gordon.exhibition.walk.fragment;import java.text.SimpleDateFormat;import java.util.ArrayList;import java.util.Date;import java.util.HashMap;import java.util.List;import android.annotation.SuppressLint;import android.content.Context;import android.content.Intent;import android.graphics.Color;import android.os.Bundle;import android.os.Handler;import android.support.v4.app.Fragment;import android.view.LayoutInflater;import android.view.View;import android.view.View.OnClickListener;import android.view.ViewGroup;import android.widget.AdapterView;import android.widget.BaseAdapter;import android.widget.Button;import android.widget.ListView;import android.widget.TextView;import android.widget.Toast;import android.widget.AdapterView.OnItemClickListener;import com.cn.gordon.exhibition.walk.activity.ExhibitionNoticeActivity;import com.cn.gordon.exhibition.walk.activity.ForumDetailsActivity;import com.cn.gordon.exhibition.walk.activity.PositionRemindActivity;import com.cn.gordon.exhibition.walk.activity.R;import com.cn.gordon.exhibition.walk.application.GlobalVaries;import com.cn.gordon.exhibition.walk.db.server.InformationDBService;import com.cn.gordon.exhibition.walk.decoding.Constants;import com.cn.gordon.exhibition.walk.entity.InformationDataBase;import com.cn.gordon.exhibition.walk.entity.MyInformationItemBean;import com.cn.gordon.exhibition.walk.utils.DrawerLayoutUtils;@SuppressLint("ResourceAsColor")public class InformationFragment extends Fragment {    private ListView listView;    private Button btnTitle,btnMenu;    private TextView textTitle;    private DrawerLayoutUtils drawerUtil;//    private InformationDBService DBservice ;    private List<InformationDataBase> DBInfoList;    private    ArrayList<HashMap<String, Object>> list;    private MyAdapter adapter;    private GlobalVaries global;    private Handler handler;    private Runnable runnable;    public View onCreateView(LayoutInflater inflater, ViewGroup container,            Bundle savedInstanceState) {        final View view = inflater.inflate(R.layout.view_information, null);        init(view);        setAction(view);        return view;    }    public void onActivityCreated(Bundle savedInstanceState) {        super.onActivityCreated(savedInstanceState);    }    private void init(View view) {        btnTitle = (Button) view.findViewById(R.id.title_information);        btnMenu = (Button) view.findViewById(R.id.btn_menu);        textTitle = (TextView) view.findViewById(R.id.titlebar_information);        textTitle.setVisibility(View.GONE);                global = (GlobalVaries)getActivity().getApplication();        drawerUtil = global.getDrawerUtil();//        DBservice = new InformationDBService(getActivity());        listView = (ListView)view.findViewById(R.id.myinformationlistView);        refreshProgram();        //TODO--->刷新页面  1 min        handler = new Handler();        runnable = new Runnable() {            @Override            public void run() {                                refreshProgram();                                handler.postDelayed(this, 60000);//一分钟刷新一次            }        };        handler.postDelayed(runnable, 60000);            }            private void setAction(final View view) {        btnTitle.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {//                Toast.makeText(view.getContext(), "点击了Title", Toast.LENGTH_SHORT).show();                //待完成......            }        });        textTitle.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                Toast.makeText(view.getContext(), "点击了TitleBar", Toast.LENGTH_SHORT).show();                textTitle.setVisibility(View.INVISIBLE);                }        });        btnMenu.setOnClickListener(new OnClickListener() {            @Override            public void onClick(View v) {                drawerUtil.openCloseDrawerLayout();            }        });    }        private void setTitleCount(){        if (global.isUserLogin()) {            InformationDBService iService = new InformationDBService(getActivity());            int Count=iService.queryUnreadInfoCount(global.getUserHashCode(),global.getExhibitionHashCode());            if(Count > 0){                textTitle.setVisibility(View.VISIBLE);                textTitle.setText(Count+"");            }else{                textTitle.setVisibility(View.GONE);            }        }    }    //..........        @SuppressLint("SimpleDateFormat")    private  ArrayList<MyInformationItemBean> getmylist(){         ArrayList<MyInformationItemBean> list = new ArrayList<MyInformationItemBean>();         if (global.isUserLogin()) {                          InformationDBService iService = new InformationDBService(getActivity());             DBInfoList = iService.queryAll(global.getUserHashCode(),global.getExhibitionHashCode());             if(DBInfoList != null && DBInfoList.size()>0){                 for(int i=0;i<DBInfoList.size();i++){                     MyInformationItemBean bean = new MyInformationItemBean();                     Constants covterType=new Constants();                     bean.setInformationContent(covterType.typeToName(DBInfoList.get(i).getType())+": "+DBInfoList.get(i).getName());                     bean.setInformationTime(new SimpleDateFormat("yyyy.MM.dd HH:mm:ss").format(new Date(DBInfoList.get(i).getBeginTime())));                     bean.setFlag(DBInfoList.get(i).getReadFlag());                     list.add(i, bean);                 }             }        }                  return list;    }    public ArrayList<HashMap<String,Object>> getItem(){         ArrayList<HashMap<String, Object>> item = new ArrayList<HashMap<String, Object>>();         MyInformationItemBean bean = new MyInformationItemBean();         for (int i = 0; i < getmylist().size(); i++) {             bean = getmylist().get(i);             HashMap<String, Object> map = new HashMap<String, Object>();             map.put("information", bean.getInformationContent());             map.put("informationtime", bean.getInformationTime());             map.put("flag", bean.getFlag());             item.add(map);        }         return item;    }            }

 

一个新的Activity跳转到带有Framgment的Activity页面