Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8015413
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T20:15:10+00:00 2026-06-04T20:15:10+00:00

I have a viewpager working well, when I click on a view/page it opens

  • 0

I have a viewpager working well, when I click on a view/page it opens a new activity, but when I want to press back to go to the viewpager the app closes. How would I get the viewpager to open properly to the current page when I press the back button?

I know it is supposed to so here is the viewpager code:

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

    }       

}

}

I’m not sure if it’s to do with restore state method or return null in parcelable savestate. It could be completely obvious so thanks in advance!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T20:15:12+00:00Added an answer on June 4, 2026 at 8:15 pm

    Solved this by using FLAG_ACTIVITY_CLEAR_TOP on my second and third activities after I call the new intent.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I created a ViewPager and everything is working fine, however I want to have
I'm trying to use viewpager in my app. I want to create 4 view
I have a ViewPager with 4 views. View #3 needs to respond to some
I have implemented HorizontalListView inside ViewPager . ViewPager works fine but a child of
I have an activity with a view pager and three pages to display. When
I'm working on creating a new base class for an asp.net app in order
I have a ViewPager with 3 pages. In each page, I would like to
I am working on an app which uses viewpager with around 100-150 views in
I have code that works in a .NET 3.5 app that isn't working when
I have a project using a ViewPager which works correctly: I can page from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.