How to handle Back Button press in android fragments

16,411

I think you can do this:

Create a generic Fragment:

public abstract class extends BaseFragment {

    public abstract boolean  onBackPressed();

}

In his Fragments, extends this BaseFragment, example:

public class TopStoriesFragment extends BaseFragment {

    ...


    @Override
    public boolean onBackPressed()
    {
       //if you return false, then it is because you have not consumed 
       //the event, but if it          returns true, which is consumed by this event.
        return false;
    }
}

In his MainActivity activity, you should check in onBackPressed event, if the event was consumed or not, to decide what to do.

It is very important that you save the current tab!

public class MainActivity extends FragmentActivity implements
        ActionBar.TabListener {

       //It is very important that you save the current tab!
       private BaseFragment currentTab;

     // ...  implementation 



public class TabsPagerAdapter extends FragmentPagerAdapter {

    public TabsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int index) {

         currentTab = null;

        switch (index) {
        case 0:
            // Top Rated fragment activity
            currentTab = new TopStoriesFragment();
       break;
        case 1:
            // Games fragment activity
            return new NationalFragment();
        case 2:
            // Movies fragment activity
            currentTab = new InternationalFragment();
          break;
        case 3:
            // Movies fragment activity
            currentTab = new SportsFragment();
           break;
        case 4:
            // Movies fragment activity
            currentTab = new TechnologyFragment();
        case 5:
            // Movies fragment activity
            currentTab = new BusinessFragment();
         break;
        case 6:
            // Movies fragment activity
            currentTab = new EntertainmentFragment();
        break;
        }


        return currentTab;
    }

    @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;
    }

}


@Override
public void onBackPressed() 
{
  //If the event returned false, then call the super.
  if(currentTab == null || !currentTab.onBackPressed())
  super.onBackPressed();
}

}

Change to your case!

Share:
16,411
Akshat
Author by

Akshat

An average scoring Computer Science graduate into career in Information Technology. Working as Mobile Apps Developer in the Silicon City of India, called Bangalore. Facebook Twitter LinkedIn

Updated on June 26, 2022

Comments

  • Akshat
    Akshat about 2 years

    I have a class which extends fragment. It receives a data from server and loads on the fragment layout. The data comes from AsyncTask doInBackground. But I am facing a problem that if asynctask is still doing in background and user presses back button, my app goes to another activity. But when the asynctask finishes, it does not find the needed layout and hence crashes the app. How to cancel this async task when user presses the back button in the middle of do in background, and more importantly, how to call on back pressed in fragments.

    Here is my 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);
    
            }
        }
    
    }
    
  • Akshat
    Akshat almost 10 years
    What is current tab here.? Can you please show how to declare it.? And also how to create that generic class you have mentioned above.?
  • Cícero Moura
    Cícero Moura almost 10 years
    You must create this BaseFragment extends Fragment . See it requires the implementation of onBackPressed method. Your adapter will keep the current tab! In this methodology, you will be using polymorphism. Note that its main activity has a global variable BaseFragment. In its adapter are assigning the instance of the current tab. When he presses the Back button, you check if the onBackPressed the Fragment method was consumed. If so, then do nothing, but if not, then perform the standard procedure.
  • Admin
    Admin almost 10 years
    can you please explain How to create that generic fragment.?
  • Cícero Moura
    Cícero Moura almost 10 years
    You simply create a class BaseFragment that extends Fragment. It is generic in the sense of serving as a basis for any fragment that you create. That way, you ensure polymorphism for all of them. Ententa generic used in this case as in all the fragments, which may be polymorphism in all cases. Generic here is not the concept of programming languages​​, like Java itself, which implements this methodology in lists, etc.. Genéirco here is the concept of the same word. You can see the setting in the code example above.