Saving tabLayout position when Back button pressed - android

how can I save and return the tab position when user press the Back button?
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
ViewPager vpPager = (ViewPager) findViewById(R.id.viewpager);
adapterViewPager = new MyPagerAdapter(getSupportFragmentManager());
vpPager.setAdapter(adapterViewPager);
// Give the TabLayout the ViewPager
tabLayout = (TabLayout) findViewById(R.id.sliding_tabs);
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
tabLayout.setTabMode(TabLayout.MODE_FIXED);
tabLayout.setupWithViewPager(vpPager);
// Attach the page change listener inside the activity
vpPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
}
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt(POSITION, tabLayout.getSelectedTabPosition());
}
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
viewPager.setCurrentItem(savedInstanceState.getInt(POSITION));
}
Do I use the onBackPressed() to do this?
Help is much appreciated. Thanks!

You can do it with back press in the following way:
#Override
public void onBackPressed() {
int pos = mViewPager.getCurrentItem();
}
and when you back in screen you can set position of pager
#Override
public void onResume(){
super.onResume();
// put your code here...
if(mViewPager !=null)
mViewPager.setCurrentItem(pos);
}

Related

Android Studio:Null Pointer Excecption occured while working on TabLayout

This is my code and the error that occured
I am a beginner and was trying to work on the tab layout. The app didn't give any error while compiling but crashes midway. My main activity code is as follows:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TabLayout tabLayout= findViewById(R.id.tab_layout);
TabItem t= findViewById(R.id.t1);
final ViewPager viewPager= findViewById(R.id.pager);
new PageAdapter(getSupportFragmentManager(),4);
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
Please help me as I have to submit this as my assignment.
You need to make the declaration outside the oncreate function to make it global and seen by other methods, then do the initialization inside like this :
TabLayout tabLayout;
TabItem t;
ViewPager viewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
tabLayout= findViewById(R.id.tab_layout);
t= findViewById(R.id.t1);
viewPager= findViewById(R.id.pager);
new PageAdapter(getSupportFragmentManager(),4);

ViewPagerFragment Tab Error

i create a viewPager and is work , my problem is for select a page i just can drag page but I want to click on the tabs to select the pages . now i add some new code but i cant find this error
my code
public class MainActivity extends FragmentActivity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
Pager adapter = new Pager(getSupportFragmentManager());
viewPager.setAdapter(adapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tablayout);
tabLayout.setupWithViewPager(viewPager);
viewPager.addOnAdapterChangeListener( new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
TabLayout.OnTabSelectedListener tabListener = new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
};
You cant use like viewPager.addOnAdapterChangeListener( new TabLayout.TabLayoutOnPageChangeListener(tabLayout)); Because addOnAdapterChangeListener() method expect OnAdapterChangeListener as paramenter. You should use void addOnPageChangeListener (ViewPager.OnPageChangeListener listener) on viewPager instead.It has several callback method, So you can detect the event when page of viewPager is changed.
Reference

Change DotsPageIndicator position programmatically

I made one GridView with DotsPageIndicator.
private GridViewPager pager;
private DotsPageIndicator dotsPageIndicator;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_teste);
pager = (GridViewPager) findViewById(R.id.pager);
pager.setAdapter(new GridPagerAdapter(getFragmentManager()));
dotsPageIndicator = (DotsPageIndicator) findViewById(R.id.page_indicator);
dotsPageIndicator.setDotFadeWhenIdle(false);
dotsPageIndicator.setPager(pager);
}
#Override
public void onResume() {
super.onResume();
pager.getAdapter().notifyDataSetChanged();
}
When I use notifyDataSetChanged() the position of dots is lost and it returns to the first. How can I change dots position programmatically?

android: how to refresh a tab after button click

I have page with a button and tabs below. I want the tab which is opened to reflect the change in variable data after i click the button. I am not able to understand it.
The following is my code:-
public class Level2MainActivity_grid1 extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.level2mainlayout_place1);
// I am putting a viewpager with fragments into them and put tabs
ViewPager viewPager = (ViewPager) findViewById(R.id.viewPager);
PagerAdapter pagerAdapter = new PagerAdapter(getSupportFragmentManager(), Titles, Numboftabs);
viewPager.setAdapter(pagerAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabLayout);
tabLayout.setupWithViewPager(viewPager);
Button buttonId = (Button) findViewById(R.id.planmytraveludupitown);
buttonId.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//I want to save some things and also refresh the current tab
}
});
}
}
Below line also works but incase it don't work then you can try another option also
pagerAdapter.notifydatasetChanged();
Try below code :-
buttonId.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
pagerAdapter = new PagerAdapter(getSupportFragmentManager(), Titles, Numboftabs);
viewPager.setAdapter(pagerAdapter);
tabLayout.setupWithViewPager(viewPager);
}
});
Note:- when you get new data redefine your Titles and Numoftabs again
Couldn't you notify the dataset in your adapter ?
pagerAdapter.notifyDatasetChanged()

Toolbar with ViewPager not restoring last tab selected when starting new activity

I'm using the v7 widget Toolbar and setting it as my support action bar in my AppCompatAcivity. When I'm on a different tab and start a new activity, I can't restore the last tab I was on with onSaveInstanceState. onCreate's savedInstanceState would be null and onRestoreInstanceState is never called when coming from activity 2 to my activity with tabs. How can I solve this?
public class ContactsActivity extends AppCompatActivity {
private static String TAB_POSITION = "POSITION";
private TabLayout tabLayout;
private ViewPager viewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_contacts);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
toolbar.setNavigationIcon(R.drawable.logo_spokeo);
viewPager = (ViewPager) findViewById(R.id.viewpager);
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new RecentContactsFragment(), "Recent Contacts");
adapter.addFragment(new AddressBookFragment(), "Address Book");
adapter.addFragment(new RecentContactsFragment(), "Settings");
viewPager.setAdapter(adapter);
if (savedInstanceState != null) {
viewPager.setCurrentItem(savedInstanceState.getInt(TAB_POSITION, 0), false);
}
}
#Override
protected void onSaveInstanceState(Bundle outState) {
outState.putInt(TAB_POSITION, tabLayout.getSelectedTabPosition());
super.onSaveInstanceState(outState);
}
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
viewPager.setCurrentItem(savedInstanceState.getInt(TAB_POSITION), true);
}
}

Categories

Resources