本文介绍了onback pressed数据没有显示在片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果我通过我的应用程序就像F1我使用的抽屉式导航在我的应用程序,我有一个像F1,F2片段等on..and在每一个片段,我解析数据,并显示在列表视图,一切工作正常> F2> F3等on..but的问题是,如果我从F3到F2,这是我在我的F2片段中的数据没有显示,而只显示空白页,什么是问题?任何一个可以帮助?
我的F1
公共类Categriesdrawers扩展片段{
私人ImageView的drwr;
私人SlidingDrawer sldrwr;
私人ProgressDialog pDialog;
JSONArray所属分类= NULL;
私人的ListView列表视图;
私人的ArrayList< HashMap的<字符串,字符串>>一个列表;
私有静态字符串INTEREST_ACCEPT_URL =?HTTP:ashx的行动=类;
私有静态最后弦乐INTEREST_ACCEPT =所属分类;
私有静态最后弦乐INTERESTACCEPT_USER_ID =ID;
私有静态最后弦乐INTEREST_ACCEPT_NAME =类别名;
私人CustomAdapterCatagory适配器;
私人TextView的noacpt;
私人的ListView catlist;
@燮pressWarnings(德precation)
@覆盖
公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,
捆绑savedInstanceState){
视图V = inflater.inflate(R.layout.tests,集装箱,假);
drwr =(ImageView的)v.findViewById(R.id.handle);
sldrwr =(SlidingDrawer)v.findViewById(R.id.bottom);
sldrwr.open();
新LoadAlbums()执行();
sldrwr.setOnDrawerOpenListener(新OnDrawerOpenListener(){
@覆盖
公共无效onDrawerOpened(){
新LoadAlbums()执行();
}
});
catlist =(ListView控件)v.findViewById(R.id.substitute_list);
catlist.setOnItemClickListener(新OnItemClickListener(){
@覆盖
公共无效onItemClick(适配器视图<>母公司视图中查看,
INT位置,长的id){
组织TF =新的组织();
束束=新包();
tf.setArguments(包);
FragmentManager FM = getFragmentManager();
FragmentTransaction英尺= fm.beginTransaction();
ft.replace(R.id.mainContent,TF);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.addToBackStack(空);
ft.commit();
}
});
返回伏;
}
类LoadAlbums扩展的AsyncTask<字符串,字符串,ArrayList的< HashMap的<字符串,字符串>>> {
/ **
*在启动后台线程显示进度对话框
* * /
@覆盖
在preExecute保护无效(){
super.on preExecute();
pDialog =新ProgressDialog(Categriesdrawers.this.getActivity());
pDialog.setMessage(载入中...);
pDialog.setIndeterminate(真正的);
pDialog.setIndeterminateDrawable(getResources()getDrawable(R.drawable.custom_progress));
pDialog.setCancelable(假);
pDialog.show();
}
受保护的ArrayList< HashMap的<字符串,字符串>> doInBackground(字符串参数... args){
ServiceHandler SH =新ServiceHandler();
//制作一个请求URL并得到响应
ArrayList的< HashMap的<字符串,字符串>>数据=新的ArrayList< HashMap的<字符串,字符串>>();
字符串jsonStr = sh.makeServiceCall(INTEREST_ACCEPT_URL,ServiceHandler.GET);
Log.d(回应:,>中+ jsonStr);
如果(jsonStr!= NULL){
尝试 {
JSONObject的jsonObj =新的JSONObject(jsonStr);
//获取JSON数组节点
所属分类= jsonObj.getJSONArray(INTEREST_ACCEPT);
的for(int i = 0; I< categorylist.length();我++){
JSONObject的C = categorylist.getJSONObject(我);
//创建新的HashMap
HashMap的<字符串,字符串>图=新的HashMap<字符串,字符串>();
//添加每个子节点HashMap中的key =>值
map.put(INTERESTACCEPT_USER_ID,c.getString(INTERESTACCEPT_USER_ID));
map.put(INTEREST_ACCEPT_NAME,c.getString(INTEREST_ACCEPT_NAME));
//添加HashList到ArrayList中
data.add(图)
}
}赶上(JSONException E){
e.printStackTrace();
}
} 其他 {
Log.e(ServiceHandler,无法从URL中得到任何数据);
}
返回的数据;
}
保护无效onPostExecute(ArrayList中< HashMap的<字符串,字符串>>的结果){
super.onPostExecute(结果);
让所有专辑后//关闭该对话框
如果(pDialog.isShowing())
pDialog.dismiss();
//从后台线程更新UI
如果(ALIST == NULL){
ALIST =新的ArrayList< HashMap的<字符串,字符串>>();
aList.addAll(结果);
适配器=新CustomAdapterCatagory(getActivity(),结果);
catlist.setAdapter(适配器);
}其他{
aList.addAll(结果);
adapter.notifyDataSetChanged();
}
}
}
MainActivity
公共类MainActivity延伸活动{
ListView控件mDrawerList;
RelativeLayout的mDrawerPane;
私人ActionBarDrawerToggle mDrawerToggle;
私人DrawerLayout mDrawerLayout;
私人的CharSequence mTitle;
ArrayList的<的NavItem> mNavItems =新的ArrayList<的NavItem>();
私人的CharSequence mDrawerTitle;
动作条动作条;
@覆盖
保护无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main);
动作条= getActionBar();
actionBar.setBackgroundDrawable(新ColorDrawable(Color.argb(255,235,139,36)));
mTitle = mDrawerTitle =的getTitle();
mNavItems.add(新的NavItem(所有邮报,R.drawable.allpost));
mNavItems.add(新的NavItem(类别,R.drawable.allpost));
mNavItems.add(新的NavItem(所有博客,R.drawable.allpost));
mNavItems.add(新的NavItem(关于我们,R.drawable.about));
mNavItems.add(新的NavItem(率美国,R.drawable.rates));
// DrawerLayout
mDrawerLayout =(DrawerLayout)findViewById(R.id.drawerLayout);
//填充Navigtion抽屉选项
mDrawerPane =(RelativeLayout的)findViewById(R.id.drawerPane);
mDrawerList =(ListView控件)findViewById(R.id.navList);
DrawerListAdapter适配器=新DrawerListAdapter(这一点,mNavItems);
mDrawerList.setAdapter(适配器);
mDrawerList.setOnItemClickListener(新OnItemClickListener(){
@覆盖
公共无效onItemClick(适配器视图<>为arg0,查看ARG1,INT位置,
长ARG3){
selectItemFromDrawer(位置);
}
});
getActionBar()setDisplayHomeAsUpEnabled(真)。
mDrawerToggle =新ActionBarDrawerToggle(这一点,mDrawerLayout,
R.drawable.ic_view,//导航菜单切换图标
R.string.app_name,//导航抽屉打开 - 说明访问
R.string.app_name //导航抽屉关闭 - 说明访问
){
公共无效onDrawerClosed(查看视图){
。getActionBar()的setTitle(mTitle);
//调用prepareOptionsMenu()来显示操作栏图标
invalidateOptionsMenu();
}
公共无效onDrawerOpened(查看drawerView){
。getActionBar()的setTitle(mDrawerTitle);
//调用prepareOptionsMenu()隐藏操作栏图标
invalidateOptionsMenu();
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
如果(savedInstanceState == NULL)
{
selectItemFromDrawer(0);
}
}
@覆盖
公共布尔onCreateOptionsMenu(功能菜单){
//充气菜单;这增加了项目操作栏,如果它是present。
。getMenuInflater()膨胀(R.menu.main,菜单);
返回true;
}
@覆盖
公共布尔onOptionsItemSelected(菜单项项){
如果(mDrawerToggle.onOptionsItemSelected(项目)){
返回true;
}
//手柄操作栏操作点击
开关(item.getItemId()){
案例R.id.action_settings:
返回true;
默认:
返回super.onOptionsItemSelected(项目);
}
}
私人无效selectItemFromDrawer(INT位置){
片段片段= NULL;
开关(位置){
情况下0:
片段=新AllPost();
打破;
情况1:
片段=新Categriesdrawers();
打破;
案例2:
片段=新AllBloggerList();
打破;
/ *案例3:
片段=新公司简介();
打破;
* /
默认:
打破;
}
如果(片段!= NULL)
{
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.mainContent,片段)
。承诺();
mDrawerList.setItemChecked(位置,真正的);
的setTitle(mNavItems.get(位置).mTitle);
mDrawerLayout.closeDrawer(mDrawerPane);
}
/ *片段片段=新的preferencesFragment();
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.mainContent,片段)
。承诺();
mDrawerList.setItemChecked(位置,真正的);
的setTitle(mNavItems.get(位置).mTitle);
//关闭抽屉
mDrawerLayout.closeDrawer(mDrawerPane); * /
}
一流的NavItem {
字符串mTitle;
字符串mSubtitle;
INT米康;
公众的NavItem(字符串名称,INT图标){
mTitle =称号;
米康=图标;
}
}
类DrawerListAdapter扩展了BaseAdapter {
语境mContext;
ArrayList的<的NavItem> mNavItems;
公共DrawerListAdapter(上下文的背景下,ArrayList的<的NavItem> navItems){
mContext =背景;
mNavItems = navItems;
}
@覆盖
公众诠释getCount将(){
返回mNavItems.size();
}
@覆盖
公共对象的getItem(INT位置){
返回mNavItems.get(位置);
}
@覆盖
众长getItemId(INT位置){
返回0;
}
@覆盖
公共查看getView(INT位置,查看convertView,ViewGroup中父){
查看图。
如果(convertView == NULL){
LayoutInflater充气=(LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
鉴于= inflater.inflate(R.layout.drawer_item,NULL);
}
其他 {
鉴于= convertView;
}
TextView的titleview的=(的TextView)view.findViewById(R.id.title);
ImageView的ICONVIEW =(ImageView的)view.findViewById(R.id.icon);
titleView.setText(mNavItems.get(位置).mTitle);
iconView.setImageResource(mNavItems.get(位置).mIcon);
返回查看;
}
}
@覆盖
保护无效onPostCreate(包savedInstanceState){
// TODO自动生成方法存根
super.onPostCreate(savedInstanceState);
mDrawerToggle.syncState();
}
@覆盖
公共无效onConfigurationChanged(配置NEWCONFIG){
// TODO自动生成方法存根
super.onConfigurationChanged(NEWCONFIG);
mDrawerToggle.onConfigurationChanged(NEWCONFIG);
}
@覆盖
公共无效的setTitle(CharSequence的标题){
mTitle =称号;
。getActionBar()的setTitle(mTitle);
}
@覆盖
公共无效onBack pressed(){
如果(getFragmentManager()getBackStackEntryCount()> 0){
getFragmentManager()popBackStack()。
} 其他 {
super.onBack pressed();
}
}
}
解决方案
请致电 addToBackStack(空)
当你执行片段交易。
fragmentManager.beginTransaction()
.replace(R.id.mainContent,片段)
.addToBackStack(空)
。承诺();
I am using navigation drawer in my app,I have fragments like F1,F2 and so on..and in every fragment I am parsing data and display in list view,everything is works fine if I go through my app like F1>F2>F3 and so on..but the issue is if I go from F3 to F2,the data which I had in my F2 fragments is not showing,and display only blank page,what is the issue?Can any one help?
My F1
public class Categriesdrawers extends Fragment{
private ImageView drwr;
private SlidingDrawer sldrwr;
private ProgressDialog pDialog;
JSONArray categorylist=null;
private ListView listview;
private ArrayList<HashMap<String,String>> aList;
private static String INTEREST_ACCEPT_URL = "http:.ashx?action=category";
private static final String INTEREST_ACCEPT="categorylist";
private static final String INTERESTACCEPT_USER_ID="Id";
private static final String INTEREST_ACCEPT_NAME="categoryname";
private CustomAdapterCatagory adapter;
private TextView noacpt;
private ListView catlist;
@SuppressWarnings("deprecation")
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v=inflater.inflate(R.layout.tests, container, false);
drwr=(ImageView)v.findViewById(R.id.handle);
sldrwr=(SlidingDrawer)v.findViewById(R.id.bottom);
sldrwr.open();
new LoadAlbums().execute();
sldrwr.setOnDrawerOpenListener(new OnDrawerOpenListener() {
@Override
public void onDrawerOpened() {
new LoadAlbums().execute();
}
});
catlist=(ListView)v.findViewById(R.id.substitute_list);
catlist.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Organization tf = new Organization();
Bundle bundle = new Bundle();
tf.setArguments(bundle);
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.mainContent, tf);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.addToBackStack(null);
ft.commit();
}
});
return v;
}
class LoadAlbums extends AsyncTask<String, String, ArrayList<HashMap<String,String>>> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(Categriesdrawers.this.getActivity());
pDialog.setMessage("Loading...");
pDialog.setIndeterminate(true);
pDialog.setIndeterminateDrawable(getResources().getDrawable(R.drawable.custom_progress));
pDialog.setCancelable(false);
pDialog.show();
}
protected ArrayList<HashMap<String,String>> doInBackground(String... args) {
ServiceHandler sh = new ServiceHandler();
// Making a request to url and getting response
ArrayList<HashMap<String,String>> data = new ArrayList<HashMap<String, String>>();
String jsonStr = sh.makeServiceCall(INTEREST_ACCEPT_URL, ServiceHandler.GET);
Log.d("Response: ", "> " + jsonStr);
if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);
// Getting JSON Array node
categorylist = jsonObj.getJSONArray(INTEREST_ACCEPT);
for (int i = 0; i < categorylist.length(); i++) {
JSONObject c = categorylist.getJSONObject(i);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(INTERESTACCEPT_USER_ID, c.getString(INTERESTACCEPT_USER_ID));
map.put(INTEREST_ACCEPT_NAME,c.getString(INTEREST_ACCEPT_NAME));
// adding HashList to ArrayList
data.add(map);
}
} catch (JSONException e) {
e.printStackTrace();
}
} else {
Log.e("ServiceHandler", "Couldn't get any data from the url");
}
return data;
}
protected void onPostExecute(ArrayList<HashMap<String,String>> result) {
super.onPostExecute(result);
// dismiss the dialog after getting all albums
if (pDialog.isShowing())
pDialog.dismiss();
// updating UI from Background Thread
if(aList == null){
aList = new ArrayList<HashMap<String, String>>();
aList.addAll(result);
adapter = new CustomAdapterCatagory(getActivity(),result);
catlist.setAdapter(adapter);
}else{
aList.addAll(result);
adapter.notifyDataSetChanged();
}
}
}
MainActivity
public class MainActivity extends Activity {
ListView mDrawerList;
RelativeLayout mDrawerPane;
private ActionBarDrawerToggle mDrawerToggle;
private DrawerLayout mDrawerLayout;
private CharSequence mTitle;
ArrayList<NavItem> mNavItems = new ArrayList<NavItem>();
private CharSequence mDrawerTitle;
ActionBar actionBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
actionBar=getActionBar();
actionBar.setBackgroundDrawable(new ColorDrawable(Color.argb(255, 235, 139, 36)));
mTitle = mDrawerTitle = getTitle();
mNavItems.add(new NavItem("All Post", R.drawable.allpost));
mNavItems.add(new NavItem("Categories", R.drawable.allpost));
mNavItems.add(new NavItem("All Bloggers", R.drawable.allpost));
mNavItems.add(new NavItem("About Us", R.drawable.about));
mNavItems.add(new NavItem("Rate US", R.drawable.rates));
// DrawerLayout
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
// Populate the Navigtion Drawer with options
mDrawerPane = (RelativeLayout) findViewById(R.id.drawerPane);
mDrawerList = (ListView) findViewById(R.id.navList);
DrawerListAdapter adapter = new DrawerListAdapter(this, mNavItems);
mDrawerList.setAdapter(adapter);
mDrawerList.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3) {
selectItemFromDrawer(position);
}
});
getActionBar().setDisplayHomeAsUpEnabled(true);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.drawable.ic_view, //nav menu toggle icon
R.string.app_name, // nav drawer open - description for accessibility
R.string.app_name // nav drawer close - description for accessibility
) {
public void onDrawerClosed(View view) {
getActionBar().setTitle(mTitle);
// calling onPrepareOptionsMenu() to show action bar icons
invalidateOptionsMenu();
}
public void onDrawerOpened(View drawerView) {
getActionBar().setTitle(mDrawerTitle);
// calling onPrepareOptionsMenu() to hide action bar icons
invalidateOptionsMenu();
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
if(savedInstanceState==null)
{
selectItemFromDrawer(0);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
// Handle action bar actions click
switch (item.getItemId()) {
case R.id.action_settings:
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void selectItemFromDrawer(int position) {
Fragment fragment=null;
switch (position) {
case 0:
fragment=new AllPost();
break;
case 1:
fragment=new Categriesdrawers();
break;
case 2:
fragment=new AllBloggerList();
break;
/*case 3:
fragment=new Aboutus();
break;
*/
default:
break;
}
if(fragment!=null)
{
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.mainContent, fragment)
.commit();
mDrawerList.setItemChecked(position, true);
setTitle(mNavItems.get(position).mTitle);
mDrawerLayout.closeDrawer(mDrawerPane);
}
/*Fragment fragment = new PreferencesFragment();
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.mainContent, fragment)
.commit();
mDrawerList.setItemChecked(position, true);
setTitle(mNavItems.get(position).mTitle);
// Close the drawer
mDrawerLayout.closeDrawer(mDrawerPane);*/
}
class NavItem {
String mTitle;
String mSubtitle;
int mIcon;
public NavItem(String title, int icon) {
mTitle = title;
mIcon = icon;
}
}
class DrawerListAdapter extends BaseAdapter {
Context mContext;
ArrayList<NavItem> mNavItems;
public DrawerListAdapter(Context context, ArrayList<NavItem> navItems) {
mContext = context;
mNavItems = navItems;
}
@Override
public int getCount() {
return mNavItems.size();
}
@Override
public Object getItem(int position) {
return mNavItems.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.drawer_item, null);
}
else {
view = convertView;
}
TextView titleView = (TextView) view.findViewById(R.id.title);
ImageView iconView = (ImageView) view.findViewById(R.id.icon);
titleView.setText( mNavItems.get(position).mTitle );
iconView.setImageResource(mNavItems.get(position).mIcon);
return view;
}
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onPostCreate(savedInstanceState);
mDrawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
// TODO Auto-generated method stub
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}
@Override
public void setTitle(CharSequence title) {
mTitle = title;
getActionBar().setTitle(mTitle);
}
@Override
public void onBackPressed() {
if (getFragmentManager().getBackStackEntryCount() > 0) {
getFragmentManager().popBackStack();
} else {
super.onBackPressed();
}
}
}
解决方案
Please call addToBackStack(null)
when you perform fragment transaction.
fragmentManager.beginTransaction()
.replace(R.id.mainContent, fragment)
.addToBackStack(null)
.commit();
这篇关于onback pressed数据没有显示在片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!