按后退按钮进行查看

时间:2012-05-06 09:46:32

标签: android android-viewpager

我有一个viewpager运行良好,当我点击一个视图/页面时,它会打开一个新活动,但是当我想回到viewpager时,应用程序关闭。当我按下后退按钮时,如何让viewpager正确打开到当前页面?

我知道这应该是viewpager代码:

public class LogoQuizActivity extends Activity {

private
class PageListener extends SimpleOnPageChangeListener{
public void onPageSelected(int position) {
    //Log.i(TAG, "page selected " + position);
       currentPage = position;
}
}    

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    MyPagerAdapter adapter = new MyPagerAdapter();
    ViewPager myPager = (ViewPager) findViewById(R.id.myfivepanelpager);
    myPager.setAdapter(adapter);
    myPager.setCurrentItem(currentPage);
    myPager.setPadding(0, 150, 0, 0);

    PageListener pageListener = new PageListener();
    myPager.setOnPageChangeListener(pageListener);

}

private class MyPagerAdapter extends PagerAdapter {

    public int getCount() {
        return 5;
    }

    public Object instantiateItem(View collection, int position) {

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

        View view = null; // this comes first
         int resId = 0;
        switch (position) {
        case 0:
            resId = R.layout.farleft;
            view = inflater.inflate(resId, null);

            break;
        case 1:
            resId = R.layout.left;
            view = inflater.inflate(resId, null);

            break;
        case 2:
            resId = R.layout.middle;
            view = inflater.inflate(resId, null);

            break;
        case 3:
            resId = R.layout.right;
            view = inflater.inflate(resId, null);

            break;
        case 4:
            resId = R.layout.farright;
            view = inflater.inflate(resId, null);

            break;
        }


        view.setTag(position);
        view.setOnClickListener(new OnClickListener() {
            public void onClick(View view) {

                int position = (Integer) view.getTag();
                Log.v("Log_tag", "Here image is clicked" + position);

                if(position == 0){
                    Intent intent = new Intent(LogoQuizActivity.this,
                            LevelOne.class);
                    startActivity(intent);

                }

                if(position == 1){
                    Intent intent = new Intent(getBaseContext(),
                            LevelTwo.class);
                    startActivity(intent);
                }

                if(position == 2){
                    Intent intent = new Intent(getBaseContext(),
                            LevelThree.class);
                    startActivity(intent);
                }

                if(position == 3){
                    Intent intent = new Intent(getBaseContext(),
                            LevelFour.class);
                    startActivity(intent);
                }

                if(position == 4){
                    Intent intent = new Intent(getBaseContext(),
                            LevelFive.class);
                    startActivity(intent);
                }
            }
        });


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

        return view;


    }



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

    }

    @Override
    public void finishUpdate(View arg0) {
        // TODO Auto-generated method stub

    }

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

    }

    @Override
    public void restoreState(Parcelable arg0, ClassLoader arg1) {
        // TODO Auto-generated method stub

    }

    @Override
    public Parcelable saveState() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void startUpdate(View arg0) {
        // TODO Auto-generated method stub

    }       

}

}

我不确定它是否与恢复状态方法有关,或者在parcelable savestate中返回null。这可能是完全明显的,所以提前感谢!

1 个答案:

答案 0 :(得分:0)

在我调用新意图后,在第二次和第三次活动中使用FLAG_ACTIVITY_CLEAR_TOP解决了这个问题。