back button not in viewPager Works

111 Views Asked by At

I've implemented a view pager in my fragment . After pressing the Back button terminates the app instead of the previous page to change . Can someone tell me how I the Back button a page will come back to me .

    public class Warenkonto extends Fragment {

    public Warenkonto() {
     }

    ViewPager viewPager;
    WarenkontoPageAdapter adapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                    Bundle savedInstanceState) {

            View view = inflater.inflate(R.layout.start_kapitel, container, false);
adapter = new WarenkontoPageAdapter(this.getActivity());
            viewPager = (ViewPager) view.findViewById(R.id.viewPager);
            viewPager.setAdapter(adapter);
      viewPager.setCurrentItem(0);
      return view;
     }

    public void onBackPressed() {
            if (viewPager.getCurrentItem() == 0) {
                    viewPager.setCurrentItem(0);
} else {
                    viewPager.setCurrentItem(viewPager.getCurrentItem() - 1);
            }
    }

}

public class WarenkontoPageAdapter extends PagerAdapter {

    Context context;

    public WarenkontoPageAdapter(Context context) {
            this.context = context;
    }

    TextView titel;
    TextView titelInhalt;
    TextView inhalt;
    TextView seite_von_bis;
    ImageView bild;
    ImageView imagePfeilrechts;
    CharSequence titelInhalttext; 
    CharSequence inhaltText; 

    public int seite;
    int maxSeitenzahl = 20;

    public int getCount() {
            return 3;
    }

    public Object instantiateItem(View collection, int position) {

            LayoutInflater inflater = (LayoutInflater) collection.getContext()
                            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            int resId = 0;

            switch (position) {
            case 0:
                    resId = R.layout.a_titel_inhalt;
                    seite = 1;
                    break;
            case 1:
                    resId = R.layout.a_titel_inhalt;
                    seite = 2;
                    break;
            case 2:
                    resId = R.layout.a_inhalt;
                    seite = 3;
                    break;

            }

            View view = inflater.inflate(resId, null);

            if (seite == 1) {
                    titel = (TextView) view.findViewById(R.id.a_ti_titel);
                    titel.setText("Warenkonto");

                    titelInhalt = (TextView) view.findViewById(R.id.a_ti_inhalt);
                    titelInhalttext = Html.fromHtml("Page 1");


            } else if (seite == 2) {

                    titel = (TextView) view.findViewById(R.id.a_ti_titel);
                    titel.setText("Page 2");

                    titelInhalt = (TextView) view.findViewById(R.id.a_ti_inhalt);
                    titelInhalttext = Html
                                    .fromHtml("Page 2");

                    titelInhalt.setText(titelInhalttext);


            } else if (seite == 3) {

                    inhalt = (TextView) view.findViewById(R.id.a_i_inhalt);
                    inhaltText = Html
                                    .fromHtml("Page 3");

                    inhalt.setText(inhaltText);


            }


            ((ViewPager) collection).addView(view, 0);

            return view;

    }

    @Override
    public void destroyItem(View arg0, int arg1, Object arg2) {
            ((ViewPager) arg0).removeView((View) arg2);
    }

    @Override
    public boolean isViewFromObject(View arg0, Object arg1) {
            return arg0 == ((View) arg1);
    }

    @Override
    public Parcelable saveState() {
            return null;
    }

}

1

There are 1 best solutions below

0
On

Per the Android Design Guide Navigation section (Emphasis added)

Changing view within a screen

Changing view options for a screen does not change the behavior of Up or Back: the screen is still in the same place within the app's hierarchy, and no new navigation history is created.

Examples of such view changes are:

  • Switching views using tabs and/or left-and-right swipes
  • Switching views using a dropdown (aka collapsed tabs)
  • Filtering a list
  • Sorting a list
  • Changing display characteristics (such as zooming)

Tabs (And ViewPagers) shouldn't have history, so they shouldn't be added to the back stack.