启动两次碎片 [英] Fragments starting twice
问题描述
我有这个程序中,即时通讯使用的碎片,但不知何故,每次我启动应用程序,每一个活动(片段)启动两次。我敲我的脑袋一下,但我无法找到答案。有人可以帮我吗?
下面是我的主要FragmentActivity:
公共类TestSearch延伸活动{
//这个活动开始的片段
私人DrawerLayout mDrawerLayout;
私人的ListView mDrawerList;
//私人ActionBarDrawerToggle mDrawerToggle;
私人的ArrayList<装置>设备;
私人的ArrayList< Recepie> recepies,mainRecepies;
私人的ArrayList< Recepie>搜索结果;
私人的LinearLayout sideWrapper;
私人的EditText SRC;
ArrayList的<类别>猫;
布尔isTablet = FALSE;
@燮pressWarnings(未登记)
@覆盖
保护无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.test_search);
Log.v( - ,开始);
如果(Constants.isTablet(本)){
isTablet = TRUE;
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
} 其他 {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
设备=新的ArrayList<装置>();
recepies =新的ArrayList< Recepie>();
mainRecepies =新的ArrayList< Recepie>();
设备=(ArrayList的<装置>)getIntent()getSerializableExtra(。
Constants.DEVICES_EXTRA);
recepies = getIntent()getParcelableArrayListExtra(全)。
mainRecepies = getIntent()。getParcelableArrayListExtra(
Constants.MAINRECEPIES);
// mTitle = mDrawerTitle =的getTitle();
猫=新的ArrayList<类别>();
猫=(ArrayList的<类别>)getIntent()getSerializableExtra(猫)。
mDrawerLayout =(DrawerLayout)findViewById(R.id.drawer_layout);
mDrawerList =(ListView控件)findViewById(R.id.left_drawer);
sideWrapper =(的LinearLayout)findViewById(R.id.listwraper);
//设置自定义影子覆盖的主要内容时,抽屉
//打开
mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow,
GravityCompat.START);
//设置抽屉的列表视图项,然后单击监听器
//查看标题= getLayoutInflater()。膨胀(R.layout.search_item,
// 空值);
SRC =(EditText上)findViewById(R.id.search_est);
src.setOnKeyListener(新OnKeyListener(){
公共布尔onKey(视图V,INT关键code,KeyEvent的事件){
//如果事件是对输入按钮的键 - 向下事件
如果((event.getAction()== KeyEvent.ACTION_DOWN)
&功放;&安培; (钥匙code == KeyEvent.KEY code_ENTER)){
Log.v( - ,开始搜索);
如果(src.getText()长度()大于0){
信息搜索结果=新的ArrayList< Recepie>();
最后ProgressDialog进度= ProgressDialog.show(
TestSearch.this,
的getString(R.string.please_wait)
的getString(R.string.getting_search_results)
真正);
新的AsyncTask<虚空,虚空,虚空>(){
在preExecute保护无效(){
InputMethodManager IMM =(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(
src.getWindowToken(),0);
};
@覆盖
保护无效doInBackground(虚空...... PARAMS){
信息搜索结果= getSearchResults(src.getText()
的ToString());
返回null;
}
保护无效onPostExecute(无效的结果){
//设置屏幕,以便搜索片段
片段片段=新SideSearchFragment();
捆绑的args =新包();
args.putString(猫标题,src.getText()
的ToString());
args.putSerializable(猫,猫);
args.putBoolean(Constants.SEARCH,真正的);
args.putParcelableArrayList(
Constants.SEARCH_RESULTS,信息搜索结果);
fragment.setArguments(参数);
progress.dismiss();
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.content_frame,片段)
.addToBackStack(search_results)
。承诺();
//的setTitle(mPlanetTitles [位置]);
mDrawerLayout.closeDrawer(sideWrapper);
//清除搜索文本,并隐藏键盘
src.setText();
};
}。执行();
}
返回true;
}
返回false;
}
});
// mDrawerList.addHeaderView(头);
SideAdapter适配器=新SideAdapter(这一点,猫);
mDrawerList.setAdapter(适配器);
mDrawerList.setOnItemClickListener(新DrawerItemClickListener());
initActionBar();
//使动作条的应用程序图标,表现为动作切换导航抽屉
getActionBar()setDisplayHomeAsUpEnabled(假)。
getActionBar()setHomeButtonEnabled(真)。
如果(savedInstanceState == NULL){
showMainFragment();
}
}
/ *的点击听者中的ListView控件中的抽屉式导航* /
私有类DrawerItemClickListener工具
ListView.OnItemClickListener {
@覆盖
公共无效onItemClick(适配器视图<>母公司视图中查看,INT位置,
长ID){
选择信息(位置);
}
}
私人无效showFavoritesFragment(){
片段片段=新FavoritesFragment();
捆绑的args =新包();
args.putSerializable(全部,recepies);
args.putBoolean(搜索,真正的);
args.putSerializable(猫,猫);
fragment.setArguments(参数);
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.content_frame,片段)
。.addToBackStack(最爱)提交();
}
私人无效showMainFragment(){
片段片段=新MainFragment();
捆绑的args =新包();
args.putParcelableArrayList(全部,recepies);
args.putSerializable(猫,猫);
args.putParcelableArrayList(Constants.MAINRECEPIES,mainRecepies);
args.putBoolean(搜索,真正的);
fragment.setArguments(参数);
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()。增加(R.id.content_frame,片段)
。承诺();
}
@覆盖
公共布尔onOptionsItemSelected(菜单项项){
开关(item.getItemId()){
案例android.R.id.home:
如果(mDrawerLayout.isDrawerOpen(sideWrapper))
mDrawerLayout.closeDrawer(sideWrapper);
其他
mDrawerLayout.openDrawer(sideWrapper);
返回true;
案例R.id.main_action_fav:
showFavoritesFragment();
返回true;
案例R.id.main_action_choose_cats:
意向意图=新的意图(这一点,CircleListActivity.class);
intent.putExtra(Constants.DEVICES_EXTRA,设备);
intent.putExtra(猫,猫);
startActivity(意向);
// 完();
返回true;
默认:
返回super.onOptionsItemSelected(项目);
}
}
私人无效initActionBar(){
getActionBar()。setBackgroundDrawable(
新ColorDrawable(Color.parseColor(#e91a34)));
getActionBar()setCustomView(R.layout.actionbar_custom_view_home)。
// actionBar.setcu
getActionBar()setDisplayShowTitleEnabled(假)。
getActionBar()setDisplayShowCustomEnabled(真)。
getActionBar()setDisplayUseLogoEnabled(假)。
getActionBar()setDisplayShowHomeEnabled(真)。
getActionBar()setHomeButtonEnabled(假)。
getActionBar()的setIcon(R.drawable.menu)。
}
@覆盖
公共布尔onCreateOptionsMenu(功能菜单){
MenuInflater充气= getMenuInflater();
inflater.inflate(R.menu.main,菜单);
返回true;
}
私人无效选择信息(INT位置){
//更新替换片段的主要内容
片段片段=新SearchFragment();
捆绑的args =新包();
args.putString(猫标题,cats.get(位置).getTitle());
args.putInt(Constants.CATEGORY_ID,cats.get(位置).getId());
args.putSerializable(猫,猫);
fragment.setArguments(参数);
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.content_frame,片段).addToBackStack(空)
。承诺();
//更新所选项目和标题,然后关上抽屉
mDrawerList.setItemChecked(位置,真正的);
mDrawerList.setSelection(位置);
//的setTitle(mPlanetTitles [位置]);
mDrawerLayout.closeDrawer(sideWrapper);
}
@覆盖
公共无效的setTitle(CharSequence的标题){
// mTitle =称号;
// getActionBar()的setTitle(mTitle)。
}
/ **
*当使用ActionBarDrawerToggle,你必须在调用它
* onPostCreate()和onConfigurationChanged()...
* /
@覆盖
保护无效onPostCreate(包savedInstanceState){
super.onPostCreate(savedInstanceState);
//同步发生onRestoreInstanceState后切换状态。
// mDrawerToggle.syncState();
}
@覆盖
公共无效onConfigurationChanged(配置NEWCONFIG){
super.onConfigurationChanged(NEWCONFIG);
//传递任何配置更改抽屉toggls
// mDrawerToggle.onConfigurationChanged(NEWCONFIG);
}
//这个函数获取搜索结果
公众的ArrayList< Recepie> getSearchResults(字符串关键字){
ArrayList的< Recepie> resultRecepie =新的ArrayList< Recepie>();
JSONParser jParser =新JSONParser();
//从URL的JSON数据
JSONObject的jObj = jParser
.getJSONObjectFromUrl(http://oursson-recipes.outsourcingfarm.com/index.php/jsoner/getRecipe?query=
+关键字);
尝试 {
JSONArray withTechnics = jObj.getJSONArray(with_technics);
JSONArray withoutTechnics = jObj.getJSONArray(without_technics);
的for(int i = 0; I< withTechnics.length();我++){
的JSONObject与= withTechnics.getJSONObject(我);
布尔bool_tehcnics = TRUE;
如果(with.getInt(with_technics)== 1)
bool_tehcnics = FALSE;
布尔my_devices = FALSE;
如果(with.getInt(mydevices)== 1)
my_devices = TRUE;
JSONArray设备= with.getJSONArray(设备);
ArrayList的<整数GT; devicesIDs =新的ArrayList<整数GT;();
对于(INT K = 0; K< devices.length(); k ++){
devicesIDs.add(Integer.valueOf(devices.getString(k))的);
}
resultRecepie.add(新Recepie(bool_tehcnics,my_devices,与
.getInt(ID),with.getInt(人),以
.getString(标题),
with.getString(preparation_time),用
.getString(IMAGE_1),devicesIDs));
}
的for(int i = 0; I< withoutTechnics.length();我++){
的JSONObject与= withoutTechnics.getJSONObject(我);
布尔bool_tehcnics = FALSE;
如果(with.getInt(with_technics)== 1)
bool_tehcnics = TRUE;
布尔my_devices = FALSE;
如果(with.getInt(mydevices)== 1)
my_devices = TRUE;
JSONArray设备= with.getJSONArray(设备);
ArrayList的<整数GT; devicesIDs =新的ArrayList<整数GT;();
对于(INT K = 0; K< devices.length(); k ++){
devicesIDs.add(Integer.valueOf(devices.getString(k))的);
}
resultRecepie.add(新Recepie(bool_tehcnics,my_devices,与
.getInt(ID),with.getInt(人),以
.getString(标题),
with.getString(preparation_time),用
.getString(IMAGE_1),devicesIDs));
}
}赶上(JSONException E){
e.printStackTrace();
}
返回resultRecepie;
}
}
这里是一个片段(这个我开始在开始时):
公共类MainFragment扩展片段{
私人MainPhoneAdapter适配器;
私人布尔搜索= FALSE;
私人的ArrayList<类别>猫;
私人的ArrayList< Recepie> recepies,mainRecepies;
私人的ArrayList< Recepie> withoTechnics;
私人的ArrayList< Recepie> wTechnics;
共享preferences preFS;
ImageView的介绍;
ProgressDialog对话框;
动画animationFadeOut;
显示屏显示;
公共MainFragment(){
需要片段子类//空构造
}
@覆盖
公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,
捆绑savedInstanceState){
查看rootView = inflater.inflate(R.layout.main,集装箱,假);
Log.v( - ,主创建);
如果(Constants.isTablet(getActivity())){
getActivity()。setRequestedOrientation(
ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
} 其他 {
getActivity()。setRequestedOrientation(
ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
猫=(ArrayList的<类别>)getArguments()getSerializable(猫)。
搜索= getArguments()getBoolean(搜索)。
窗口管理WM =(窗口管理器)getActivity()。getSystemService(
Context.WINDOW_SERVICE);
显示器= wm.getDefaultDisplay();
preFS = getActivity()。getShared preferences(
。getActivity()getPackageName(),Context.MODE_PRIVATE);
initActionBar();
mainRecepies =新的ArrayList< Recepie>();
recepies =新的ArrayList< Recepie>();
wTechnics =新的ArrayList< Recepie>();
withoTechnics =新的ArrayList< Recepie>();
对话框=新ProgressDialog(getActivity());
如果(getActivity()。getIntent()。getBooleanExtra(fromDevs,FALSE)
&功放;&安培;搜索) {
dialog.setMessage(中..);
dialog.show();
parseJson(rootView);
} 其他 {
recepies = getArguments()getParcelableArrayList(全)。
mainRecepies = getArguments()。getParcelableArrayList(
Constants.MAINRECEPIES);
Log.v( - ,mainRecepies.size()+主要尺寸);
// Log.v( - ,主断枝+ recepies.size());
的for(int i = 0; I< mainRecepies.size();我++){
如果(mainRecepies.get(ⅰ).isWithTechnics())
wTechnics.add(mainRecepies.get(ⅰ));
其他
withoTechnics.add(mainRecepies.get(ⅰ));
}
如果(Constants.isTablet(getActivity())){
populateViewForTablet(rootView);
populateWithouTechnicsTablet(rootView);
showAnimation(rootView);
} 其他
populateForPhones(rootView);
}
initImgLoader();
返回rootView;
}
私人无效initActionBar(){
getActivity()。getActionBar()。setBackgroundDrawable(
新ColorDrawable(Color.parseColor(#e91a34)));
getActivity()。getActionBar()。setCustomView(
R.layout.actionbar_custom_view_home);
// actionBar.setcu
。getActivity()getActionBar()setDisplayShowTitleEnabled(假)。
。getActivity()getActionBar()setDisplayShowCustomEnabled(真)。
。getActivity()getActionBar()setDisplayUseLogoEnabled(假)。
。getActivity()getActionBar()setDisplayShowHomeEnabled(真)。
getActivity()。getActionBar()。setNavigationMode(
ActionBar.NAVIGATION_MODE_STANDARD);
// getActivity()getActionBar()setHomeButtonEnabled(假)。
// getActivity()getActionBar()的setIcon(R.drawable.menu)。
}
私人无效initImgLoader(){
ImageLoader.getInstance()。INIT(
ImageLoaderConfiguration.createDefault(getActivity()));
}
私人无效showAnimation(查看视图){
如果(!prefs.getBoolean(main_shown,FALSE)){
prefs.edit()putBoolean(main_shown,真).commit()。
startActivity(新意图(getActivity(),Overlay.class));
getActivity()。overridePendingTransition(R.anim.fadein,
R.anim.fadeout);
}
}
私人无效parseJson(最终查看rootView){
新的AsyncTask<虚空,虚空,虚空>(){
@覆盖
保护无效doInBackground(虚空...... PARAMS){
//从preFS设备ID的添加静态IDS
字符串开发者[] = prefs.getString(Constants.SELECTED_DEVICES,
1,23,4)分裂()。
INT端= devs.length;
HashSet的<整数GT;设置=新的HashSet<整数GT;();
的for(int i = 0; I<结束;我++){
set.add(Integer.valueOf(开发者[I]));
}
字符串devicesUrl =;
迭代器<整数GT;它= set.iterator();
而(it.hasNext()){
devicesUrl + = it.next()+,;
}
devicesUrl = devicesUrl.substring(0,devicesUrl.length() - 1);
Log.v( - ,devicesUrl +DV);
getJSONFromUrl(http://oursson-recipes.outsourcingfarm.com/index.php/jsoner/getRecipe?devices=
+ devicesUrl);
getMainScreenRecepies(http://oursson-recipes.outsourcingfarm.com/index.php/jsoner/getHome?devices=
+ devicesUrl);
返回null;
}
保护无效onPostExecute(无效的结果){
//如果TABLET
如果(Constants.isTablet(getActivity())){
populateViewForTablet(rootView);
populateWithouTechnicsTablet(rootView);
showAnimation(rootView);
} 其他
populateForPhones(rootView);
};
}。执行();
}
私人无效populateForPhones(查看rootView){
ListView控件的GridView =(ListView控件)rootView.findViewById(R.id.main_grid);
适配器=新MainPhoneAdapter(getActivity(),wTechnics);
gridView.setAdapter(适配器);
如果(dialog.isShowing())
dialog.dismiss();
gridView.setOnItemClickListener(新OnItemClickListener(){
@覆盖
公共无效onItemClick(适配器视图<>母公司视图中查看,
INT位置,长的id){
意向意图=新的意图(getActivity()
FullScreenBanner.class);
intent.putExtra(猫,猫);
intent.putExtra(Constants.RECEPIES_EXTRA,
wTechnics.get(位置));
intent.putExtra(fromMain,真正的);
intent.putExtra(recepieExtra,wTechnics.get(位置).getId());
startActivity(意向);
}
});
}
私人无效populateViewForTablet(查看rootView){
withTest(rootView);
}
//
私人无效withTest(查看rootView){
的LinearLayout父=((的LinearLayout)rootView
.findViewById(R.id.main_parent));
点大小=新的点();
display.getSize(大小);
。INT宽度= getActivity()getResources()getDisplayMetrics()widthPixels。
INT身高=(。getActivity()getResources()getDisplayMetrics()heightPixels / 3)* 2;
的for(int i = 0;我3;;我++){
最终诠释位置= I;
查看R = LayoutInflater.from(getActivity())。膨胀(
R.layout.main_item_gray,父母,假);
TextView的顶部=(TextView的)R
.findViewById(R.id.main_item_oursson_or_not);
top.setBackgroundResource(R.drawable.redtint);
ImageView的itemBg =(ImageView的)r.findViewById(R.id.main_item_img);
TextView的标题=(TextView中)r.findViewById(R.id.main_item_title);
TextView的时间=(TextView中)r.findViewById(R.id.main_item_time);
TextView的人=(TextView的)R
.findViewById(R.id.main_item_persons);
串imgLink = wTechnics.get(ⅰ).getImage_1()
.replace([高度],高度+)
.replace([宽度]宽/ 3 +);
title.setText(wTechnics.get(ⅰ).getTitle());
time.setText(wTechnics.get(ⅰ)。获得preparation_time());
persons.setText(wTechnics.get(ⅰ).getPersons()+);
。ImageLoader.getInstance()displayImage(imgLink,itemBg);
parent.addView(r)的;
如果(dialog.isShowing())
dialog.dismiss();
r.setOnClickListener(新OnClickListener(){
@覆盖
公共无效的onClick(视图v){
意向意图=新的意图(getActivity()
FullScreenBanner.class);
intent.putExtra(Constants.RECEPIES_EXTRA,
wTechnics.get(位置));
intent.putExtra(猫,猫);
intent.putExtra(recepieExtra,wTechnics.get(位置)
.getId());
startActivity(意向);
}
});
}
}
//
私人无效populateWithouTechnicsTablet(查看rootView){
的LinearLayout父=((的LinearLayout)rootView
.findViewById(R.id.main_without));
点大小=新的点();
display.getSize(大小);
。INT宽度= getActivity()getResources()getDisplayMetrics()widthPixels。
INT高= getActivity()getResources()getDisplayMetrics()heightPixels / 2。;
的for(int i = 0;我4;;我++){
最终诠释位置= I;
查看R = LayoutInflater.from(getActivity())。膨胀(
R.layout.main_item_gray_notfeatured,父母,假);
ImageView的itemBg =(ImageView的)r.findViewById(R.id.main_item_img);
TextView的标题=(TextView中)r.findViewById(R.id.main_item_title);
TextView的时间=(TextView中)r.findViewById(R.id.main_item_time);
TextView的人=(TextView的)R
.findViewById(R.id.main_item_persons);
串imgLink = withoTechnics.get(ⅰ).getImage_1()
.replace([高度],高度+)
.replace([宽度]宽/ 4 +);
title.setText(withoTechnics.get(ⅰ).getTitle());
time.setText(withoTechnics.get(ⅰ)。获得preparation_time());
persons.setText(withoTechnics.get(ⅰ).getPersons()+);
。ImageLoader.getInstance()displayImage(imgLink,itemBg);
parent.addView(r)的;
如果(dialog.isShowing())
dialog.dismiss();
r.setOnClickListener(新OnClickListener(){
@覆盖
公共无效的onClick(视图v){
意向意图=新的意图(getActivity()
FullScreenBanner.class);
intent.putExtra(Constants.RECEPIES_EXTRA,
withoTechnics.get(位置));
intent.putExtra(猫,猫);
intent.putExtra(recepieExtra,withoTechnics.get(位置)
.getId());
startActivity(意向);
}
});
}
}
公共无效getMainScreenRecepies(字符串URL){
JSONParser jParser =新JSONParser();
//从URL的JSON数据
JSONObject的jObj = jParser.getJSONObjectFromUrl(URL);
尝试 {
JSONArray withTechnics = jObj.getJSONArray(with_technics);
JSONArray withoutTechnics = jObj.getJSONArray(without_technics);
的for(int i = 0; I< withTechnics.length();我++){
的JSONObject与= withTechnics.getJSONObject(我);
mainRecepies.add(新Recepie(真,with.getInt(ID),与
.getInt(人),with.getString(标题),与
.getString(preparation_time),用
.getString(IMAGE_1)));
}
的for(int i = 0; I< withoutTechnics.length();我++){
的JSONObject与= withoutTechnics.getJSONObject(我);
mainRecepies.add(新Recepie(假,with.getInt(ID),与
.getInt(人),with.getString(标题),与
.getString(preparation_time),用
.getString(IMAGE_1)));
}
Log.v( - ,解析的+ mainRecepies.size()++网址);
}赶上(JSONException E){
e.printStackTrace();
}
}
公共无效getJSONFromUrl(字符串URL){
JSONParser jParser =新JSONParser();
//从URL的JSON数据
JSONObject的jObj = jParser.getJSONObjectFromUrl(URL);
Log.v( - ,ASDS);
尝试 {
JSONArray withTechnics = jObj.getJSONArray(with_technics);
JSONArray withoutTechnics = jObj.getJSONArray(without_technics);
的for(int i = 0; I< withTechnics.length();我++){
的JSONObject与= withTechnics.getJSONObject(我);
布尔bool_tehcnics = FALSE;
如果(with.getInt(with_technics)== 1)
bool_tehcnics = TRUE;
布尔my_devices = FALSE;
如果(with.getInt(mydevices)== 1)
my_devices = TRUE;
JSONArray设备= with.getJSONArray(设备);
ArrayList的<整数GT; devicesIDs =新的ArrayList<整数GT;();
对于(INT K = 0; K< devices.length(); k ++){
devicesIDs.add(Integer.valueOf(devices.getString(k))的);
}
wTechnics.add(新Recepie(bool_tehcnics,my_devices,与
.getInt(ID),with.getInt(人),以
.getString(标题),
with.getString(preparation_time),用
.getString(IMAGE_1),devicesIDs));
recepies.add(新Recepie(bool_tehcnics,my_devices,与
.getInt(ID),with.getInt(人),以
.getString(标题),
with.getString(preparation_time),用
.getString(IMAGE_1),devicesIDs));
}
的for(int i = 0; I< withoutTechnics.length();我++){
的JSONObject与= withoutTechnics.getJSONObject(我);
布尔bool_tehcnics = FALSE;
如果(with.getInt(with_technics)== 1)
bool_tehcnics = TRUE;
布尔my_devices = FALSE;
如果(with.getInt(mydevices)== 1)
my_devices = TRUE;
JSONArray设备= with.getJSONArray(设备);
ArrayList的<整数GT; devicesIDs =新的ArrayList<整数GT;();
对于(INT K = 0; K< devices.length(); k ++){
devicesIDs.add(Integer.valueOf(devices.getString(k))的);
}
withoTechnics.add(新Recepie(bool_tehcnics,my_devices,与
.getInt(ID),with.getInt(人),以
.getString(标题),
with.getString(preparation_time),用
.getString(IMAGE_1),devicesIDs));
recepies.add(新Recepie(bool_tehcnics,my_devices,与
.getInt(ID),with.getInt(人),以
.getString(标题),
with.getString(preparation_time),用
.getString(IMAGE_1),devicesIDs));
}
Log.v( - ,recepies.size()+RS);
}赶上(JSONException E){
// TODO自动生成的catch块
e.printStackTrace();
}
}
}
也许使用newInstance方法将纠正这个出发的两个片段。诉诸公众我看到了很多的newInstance code,当我在看片段的例子。
公共类MainFragment扩展片段{
公共静态片段的newInstance(INT实例#传递receipieces和猫在这里){
MainFragment F =新MainFragment();
//供应NUM输入作为参数。
捆绑的args =新包();
args.putParcelableArrayList(全部,recepies);
args.putSerializable(猫,猫);
args.putParcelableArrayList(Constants.MAINRECEPIES,mainRecepies);
args.putBoolean(搜索,真正的);
fragment.setArguments(参数);
返回F;
}
同时回,显示该片段使用新的实例启动片段活动。
私人无效showMainFragment(){
片段片段= MainFragment.newInstacne(例如#传递recipies和猫在这里);
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()。增加(R.id.content_frame,片段)
。承诺();
}
I have this app in which im using fragments, but somehow everytime I start the app, every activity(fragment) is started two times. I'm banging my head about this but I cannot find an answer. Can someone help me?
Here is my main FragmentActivity:
public class TestSearch extends Activity {
//this activity starts a fragment
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
// private ActionBarDrawerToggle mDrawerToggle;
private ArrayList<Device> devices;
private ArrayList<Recepie> recepies, mainRecepies;
private ArrayList<Recepie> searchResult;
private LinearLayout sideWrapper;
private EditText src;
ArrayList<Categories> cats;
boolean isTablet = false;
@SuppressWarnings("unchecked")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test_search);
Log.v("--", "started");
if (Constants.isTablet(this)) {
isTablet = true;
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
} else {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
devices = new ArrayList<Device>();
recepies = new ArrayList<Recepie>();
mainRecepies = new ArrayList<Recepie>();
devices = (ArrayList<Device>) getIntent().getSerializableExtra(
Constants.DEVICES_EXTRA);
recepies = getIntent().getParcelableArrayListExtra("all");
mainRecepies = getIntent().getParcelableArrayListExtra(
Constants.MAINRECEPIES);
// mTitle = mDrawerTitle = getTitle();
cats = new ArrayList<Categories>();
cats = (ArrayList<Categories>) getIntent().getSerializableExtra("cats");
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.left_drawer);
sideWrapper = (LinearLayout) findViewById(R.id.listwraper);
// set a custom shadow that overlays the main content when the drawer
// opens
mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow,
GravityCompat.START);
// set up the drawer's list view with items and click listener
// View header = getLayoutInflater().inflate(R.layout.search_item,
// null);
src = (EditText) findViewById(R.id.search_est);
src.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
// If the event is a key-down event on the "enter" button
if ((event.getAction() == KeyEvent.ACTION_DOWN)
&& (keyCode == KeyEvent.KEYCODE_ENTER)) {
Log.v("--", "Start search");
if (src.getText().length() > 0) {
searchResult = new ArrayList<Recepie>();
final ProgressDialog progress = ProgressDialog.show(
TestSearch.this,
getString(R.string.please_wait),
getString(R.string.getting_search_results),
true);
new AsyncTask<Void, Void, Void>() {
protected void onPreExecute() {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(
src.getWindowToken(), 0);
};
@Override
protected Void doInBackground(Void... params) {
searchResult = getSearchResults(src.getText()
.toString());
return null;
}
protected void onPostExecute(Void result) {
// set screen to search fragment
Fragment fragment = new SideSearchFragment();
Bundle args = new Bundle();
args.putString("cat-title", src.getText()
.toString());
args.putSerializable("cats", cats);
args.putBoolean(Constants.SEARCH, true);
args.putParcelableArrayList(
Constants.SEARCH_RESULTS, searchResult);
fragment.setArguments(args);
progress.dismiss();
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.content_frame, fragment)
.addToBackStack("search_results")
.commit();
// setTitle(mPlanetTitles[position]);
mDrawerLayout.closeDrawer(sideWrapper);
// clear search text and hide keyboard
src.setText("");
};
}.execute();
}
return true;
}
return false;
}
});
// mDrawerList.addHeaderView(header);
SideAdapter adapter = new SideAdapter(this, cats);
mDrawerList.setAdapter(adapter);
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
initActionBar();
// enable ActionBar app icon to behave as action to toggle nav drawer
getActionBar().setDisplayHomeAsUpEnabled(false);
getActionBar().setHomeButtonEnabled(true);
if (savedInstanceState == null) {
showMainFragment();
}
}
/* The click listner for ListView in the navigation drawer */
private class DrawerItemClickListener implements
ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
selectItem(position);
}
}
private void showFavoritesFragment() {
Fragment fragment = new FavoritesFragment();
Bundle args = new Bundle();
args.putSerializable("all", recepies);
args.putBoolean("search", true);
args.putSerializable("cats", cats);
fragment.setArguments(args);
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.content_frame, fragment)
.addToBackStack("favorites").commit();
}
private void showMainFragment() {
Fragment fragment = new MainFragment();
Bundle args = new Bundle();
args.putParcelableArrayList("all", recepies);
args.putSerializable("cats", cats);
args.putParcelableArrayList(Constants.MAINRECEPIES, mainRecepies);
args.putBoolean("search", true);
fragment.setArguments(args);
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().add(R.id.content_frame, fragment)
.commit();
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
if (mDrawerLayout.isDrawerOpen(sideWrapper))
mDrawerLayout.closeDrawer(sideWrapper);
else
mDrawerLayout.openDrawer(sideWrapper);
return true;
case R.id.main_action_fav:
showFavoritesFragment();
return true;
case R.id.main_action_choose_cats:
Intent intent = new Intent(this, CircleListActivity.class);
intent.putExtra(Constants.DEVICES_EXTRA, devices);
intent.putExtra("cats", cats);
startActivity(intent);
// finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void initActionBar() {
getActionBar().setBackgroundDrawable(
new ColorDrawable(Color.parseColor("#e91a34")));
getActionBar().setCustomView(R.layout.actionbar_custom_view_home);
// actionBar.setcu
getActionBar().setDisplayShowTitleEnabled(false);
getActionBar().setDisplayShowCustomEnabled(true);
getActionBar().setDisplayUseLogoEnabled(false);
getActionBar().setDisplayShowHomeEnabled(true);
getActionBar().setHomeButtonEnabled(false);
getActionBar().setIcon(R.drawable.menu);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return true;
}
private void selectItem(int position) {
// update the main content by replacing fragments
Fragment fragment = new SearchFragment();
Bundle args = new Bundle();
args.putString("cat-title", cats.get(position).getTitle());
args.putInt(Constants.CATEGORY_ID, cats.get(position).getId());
args.putSerializable("cats", cats);
fragment.setArguments(args);
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.content_frame, fragment).addToBackStack(null)
.commit();
// update selected item and title, then close the drawer
mDrawerList.setItemChecked(position, true);
mDrawerList.setSelection(position);
// setTitle(mPlanetTitles[position]);
mDrawerLayout.closeDrawer(sideWrapper);
}
@Override
public void setTitle(CharSequence title) {
// mTitle = title;
// getActionBar().setTitle(mTitle);
}
/**
* When using the ActionBarDrawerToggle, you must call it during
* onPostCreate() and onConfigurationChanged()...
*/
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
// mDrawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Pass any configuration change to the drawer toggls
// mDrawerToggle.onConfigurationChanged(newConfig);
}
// This function gets the search results
public ArrayList<Recepie> getSearchResults(String keyword) {
ArrayList<Recepie> resultRecepie = new ArrayList<Recepie>();
JSONParser jParser = new JSONParser();
// get JSON data from URL
JSONObject jObj = jParser
.getJSONObjectFromUrl("http://oursson-recipes.outsourcingfarm.com/index.php/jsoner/getRecipe?query="
+ keyword);
try {
JSONArray withTechnics = jObj.getJSONArray("with_technics");
JSONArray withoutTechnics = jObj.getJSONArray("without_technics");
for (int i = 0; i < withTechnics.length(); i++) {
JSONObject with = withTechnics.getJSONObject(i);
boolean bool_tehcnics = true;
if (with.getInt("with_technics") == 1)
bool_tehcnics = false;
boolean my_devices = false;
if (with.getInt("mydevices") == 1)
my_devices = true;
JSONArray devices = with.getJSONArray("devices");
ArrayList<Integer> devicesIDs = new ArrayList<Integer>();
for (int k = 0; k < devices.length(); k++) {
devicesIDs.add(Integer.valueOf(devices.getString(k)));
}
resultRecepie.add(new Recepie(bool_tehcnics, my_devices, with
.getInt("id"), with.getInt("persons"), with
.getString("title"),
with.getString("preparation_time"), with
.getString("image_1"), devicesIDs));
}
for (int i = 0; i < withoutTechnics.length(); i++) {
JSONObject with = withoutTechnics.getJSONObject(i);
boolean bool_tehcnics = false;
if (with.getInt("with_technics") == 1)
bool_tehcnics = true;
boolean my_devices = false;
if (with.getInt("mydevices") == 1)
my_devices = true;
JSONArray devices = with.getJSONArray("devices");
ArrayList<Integer> devicesIDs = new ArrayList<Integer>();
for (int k = 0; k < devices.length(); k++) {
devicesIDs.add(Integer.valueOf(devices.getString(k)));
}
resultRecepie.add(new Recepie(bool_tehcnics, my_devices, with
.getInt("id"), with.getInt("persons"), with
.getString("title"),
with.getString("preparation_time"), with
.getString("image_1"), devicesIDs));
}
} catch (JSONException e) {
e.printStackTrace();
}
return resultRecepie;
}
}
And here is a fragment(this one I start at the begining):
public class MainFragment extends Fragment {
private MainPhoneAdapter adapter;
private boolean search = false;
private ArrayList<Categories> cats;
private ArrayList<Recepie> recepies, mainRecepies;
private ArrayList<Recepie> withoTechnics;
private ArrayList<Recepie> wTechnics;
SharedPreferences prefs;
ImageView intro;
ProgressDialog dialog;
Animation animationFadeOut;
Display display;
public MainFragment() {
// Empty constructor required for fragment subclasses
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.main, container, false);
Log.v("--", "main create");
if (Constants.isTablet(getActivity())) {
getActivity().setRequestedOrientation(
ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
} else {
getActivity().setRequestedOrientation(
ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
cats = (ArrayList<Categories>) getArguments().getSerializable("cats");
search = getArguments().getBoolean("search");
WindowManager wm = (WindowManager) getActivity().getSystemService(
Context.WINDOW_SERVICE);
display = wm.getDefaultDisplay();
prefs = getActivity().getSharedPreferences(
getActivity().getPackageName(), Context.MODE_PRIVATE);
initActionBar();
mainRecepies = new ArrayList<Recepie>();
recepies = new ArrayList<Recepie>();
wTechnics = new ArrayList<Recepie>();
withoTechnics = new ArrayList<Recepie>();
dialog = new ProgressDialog(getActivity());
if (getActivity().getIntent().getBooleanExtra("fromDevs", false)
&& search) {
dialog.setMessage("Loading..");
dialog.show();
parseJson(rootView);
} else {
recepies = getArguments().getParcelableArrayList("all");
mainRecepies = getArguments().getParcelableArrayList(
Constants.MAINRECEPIES);
Log.v("--", mainRecepies.size() + " MAiN SIZE");
// Log.v("--", "main frag" + recepies.size());
for (int i = 0; i < mainRecepies.size(); i++) {
if (mainRecepies.get(i).isWithTechnics())
wTechnics.add(mainRecepies.get(i));
else
withoTechnics.add(mainRecepies.get(i));
}
if (Constants.isTablet(getActivity())) {
populateViewForTablet(rootView);
populateWithouTechnicsTablet(rootView);
showAnimation(rootView);
} else
populateForPhones(rootView);
}
initImgLoader();
return rootView;
}
private void initActionBar() {
getActivity().getActionBar().setBackgroundDrawable(
new ColorDrawable(Color.parseColor("#e91a34")));
getActivity().getActionBar().setCustomView(
R.layout.actionbar_custom_view_home);
// actionBar.setcu
getActivity().getActionBar().setDisplayShowTitleEnabled(false);
getActivity().getActionBar().setDisplayShowCustomEnabled(true);
getActivity().getActionBar().setDisplayUseLogoEnabled(false);
getActivity().getActionBar().setDisplayShowHomeEnabled(true);
getActivity().getActionBar().setNavigationMode(
ActionBar.NAVIGATION_MODE_STANDARD);
// getActivity().getActionBar().setHomeButtonEnabled(false);
// getActivity().getActionBar().setIcon(R.drawable.menu);
}
private void initImgLoader() {
ImageLoader.getInstance().init(
ImageLoaderConfiguration.createDefault(getActivity()));
}
private void showAnimation(View view) {
if (!prefs.getBoolean("main_shown", false)) {
prefs.edit().putBoolean("main_shown", true).commit();
startActivity(new Intent(getActivity(), Overlay.class));
getActivity().overridePendingTransition(R.anim.fadein,
R.anim.fadeout);
}
}
private void parseJson(final View rootView) {
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... params) {
// add static ids from prefs as device ID's
String devs[] = prefs.getString(Constants.SELECTED_DEVICES,
"1,23,4").split(",");
int end = devs.length;
HashSet<Integer> set = new HashSet<Integer>();
for (int i = 0; i < end; i++) {
set.add(Integer.valueOf(devs[i]));
}
String devicesUrl = "";
Iterator<Integer> it = set.iterator();
while (it.hasNext()) {
devicesUrl += it.next() + ",";
}
devicesUrl = devicesUrl.substring(0, devicesUrl.length() - 1);
Log.v("--", devicesUrl + " DV");
getJSONFromUrl("http://oursson-recipes.outsourcingfarm.com/index.php/jsoner/getRecipe?devices="
+ devicesUrl);
getMainScreenRecepies("http://oursson-recipes.outsourcingfarm.com/index.php/jsoner/getHome?devices="
+ devicesUrl);
return null;
}
protected void onPostExecute(Void result) {
// if TABLET
if (Constants.isTablet(getActivity())) {
populateViewForTablet(rootView);
populateWithouTechnicsTablet(rootView);
showAnimation(rootView);
} else
populateForPhones(rootView);
};
}.execute();
}
private void populateForPhones(View rootView) {
ListView gridView = (ListView) rootView.findViewById(R.id.main_grid);
adapter = new MainPhoneAdapter(getActivity(), wTechnics);
gridView.setAdapter(adapter);
if (dialog.isShowing())
dialog.dismiss();
gridView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent intent = new Intent(getActivity(),
FullScreenBanner.class);
intent.putExtra("cats", cats);
intent.putExtra(Constants.RECEPIES_EXTRA,
wTechnics.get(position));
intent.putExtra("fromMain", true);
intent.putExtra("recepieExtra", wTechnics.get(position).getId());
startActivity(intent);
}
});
}
private void populateViewForTablet(View rootView) {
withTest(rootView);
}
//
private void withTest(View rootView) {
LinearLayout parent = ((LinearLayout) rootView
.findViewById(R.id.main_parent));
Point size = new Point();
display.getSize(size);
int width = getActivity().getResources().getDisplayMetrics().widthPixels;
int height = (getActivity().getResources().getDisplayMetrics().heightPixels / 3) * 2;
for (int i = 0; i < 3; i++) {
final int position = i;
View r = LayoutInflater.from(getActivity()).inflate(
R.layout.main_item_gray, parent, false);
TextView top = (TextView) r
.findViewById(R.id.main_item_oursson_or_not);
top.setBackgroundResource(R.drawable.redtint);
ImageView itemBg = (ImageView) r.findViewById(R.id.main_item_img);
TextView title = (TextView) r.findViewById(R.id.main_item_title);
TextView time = (TextView) r.findViewById(R.id.main_item_time);
TextView persons = (TextView) r
.findViewById(R.id.main_item_persons);
String imgLink = wTechnics.get(i).getImage_1()
.replace("[HEIGHT]", height + "")
.replace("[WIDTH]", width / 3 + "");
title.setText(wTechnics.get(i).getTitle());
time.setText(wTechnics.get(i).getPreparation_time());
persons.setText(wTechnics.get(i).getPersons() + "");
ImageLoader.getInstance().displayImage(imgLink, itemBg);
parent.addView(r);
if (dialog.isShowing())
dialog.dismiss();
r.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(),
FullScreenBanner.class);
intent.putExtra(Constants.RECEPIES_EXTRA,
wTechnics.get(position));
intent.putExtra("cats", cats);
intent.putExtra("recepieExtra", wTechnics.get(position)
.getId());
startActivity(intent);
}
});
}
}
//
private void populateWithouTechnicsTablet(View rootView) {
LinearLayout parent = ((LinearLayout) rootView
.findViewById(R.id.main_without));
Point size = new Point();
display.getSize(size);
int width = getActivity().getResources().getDisplayMetrics().widthPixels;
int height = getActivity().getResources().getDisplayMetrics().heightPixels / 2;
for (int i = 0; i < 4; i++) {
final int position = i;
View r = LayoutInflater.from(getActivity()).inflate(
R.layout.main_item_gray_notfeatured, parent, false);
ImageView itemBg = (ImageView) r.findViewById(R.id.main_item_img);
TextView title = (TextView) r.findViewById(R.id.main_item_title);
TextView time = (TextView) r.findViewById(R.id.main_item_time);
TextView persons = (TextView) r
.findViewById(R.id.main_item_persons);
String imgLink = withoTechnics.get(i).getImage_1()
.replace("[HEIGHT]", height + "")
.replace("[WIDTH]", width / 4 + "");
title.setText(withoTechnics.get(i).getTitle());
time.setText(withoTechnics.get(i).getPreparation_time());
persons.setText(withoTechnics.get(i).getPersons() + "");
ImageLoader.getInstance().displayImage(imgLink, itemBg);
parent.addView(r);
if (dialog.isShowing())
dialog.dismiss();
r.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(),
FullScreenBanner.class);
intent.putExtra(Constants.RECEPIES_EXTRA,
withoTechnics.get(position));
intent.putExtra("cats", cats);
intent.putExtra("recepieExtra", withoTechnics.get(position)
.getId());
startActivity(intent);
}
});
}
}
public void getMainScreenRecepies(String url) {
JSONParser jParser = new JSONParser();
// get JSON data from URL
JSONObject jObj = jParser.getJSONObjectFromUrl(url);
try {
JSONArray withTechnics = jObj.getJSONArray("with_technics");
JSONArray withoutTechnics = jObj.getJSONArray("without_technics");
for (int i = 0; i < withTechnics.length(); i++) {
JSONObject with = withTechnics.getJSONObject(i);
mainRecepies.add(new Recepie(true, with.getInt("id"), with
.getInt("persons"), with.getString("title"), with
.getString("preparation_time"), with
.getString("image_1")));
}
for (int i = 0; i < withoutTechnics.length(); i++) {
JSONObject with = withoutTechnics.getJSONObject(i);
mainRecepies.add(new Recepie(false, with.getInt("id"), with
.getInt("persons"), with.getString("title"), with
.getString("preparation_time"), with
.getString("image_1")));
}
Log.v("--", "PARSED " + mainRecepies.size() + " " + url);
} catch (JSONException e) {
e.printStackTrace();
}
}
public void getJSONFromUrl(String url) {
JSONParser jParser = new JSONParser();
// get JSON data from URL
JSONObject jObj = jParser.getJSONObjectFromUrl(url);
Log.v("--", "asds");
try {
JSONArray withTechnics = jObj.getJSONArray("with_technics");
JSONArray withoutTechnics = jObj.getJSONArray("without_technics");
for (int i = 0; i < withTechnics.length(); i++) {
JSONObject with = withTechnics.getJSONObject(i);
boolean bool_tehcnics = false;
if (with.getInt("with_technics") == 1)
bool_tehcnics = true;
boolean my_devices = false;
if (with.getInt("mydevices") == 1)
my_devices = true;
JSONArray devices = with.getJSONArray("devices");
ArrayList<Integer> devicesIDs = new ArrayList<Integer>();
for (int k = 0; k < devices.length(); k++) {
devicesIDs.add(Integer.valueOf(devices.getString(k)));
}
wTechnics.add(new Recepie(bool_tehcnics, my_devices, with
.getInt("id"), with.getInt("persons"), with
.getString("title"),
with.getString("preparation_time"), with
.getString("image_1"), devicesIDs));
recepies.add(new Recepie(bool_tehcnics, my_devices, with
.getInt("id"), with.getInt("persons"), with
.getString("title"),
with.getString("preparation_time"), with
.getString("image_1"), devicesIDs));
}
for (int i = 0; i < withoutTechnics.length(); i++) {
JSONObject with = withoutTechnics.getJSONObject(i);
boolean bool_tehcnics = false;
if (with.getInt("with_technics") == 1)
bool_tehcnics = true;
boolean my_devices = false;
if (with.getInt("mydevices") == 1)
my_devices = true;
JSONArray devices = with.getJSONArray("devices");
ArrayList<Integer> devicesIDs = new ArrayList<Integer>();
for (int k = 0; k < devices.length(); k++) {
devicesIDs.add(Integer.valueOf(devices.getString(k)));
}
withoTechnics.add(new Recepie(bool_tehcnics, my_devices, with
.getInt("id"), with.getInt("persons"), with
.getString("title"),
with.getString("preparation_time"), with
.getString("image_1"), devicesIDs));
recepies.add(new Recepie(bool_tehcnics, my_devices, with
.getInt("id"), with.getInt("persons"), with
.getString("title"),
with.getString("preparation_time"), with
.getString("image_1"), devicesIDs));
}
Log.v("--", recepies.size() + " RS");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Maybe using a newInstance method will correct this starting of two fragments. Ad populum I see alot of newInstance code when I'm looking at fragment examples.
public class MainFragment extends Fragment {
public static Fragment newInstance(int instance# pass in receipieces and cats here) {
MainFragment f = new MainFragment();
// Supply num input as an argument.
Bundle args = new Bundle();
args.putParcelableArrayList("all", recepies);
args.putSerializable("cats", cats);
args.putParcelableArrayList(Constants.MAINRECEPIES, mainRecepies);
args.putBoolean("search", true);
fragment.setArguments(args);
return f;
}
meanwhile back in the activity that shows the fragment use new instance to start the fragment.
private void showMainFragment() {
Fragment fragment = MainFragment.newInstacne(instance# pass in recipies and cats here);
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().add(R.id.content_frame, fragment)
.commit();
}
这篇关于启动两次碎片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!