I want the app as follows: When user clicks on one of the buttons it goes to another card activity which contains cardstack view-pager and user will read the cards and swipe when that card is done. And another card will appear. And if he goes back to main activity and comes back to card activity then card activity should start with card which he left off.
Here is my viewpager initialization:
//Viewpager initialization
mViewPager = (ViewPager)findViewById(R.id.viewpager);
CardStackAdapter mAdapter = new CardStackAdapter(getSupportFragmentManager());
mViewPager.setPageTransformer(true,new CardStackTransformer());
mViewPager.setOffscreenPageLimit(6);
mViewPager.setPadding(0,0,0,10);
mViewPager.setAdapter(mAdapter);
Here is ADAPTER:
public class CardStackAdapter extends FragmentStatePagerAdapter{
public CardStackAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
switch(position){
case 0:
return new overview1();
case 1:
return new overview2();
case 2:
return new overview2();
case 3:
return new overview2();
case 4:
return new overview2();
case 5:
return new overview2();
default:
break;
}
return null;
}
@Override
public int getCount() {
return 6;
}
}
Here is my whole Activity
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
public class overview extends AppCompatActivity {
private ViewPager mViewPager;
@Override
protected void onSaveInstanceState(Bundle outState) {
outState.putInt("ViewPagerPosition",mViewPager.getCurrentItem());
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_overview);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
this.setTitle("");
this.getSupportActionBar().setHomeAsUpIndicator(R.mipmap.ic_close_white_24dp);
this.getSupportActionBar().setDisplayHomeAsUpEnabled(true);
//Viewpager initialization
mViewPager = (ViewPager)findViewById(R.id.viewpager);
CardStackAdapter mAdapter = new CardStackAdapter(getSupportFragmentManager());
mViewPager.setPageTransformer(true,new CardStackTransformer());
mViewPager.setOffscreenPageLimit(6);
mViewPager.setPadding(0,0,0,10);
mViewPager.setAdapter(mAdapter);
if(savedInstanceState!=null){
int lastPosition = savedInstanceState.getInt("ViewPagerPosition",0);
mViewPager.setCurrentItem(lastPosition);
}
}
public class CardStackAdapter extends FragmentStatePagerAdapter{
public CardStackAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
switch(position){
case 0:
return new overview1();
case 1:
return new overview2();
case 2:
return new overview2();
case 3:
return new overview2();
case 4:
return new overview2();
case 5:
return new overview2();
default:
break;
}
return null;
}
@Override
public int getCount() {
return 6;
}
}
private class CardStackTransformer implements ViewPager.PageTransformer
{
@Override
public void transformPage(View page, float position)
{
if(position>=0)
{
page.setScaleX(0.93f - 0.015f * position);
page.setScaleY(0.93f);
page.setTranslationX(- page.getWidth()*position);
page.setTranslationY(10 * position);
}
}
}
}
You should try saving your activity's state in onSaveInstanceState() and restoring it in onCreate():
@Override
protected void onSaveInstanceState(Bundle instanceState) {
super.onSaveInstanceState(instanceState);
instanceState.putInt("viewPagerPosition", viewPager.getCurrentItem());
}
@Override
protected void onCreate(Bundle savedInstanceState) {
//after setting the adapter
if (savedInstanceState != null) {
int lastPosition = savedInstanceState.getInt("viewPagerPosition", 0);
viewPager.setCurrentItem(lastPosition);
}
}