Bye Webster Bye Webster - 2 months ago 25
Android Question

How to set back to specific tab from activity android, using onBackPressed?

i'm using TabLayout and viewpager for swipe between each tab, and i have problem at positioning tabs, ex: im at tabs3 and click something & go to new activity which is detail of something, and my problems is when i want go back to specific on tabs3, but not back to tabs3 and its direct back to tabs1, and i already search but still not get solve.

how to back from activity to specific tabs which is in tab using fragment..?

i'm using this for the first calling TabFragment.

public class MainActivity extends AppCompatActivity{

DrawerLayout mDrawerLayout;
NavigationView mNavigationView;
FragmentManager mFragmentManager;
FragmentTransaction mFragmentTransaction;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// session manager
session = new SessionManager(getApplicationContext());

if (!session.isLoggedIn()) {
logoutUser();
}



mFragmentManager = getSupportFragmentManager();
mFragmentTransaction = mFragmentManager.beginTransaction();
mFragmentTransaction.replace(R.id.containerView, new TabFragment()).commit();

}}


and thenn, this is how i setting tabs.

public class TabFragment extends Fragment {

public static TabLayout tabLayout;
public static ViewPager viewPager;
public static int int_items = 4 ;
public static Toolbar toolbar;

private int[] icons_off = {
R.mipmap.ic_panorama_white_36dp,
R.mipmap.ic_notifications_active_white_36dp,
R.mipmap.ic_inbox_white_36dp,
R.mipmap.ic_tag_faces_white_36dp
};
private int[] icons_on = {
R.mipmap.ic_panorama_black_36dp,
R.mipmap.ic_notifications_active_black_36dp,
R.mipmap.ic_inbox_black_36dp,
R.mipmap.ic_tag_faces_black_36dp
};

private String[] title_tab = {
"",
"Tour",
"Draft",
"My Profile"
};

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
/**
*Inflate tab_layout and setup Views.
*/
View x = inflater.inflate(R.layout.tab_layout,null);
tabLayout = (TabLayout) x.findViewById(R.id.tabs);
viewPager = (ViewPager) x.findViewById(R.id.viewpager);

/**
*Set an Apater for the View Pager
*/
viewPager.setAdapter(new MyAdapter(getChildFragmentManager()));


toolbar = (android.support.v7.widget.Toolbar) x.findViewById(R.id.tool_bar);
((AppCompatActivity)getActivity()).setSupportActionBar(toolbar);
((AppCompatActivity)getActivity()).getSupportActionBar().setDisplayShowTitleEnabled(false);
toolbar.setLogo(R.mipmap.ic_landscape_white_36dp);
setHasOptionsMenu(true);

/**
* Now , this is a workaround ,
* The setupWithViewPager dose't works without the runnable .
* Maybe a Support Library Bug .
*/

tabLayout.post(new Runnable() {
@Override
public void run() {
tabLayout.setupWithViewPager(viewPager);

tabLayout.getTabAt(0).setIcon(icons_on[0]).select();
tabLayout.getTabAt(1).setIcon(icons_off[1]);
tabLayout.getTabAt(2).setIcon(icons_off[2]);
tabLayout.getTabAt(3).setIcon(icons_off[3]);
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

}

@Override
public void onPageSelected(int position) {
switch (position) {
case 0:
toolbar.setLogo(R.mipmap.ic_landscape_white_36dp);
toolbar.setTitle(Html.fromHtml("<font color='#ffffff'>"+title_tab[position]+"</font>"));
tabLayout.getTabAt(0).setIcon(icons_on[0]);
tabLayout.getTabAt(1).setIcon(icons_off[1]);
tabLayout.getTabAt(2).setIcon(icons_off[2]);
tabLayout.getTabAt(3).setIcon(icons_off[3]);
break;
case 1:
toolbar.setLogo(null);
toolbar.setTitle(Html.fromHtml("<font color='#ffffff'>"+title_tab[position]+"</font>"));
tabLayout.getTabAt(0).setIcon(icons_off[0]);
tabLayout.getTabAt(1).setIcon(icons_on[1]);
tabLayout.getTabAt(2).setIcon(icons_off[2]);
tabLayout.getTabAt(3).setIcon(icons_off[3]);
break;
case 2:
toolbar.setLogo(null);
toolbar.setTitle(Html.fromHtml("<font color='#ffffff'>"+title_tab[position]+"</font>"));
tabLayout.getTabAt(0).setIcon(icons_off[0]);
tabLayout.getTabAt(1).setIcon(icons_off[1]);
tabLayout.getTabAt(2).setIcon(icons_on[2]);
tabLayout.getTabAt(3).setIcon(icons_off[3]);
break;
case 3:
toolbar.setLogo(null);
toolbar.setTitle(Html.fromHtml("<font color='#ffffff'>"+title_tab[position]+"</font>"));
tabLayout.getTabAt(0).setIcon(icons_off[0]);
tabLayout.getTabAt(1).setIcon(icons_off[1]);
tabLayout.getTabAt(2).setIcon(icons_off[2]);
tabLayout.getTabAt(3).setIcon(icons_on[3]);
break;
}
}

@Override
public void onPageScrollStateChanged(int state) {

}

});


}
});

return x;

}

public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_main, menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_search) {
//Toast.makeText(getContext(), "Chat Clicked !", Toast.LENGTH_SHORT).show();

Intent in = new Intent(getActivity(), SearchActivity.class);
in.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(in);
} else if(id == R.id.action_setting){
//Toast.makeText(getContext(), "Chat Clicked !", Toast.LENGTH_SHORT).show();

Intent in = new Intent(getActivity(), SettingsActivity.class);
in.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(in);
}

return super.onOptionsItemSelected(item);
}

class MyAdapter extends FragmentPagerAdapter{

public MyAdapter(FragmentManager fm) {
super(fm);
}

/**
* Return fragment with respect to Position .
*/

@Override
public Fragment getItem(int position)
{
switch (position){
case 0 : return new Feed();
case 1 : return new Tour();
case 2 : return new Project();
case 3 : return new MyProfile();
}
return null;
}

@Override
public int getCount() {

return int_items;

}

@Override
public CharSequence getPageTitle(int position) {
/*
switch (position){
case 0 :
return "Primary";
case 1 :
return "Social";
case 2 :
return "Updates";
case 3 :
return "User";
}
*/
return null;
}

public boolean onTouch(View v, MotionEvent event) {
return true;
}

}}


and my last position before back to what i want.

public class TourActivity extends AppCompatActivity {
@Override
public void onBackPressed() {
super.onBackPressed();
// here's where i want to back to specific tab
}

Answer

Before you create the new activity, save the viewpager position, after that, when you navigate back, set the position to the view pager with setCurrentItem(position);

First, save your position before you start the other activity, it can be a sharedPreferences or something else.

Secondly, at the tab fragment'a onResume read the position from sharedPreferences and set the current item to the viewpager with viewpager.setCurrentItem(position).