0

I'm trying to implement a small slideshow similar to those used by many apps as an intro, such as Google Docs.

I separated the adapter from the activity as to keep everything tidy:

public class IntroActivity extends FragmentActivity { private int[] mResources = { R.drawable.slide1, R.drawable.slide2, R.drawable.slide3, R.drawable.slide4 }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_intro); CustomPagerAdapter customPagerAdapter = new CustomPagerAdapter(this, mResources); ViewPager viewPager = (ViewPager) findViewById(R.id.pager); viewPager.setAdapter(customPagerAdapter); } } 

And the following code in my CustomPagerAdapter handles the click event of a button on the last slide which opens the MainActivity through an intent. Pretty standard stuff:

 @Override public Object instantiateItem(ViewGroup container, int position) { if (position == (mResources.length - 1)) { System.out.println("POSITION " + position); View itemView = mLayoutInflater.inflate(R.layout.pager_last_item, container, false); ImageView imageView = (ImageView) itemView.findViewById(R.id.lastImageView); imageView.setImageResource(mResources[position]); Button btn = (Button) itemView.findViewById(R.id.btn_open_main_activity); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent = new Intent(mContext, MainActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); mContext.startActivity(intent); //TODO destroy IntroActivity.java } }); container.addView(itemView); return itemView; } (...) //if not the last slide... } 

But it doesn't seem to work, even with those Intent flags. I have no way to access the finish() method from the IntroActivity. The app runs fine, but when I press the back button in the MainActivity, it brings me back to the last slide. Is there a way to fix it?

EDIT - This is how I'm creating the PagerAdapter. I'm just passing the context and the resource array from IntroActivity:

public class CustomPagerAdapter extends PagerAdapter { private Context mContext; private LayoutInflater mLayoutInflater; private int[] mResources; public CustomPagerAdapter(Context context, int[] mResources) { this.mContext = context; this.mLayoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); this.mResources = mResources; } 
1
  • As a little addendum, can someone clarify if passing Activity references to normal classes is good practice? Commented Mar 27, 2015 at 0:21

1 Answer 1

1

You can add android:noHistory="true" attribute in IntroActivity declaration in manifest.

refer this

Or you can pass activity reference to the fragment or the adapter claa via a constructor and call activity.finish()

something like new Adapter(mActivity) and call mActivity.finish()

Sign up to request clarification or add additional context in comments.

3 Comments

I'm a bit dubious about that solution... They said I should avoid it in case I implement Login via facebook for example. Which I eventually will, since this is just a spike for a project
So in IntroActivity I should do something like CustomPagerAdapter customPagerAdapter = new CustomPagerAdapter(/*reference here*/ ,this, mResources);? I'm still pretty new to Android. I understand that passing 'this' refers to the context, and it's not a direct reference to the Activity
Well, eventually I did exactly what you proposed, and it has worked. Thank you :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.