I'm currently working in a new android project .
I'm using ActionBar Compact
with Tab navigation mode
.
I added 3 tabs
in my Activity.
In the first Fragment , there is a listView and TextView (witch contains the text of slected row of list)
Now , when i select the second tab (fragment (empty) ), and go back to my first fragment , the TextView contains the selected value BUT when I select the third tab (fragment 3 empty) , and go back to my first fragent , the TextView was initilised . I think that my problem is in the TabListener
can anyone help me please ! this is the code (simplified)
MainActivity:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/** Getting a reference to action bar of this activity */
mActionbar = getSupportActionBar();
/** Set tab navigation mode */
mActionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
/** set HomeButton to true */
mActionbar.setHomeButtonEnabled(true);
/** Getting a reference to ViewPager from the layout */
mPager = (ViewPager) findViewById(R.id.pager);
/** Getting a reference to FragmentManager */
FragmentManager fm = getSupportFragmentManager();
/** Defining a listener for pageChange */
ViewPager.SimpleOnPageChangeListener pageChangeListener = new ViewPager.SimpleOnPageChangeListener(){
@Override
public void onPageSelected(int position) {
super.onPageSelected(position);
mActionbar.setSelectedNavigationItem(position);
}
};
/** Setting the pageChange listener to the viewPager */
mPager.setOnPageChangeListener(pageChangeListener);
/** Creating an instance of FragmentPagerAdapter */
MyFragmentPagerAdapter fragmentPagerAdapter = new MyFragmentPagerAdapter(fm);
/** Setting the FragmentPagerAdapter object to the viewPager object */
mPager.setAdapter(fragmentPagerAdapter);
mActionbar.setDisplayShowTitleEnabled(true);
/** Defining tab listener */
ActionBar.TabListener tabListener = new ActionBar.TabListener() {
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
mPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
};
/** Creating fragment1 Tab */
Tab tab = mActionbar.newTab()
.setText("Categories")
.setTabListener(tabListener);
mActionbar.addTab(tab, 0, false);
/** Creating fragment2 Tab */
tab = mActionbar.newTab()
.setText("Acceuil")
.setTabListener(tabListener);
mActionbar.addTab(tab, 1, true);
/** Creating fragment3 Tab */
tab = mActionbar.newTab()
.setText("Services")
.setTabListener(tabListener);
mActionbar.addTab(tab, 2, false);
}
Fragment 2 ::
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
categories = inflater.inflate(R.layout.fragment_categories, container, false);
list= (ListView)categories.findViewById(R.id.listCategories);
// Defined Array values to show in ListView
String[] values = new String[] { "Android List View",
"Adapter implementation",
"Simple List View In Android",
"Create List View Android",
"Android Example",
"List View Source Code",
"List View Array Adapter",
"Android Example List View"
};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity().getApplicationContext(),android.R.layout.simple_list_item_1, android.R.id.text1, values);
// Assign adapter to ListView
list.setAdapter(adapter);
t =(TextView)categories.findViewById(R.id.textView1);
list.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent,View v, int position, long id)
{
Toast.makeText(getActivity().getBaseContext(),"category " + (position + 1) +" selected",Toast.LENGTH_SHORT).show();
t.setText("category " + (position + 1) +" selected");
}
});
return categories;
}
ScreenShots
This when I select row from listView (fragment 1) ==> fragment 2 ==> go back fragment 1
This when I select row from listView (fragment 1) ==> fragment 3 ==> go back fragment 1
In onCreate()
of your main Activity, after you initialize your ViewPager
, add:
mPager.setOffscreenPageLimit(2);
Per the Android docs, setOffscreenPageLimit()
:
Sets the number of pages that should be retained to either side of the current page in the view hierarchy in an idle state. Pages beyond this limit will be recreated from the adapter when needed.
This is offered as an optimization. If you know in advance the number of pages you will need to support or have lazy-loading mechanisms in place on your pages, tweaking this setting can have benefits in perceived smoothness of paging animations and interaction. If you have a small number of pages (3-4) that you can keep active all at once, less time will be spent in layout for newly created view subtrees as the user pages back and forth.
You should keep this limit low, especially if your pages have complex layouts. This setting defaults to 1.
Since the default setting is 1, when you start on the first tab and navigate to the third tab, the first page (i.e., fragment) is being recreated when you navigate back to the first tab. Setting this to 2 should retain the fragment in the first tab even when you navigate to the third tab.