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;
}
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()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.