Comment gérer appuyez sur le Bouton de en android fragments

J'ai une classe qui extends fragment. Il reçoit des données à partir du serveur et des charges sur le fragment de mise en page. Les données proviennent d'AsyncTask doInBackground. Mais je suis confronté à un problème que si asynctask est toujours en arrière-plan et de l'utilisateur appuie sur le bouton de retour, mon application va à une autre activité. Mais lorsque l'asynctask finitions, il ne trouve pas le besoin de mise en page et donc bloque l'application. Comment faire pour annuler cette tâche asynchrone lorsque l'utilisateur appuie sur le bouton retour dans le milieu de faire en arrière-plan, et, plus important encore, comment appeler sur le dos appuyé par fragments.

Voici mon code:

TabsPagerAdapter.java

public class TabsPagerAdapter extends FragmentPagerAdapter {
public TabsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int index) {
switch (index) {
case 0:
//Top Rated fragment activity
return new TopStoriesFragment();
case 1:
//Games fragment activity
return new NationalFragment();
case 2:
//Movies fragment activity
return new InternationalFragment();
case 3:
//Movies fragment activity
return new SportsFragment();
case 4:
//Movies fragment activity
return new TechnologyFragment();
case 5:
//Movies fragment activity
return new BusinessFragment();
case 6:
//Movies fragment activity
return new EntertainmentFragment();
}
return null;
}
@Override
public int getCount() {
//get item count - equal to number of tabs
return 7;
}
public void stop() {
//get item count - equal to number of tabs
return;
}
}

MainActivity.java

public class MainActivity extends FragmentActivity implements
ActionBar.TabListener {
private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private ActionBar actionBar;
//Tab titles
private String[] tabs = { "Top Stories", "National", "International", "Sports", "Technology", "Business", "Entertainment"};
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager = (ViewPager) findViewById(R.id.pager);
actionBar = getActionBar();
mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(mAdapter);
actionBar.setHomeButtonEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);        
//Adding Tabs
for (String tab_name : tabs) {
actionBar.addTab(actionBar.newTab().setText(tab_name)
.setTabListener(this));
}
/**
* on swiping the viewpager make respective tab selected
* */
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageSelected(int position) {
//on changing the page
//make respected tab selected
actionBar.setSelectedNavigationItem(position);
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
});
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
//on tab selected
//show respected fragment view
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}
}

BusinessFragment.java

public class BusinessFragment extends Fragment {
public String thehindu = "xxxxx";
public String toi = "xxxxx";
public String hindustantimes = "xxxxx";
public String indianexpress = "xxxxxx";
public String reuters = "xxxxxxx";
public String dainikjagaran = "xxxxxx";
public String dainikbhaskar = "xxxxx";
public String amarujala = "xxxxxx";
public String currenturl;
AlertDialog levelDialog;
public LayoutInflater inflater;
ListView listView;
int mCurCheckPosition;
SharedPreferences sp;
String papername;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_data, container,
false);
sp = getActivity().getSharedPreferences("myfile",
getActivity().MODE_PRIVATE);
papername = sp.getString("Newspaper_name", null);
feedTitle = (TextView) rootView.findViewById(R.id.feedtitle);
feedDescribtion = (TextView) rootView
.findViewById(R.id.feeddescribtion);
getCurrentURL();
listView = (ListView) rootView.findViewById(android.R.id.list);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
Intent intent = new Intent(getActivity(), ShowDetails.class);
Bundle bundle = new Bundle();
bundle.putString("keyTitle", myRssFeed.getItem(position)
.getTitle());
bundle.putString("keyDescription", myRssFeed.getItem(position)
.getDescription());
bundle.putString("keyLink", myRssFeed.getItem(position)
.getLink());
bundle.putString("keyPubdate", myRssFeed.getItem(position)
.getPubdate());
intent.putExtras(bundle);
startActivity(intent);
}
});
startReadRss();
return rootView;
}
public class RssLoadingTask extends AsyncTask<Void, Void, Void> {
@Override
protected void onPostExecute(Void result) {
//TODO Auto-generated method stub
displayRss();
}
@Override
protected void onPreExecute() {
//TODO Auto-generated method stub
preReadRss();
}
@Override
protected void onProgressUpdate(Void... values) {
//TODO Auto-generated method stub
//super.onProgressUpdate(values);
}
@Override
protected Void doInBackground(Void... arg0) {
//TODO Auto-generated method stub
readRss();
return null;
}
}
private RSSFeed myRssFeed = null;
TextView feedTitle;
TextView feedDescribtion;
//TextView feedPubdate;
//TextView feedLink;
public class MyCustomAdapter extends ArrayAdapter<RSSItem> {
public MyCustomAdapter(Context context, int textViewResourceId,
List<RSSItem> list) {
super(context, textViewResourceId, list);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//TODO Auto-generated method stub
//return super.getView(position, convertView, parent);
View row = convertView;
if (row == null) {
inflater = LayoutInflater.from(getActivity());
row = inflater.inflate(R.layout.row, parent, false);
}
TextView listTitle = (TextView) row.findViewById(R.id.listtitle);
listTitle.setText(myRssFeed.getList().get(position).getTitle());
TextView listPubdate = (TextView) row
.findViewById(R.id.listpubdate);
listPubdate.setText(myRssFeed.getList().get(position).getPubdate());
return row;
}
}
private void startReadRss() {
new RssLoadingTask().execute();
}
private void getCurrentURL() {
if (papername.equalsIgnoreCase("The Times of India")) {
currenturl = toi;
} else if (papername.equalsIgnoreCase("The Hindu")) {
currenturl = thehindu;
} else if (papername.equalsIgnoreCase("The Hindustan Times")) {
currenturl = hindustantimes;
} else if (papername.equalsIgnoreCase("The Indian Express")) {
currenturl = indianexpress;
} else if (papername.equalsIgnoreCase("Reuters")) {
currenturl = reuters;
} else if (papername.equalsIgnoreCase("दैनिक जागरण")) {
currenturl = dainikjagaran;
} else if (papername.equalsIgnoreCase("दैनिक भास्कर")) {
currenturl = dainikbhaskar;
} else if (papername.equalsIgnoreCase("अमर उजाला")) {
currenturl = amarujala;
}
//else if(papername.equalsIgnoreCase("Times of India"))
//{
//
//}
//else if(papername.equalsIgnoreCase("Times of India"))
//{
//
//}
//
}
private void preReadRss() {
feedTitle.setText("--- wait ---");
feedDescribtion.setText("");
//feedPubdate.setText("");
//feedLink.setText("");
listView.setAdapter(null);
//Toast.makeText(getActivity(), "Reading RSS, Please wait.",
//Toast.LENGTH_LONG)
//.show();
}
private void readRss() {
try {
URL rssUrl = new URL(currenturl);
SAXParserFactory mySAXParserFactory = SAXParserFactory
.newInstance();
SAXParser mySAXParser = mySAXParserFactory.newSAXParser();
XMLReader myXMLReader = mySAXParser.getXMLReader();
RSSHandler myRSSHandler = new RSSHandler();
myXMLReader.setContentHandler(myRSSHandler);
InputSource myInputSource = new InputSource(rssUrl.openStream());
myXMLReader.parse(myInputSource);
myRssFeed = myRSSHandler.getFeed();
} catch (MalformedURLException e) {
//TODO Auto-generated catch block
e.printStackTrace();
} catch (ParserConfigurationException e) {
//TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
//TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
//TODO Auto-generated catch block
e.printStackTrace();
}
}
private void displayRss() {
if (myRssFeed != null) {
Calendar c = Calendar.getInstance();
//String strCurrentTiime = "\n(Time of Reading - "
//+ c.get(Calendar.HOUR_OF_DAY)
//+ " Hrs. : "
//+ c.get(Calendar.MINUTE) + " Mins.)\n" +c.get(Calendar.AM_PM);
SimpleDateFormat sdf = new SimpleDateFormat(
"MMMM-dd-yyyy HH:mm:ss a");
String strCurrentTiime = sdf.format(c.getTime());
feedTitle.setText(papername);
feedDescribtion.setText(strCurrentTiime);
//feedPubdate.setText(myRssFeed.getPubdate());
//feedLink.setText(myRssFeed.getLink());
MyCustomAdapter adapter = new MyCustomAdapter(getActivity(),
R.layout.row, myRssFeed.getList());
listView.setAdapter(adapter);
}
}
}

OriginalL'auteur Akshat | 2014-07-12