How can I set the text of an EditText of an Fragment from the Activity?
Here es the Activity code:
public class MainActivity extends AppCompatActivity
{
FrameLayout simpleFrameLayout;
TabLayout tabLayout;
EditText editText;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
simpleFrameLayout = (FrameLayout) findViewById(R.id.simpleFrameLayout);
tabLayout = (TabLayout) findViewById(R.id.simpleTabLayout);
// Create a new Tab named "First"
TabLayout.Tab firstTab = tabLayout.newTab();
firstTab.setText("Personal"); // set the Text for the first Tab
tabLayout.addTab(firstTab); // add the tab at in the TabLayout
// Create a new Tab named "Second"
TabLayout.Tab secondTab = tabLayout.newTab();
secondTab.setText("Chat"); // set the Text for the second Tab
tabLayout.addTab(secondTab); // add the tab in the TabLayout
try {
Fragment fragment = null;
fragment = new FirstFragment();
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.simpleFrameLayout, fragment);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.commit();
}
catch (Exception e) {
}
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
// get the current selected tab's position and replace the fragment accordingly
try {
Fragment fragment = null;
switch (tab.getPosition()) {
case 0:
fragment = new FirstFragment();
break;
case 1:
fragment = new SecondFragment();
break;
}
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.simpleFrameLayout, fragment);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.commit();
}
catch (Exception e) {
}
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
public void connect(){
//set text of EditText of SecondFragment
}
}
I am looking for a solution to this problem:
I want to create a chat client and for that I need to update the EditText of a tab from time to time without the need to switch to that tab. I also need the content of that EditText not to be lost when switching tabs.
Just create a variable inside the main activity that you can update from the fragments. Since the activity's lifecycle doesn't change from one fragment to the other, you can then get the same value from the second fragment.
I hope this helps.
Assuming you don't want to use a ViewPager and a PagerAdapter (you don't need swiping between your tabs), this is what it should look like:
public class MainActivity extends AppCompatActivity
{
FrameLayout simpleFrameLayout;
TabLayout tabLayout;
EditText editText;
FirstFragment firstFragment;
SecondFragment secondFragment;
private int selectedTabIndex = 0;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
simpleFrameLayout = findViewById(R.id.simpleFrameLayout);
tabLayout = findViewById(R.id.simpleTabLayout);
TabLayout.Tab firstTab = tabLayout.newTab();
firstTab.setText("Personal");
tabLayout.addTab(firstTab);
TabLayout.Tab secondTab = tabLayout.newTab();
secondTab.setText("Chat");
tabLayout.addTab(secondTab);
FragmentManager fm = getSupportFragmentManager();
if (savedInstanceState == null) {
firstFragment = new FirstFragment();
secondFragment = new SecondFragment();
fm.beginTransaction()
.add(R.id.simpleFrameLayout, firstFragment, "firstFragment")
.add(R.id.simpleFrameLayout, secondFragment, "secondFragment");
.detach(secondFragment)
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
.commitNow();
} else {
firstFragment = (FirstFragment) fm.findFragmentByTag("firstFragment");
secondFragment = (SecondFragment) fm.findFragmentByTag("secondFragment");
this.selectedTabIndex = savedInstanceState.getInt("selectedTabIndex", 0);
// selectTab(selectedTabIndex); // you don't need this line
}
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
this.selectedTabIndex = tab.getPosition();
selectTab(selectedTabIndex);
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
private void selectTab(int selectedTabIndex) {
FragmentManager fm = getSupportFragmentManager();
if (selectedTabIndex == 0) {
fm.beginTransaction()
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
.detach(secondFragment)
.attach(firstFragment)
.commitAllowingStateLoss();
} else if(selectedTabIndex == 1) {
fm.beginTransaction()
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
.detach(firstFragment)
.attach(secondFragment)
.commitAllowingStateLoss();
}
}
public void connect(){
secondFragment.updateText("someText"); // this can run when secondFragment has no view!
}
}
Note: if you need to use more tabs, then you could use an array, but you'd still need to initialize / reinitialize as it is shown in this snippet.
A general answer based on your general question Make your edittext public static public static EditText editText;
Assign a value to it then call it like MainActivity.editText.setText(""); from any other class
`
Use this approach only if its absolutely necessary as it may lead to memory leaks since you will have to manually destroy/nullify your object after use
Related
I have a Tablelayout with two tabs, in the code I wrote that the tabs were displayed when I clicked on them. Now it works like this: I open the activity and the fragments from the TabLayout are not displayed until I click on the title of the TabLayout header. How can I make it so that when I open the activity - fragment it is already visible?
public class DetailActivity extends AppCompatActivity {
FrameLayout simpleFrameLayout;
TabLayout tabLayout;
ImageView onBackPressed;
Button subsButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);
if (getSupportActionBar() != null) {
getSupportActionBar().hide();
}
subsButton = findViewById(R.id.subsButton);
subsButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (subsButton.getText().equals("Подписаться")) {
subsButton.setText("Отписаться");
subsButton.setBackgroundColor(subsButton.getContext().getResources().getColor(R.color.colorGrey));
} else if (subsButton.getText().equals("Отписаться")) {
subsButton.setText("Подписаться");
subsButton.setBackgroundColor(subsButton.getContext().getResources().getColor(R.color.colorPrimary));
}
}
});
simpleFrameLayout = (FrameLayout) findViewById(R.id.simpleFrameLayout);
tabLayout = (TabLayout) findViewById(R.id.simpleTabLayout);
onBackPressed = (ImageView) findViewById(R.id.onBackPressed);
tabLayout.selectTab(tabLayout.getTabAt(0));
TabLayout.Tab firstTab = tabLayout.newTab();
firstTab.setText("Chelsea");
tabLayout.addTab(firstTab);
TabLayout.Tab secondTab = tabLayout.newTab();
secondTab.setText("Manchester City");
tabLayout.addTab(secondTab);
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
Fragment fragment = null;
switch (tab.getPosition()) {
case 0:
fragment = new FragmentHisOne();
break;
case 1:
fragment = new FragmentHisTwo();
break;
default:
fragment = new FragmentHisOne();
break;
}
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.simpleFrameLayout, fragment);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.commit();
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
public void onBackPressed(View view) {
Intent intent = new Intent(view.getContext(), MainActivity.class);
view.getContext().startActivity(intent);
}
You are selecting the tab without even adding it before.
tabLayout.selectTab(tabLayout.getTabAt(0)); Move this code after adding tabs.
The fact is that I did not create the default fragment in advance, before creating the TabLayout tabs. My solution:
FrameLayout simpleFrameLayout;
TabLayout tabLayout;
ImageView onBackPressed;
Button subsButton;
Fragment fragment = null;
FragmentManager fragmentManager;
FragmentTransaction fragmentTransaction;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);
if (getSupportActionBar() != null) {
getSupportActionBar().hide();
}
subsButton = findViewById(R.id.subsButton);
subsButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (subsButton.getText().equals("Подписаться")) {
subsButton.setText("Отписаться");
subsButton.setBackgroundColor(subsButton.getContext().getResources().getColor(R.color.colorGrey));
} else if (subsButton.getText().equals("Отписаться")) {
subsButton.setText("Подписаться");
subsButton.setBackgroundColor(subsButton.getContext().getResources().getColor(R.color.colorPrimary));
}
}
});
simpleFrameLayout = (FrameLayout) findViewById(R.id.simpleFrameLayout);
tabLayout = (TabLayout) findViewById(R.id.simpleTabLayout);
onBackPressed = (ImageView) findViewById(R.id.onBackPressed);
tabLayout.selectTab(tabLayout.getTabAt(0));
TabLayout.Tab firstTab = tabLayout.newTab();
firstTab.setText("Chelsea");
tabLayout.addTab(firstTab);
TabLayout.Tab secondTab = tabLayout.newTab();
secondTab.setText("Manchester City");
tabLayout.addTab(secondTab);
fragment = new FragmentHisOne();
fragmentManager = getSupportFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.simpleFrameLayout, fragment);
fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
fragmentTransaction.commit();
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
Fragment fragment = null;
switch (tab.getPosition()) {
case 0:
fragment = new FragmentHisOne();
break;
case 1:
fragment = new FragmentHisTwo();
break;
default:
fragment = new FragmentHisOne();
break;
}
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.simpleFrameLayout, fragment);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.commit();
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
public void onBackPressed(View view) {
Intent intent = new Intent(view.getContext(), MainActivity.class);
view.getContext().startActivity(intent);
}
public void subsButton(View view) {
//Button.setBackground
}
In my Android Studio project I have a TabView as a fragment of a Navigation Drawer Activity.
Fragment:
public class TabView extends Fragment {
View inflatedView;
TextView tvBalance;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
inflatedView = inflater.inflate(R.layout.delivery, container, false);
getActivity().setTitle("TabView");
TabLayout tabLayout = (TabLayout) inflatedView.findViewById(R.id.tabLayout);
tabLayout.addTab(tabLayout.newTab().setText("Tab_1"));
tabLayout.addTab(tabLayout.newTab().setText("Tab_2"));
tabLayout.addTab(tabLayout.newTab().setText("Tab_3"));
final ViewPager viewPager = (ViewPager) inflatedView.findViewById(R.id.viewpager);
viewPager.setAdapter(new PagerAdapter
(getFragmentManager(), tabLayout.getTabCount()));
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.setOnTabSelectedListener(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) {
}
});
return inflatedView;
}
public void updateBalance(String balance) {
if (inflatedView != null) {
tvBalance = inflatedView.findViewById(R.id.tvBalance);
tvBalance.setText("$" + balance);
}
Log.d("updateBalance", balance);
}
The UpdateBalance method is called in my MainActivity everytime the value changes. This works well as Log.d(...) prints the right value at the right time.
Somehow the Textfield does not change as if (inflatedView != null) is never true, although the fragment is displayed on screen.
Why is inflatedView allways null and how to avoid this problem?
I'm using TAGS when placing a fragment, and findFragmentByTag to test afterwards is a fragment is inflated... For example:
// Loading the fragment with a tag:
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.content, new HomeFragment(), "HOME_FRAGMENT");
ft.commit();
// then later check if the fragment is there:
HomeFragment fHome = (HomeFragment) getSupportFragmentManager().findFragmentByTag("HOME_FRAGMENT");
if (fHome != null && fHome.isVisible())
{
// my fragment is inflated and visible
}
super new to coding. I followed a tutorial on creating tabs and fragments. My app right now has 3 tabs on the toolbar, and when clicked they change to their respective layout. I'd like to now add a button that will take me to those layouts instead of using the toolbar, so I can move it around and place it where I want.
This is what I have in my MainActivity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSectionsPageAdapter = new SectionsPageAdapter(getSupportFragmentManager());
//Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
setupViewPager(mViewPager);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
}
private void setupViewPager(ViewPager viewPager) {
SectionsPageAdapter adapter = new SectionsPageAdapter(getSupportFragmentManager());
adapter.addFragment(new Main2Activity(), "TAB1");
adapter.addFragment(new Main3Activity(), "TAB2");
adapter.addFragment(new Main4Activity(), "TAB3");
viewPager.setAdapter(adapter);
}
So then I added this code for the button. In my XML, I placed the button above the container id so that I can always see it. Ideally, I'd have 3 buttons, each one putting into the container id Main2Activity, Main3Activity, and Main4Activity. Here is just 1 button as I try to figure out how to get the view to change.
public void onClickBtn1(View v){
FragmentManager fm = getSupportFragmentManager();
android.support.v4.app.FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.container, new Main2Activity());
ft.commit();
}
This is also my SectionsPageAdapter page if it helps.
public class SectionsPageAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
public SectionsPageAdapter(FragmentManager fm) {
super(fm);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
}
Thanks in advance!
Call ViewPager#setCurrentItem in your on click to go to the index of the item in the viewpager's adapter's list/array:
public void onClickBtn1(View v){
int gotoIndex = 0;
mViewPager.setCurrentItem(gotoIndex, /*smoothscrolling*/ true);
}
I have a home activity in that I am replacing fragments as required.
In home activity I have main fragment, then from main fragment I am replacing a Transport fragment, from Transport fragment I am replacing TransportList Fragment.
Now as I press back from TransportList fragment I see the main fragment instead of Transport fragment.
I have added the fragments to backstack still its working like this.
Home activity
public class HomeActivity extends AppCompatActivity{
private boolean mBackPressCancelled = false;
private static final long BACK_PRESS_DELAY = 10000;
private long mBackPressTimestamp;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FragmentManager fragmentManager = HomeActivity.this.getFragmentManager();
MainFragment fragment = new MainFragment();
fragmentManager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
fragmentManager.beginTransaction().replace(R.id.mycontainer, fragment,"MAIN_FRAGMENT").commitAllowingStateLoss();
}
#Override
public void onBackPressed() {
// Do nothing if the back button is disabled.
if (!mBackPressCancelled) {
// Pop fragment if the back stack is not empty.
if (getFragmentManager().getBackStackEntryCount() > 0) {
mTxtTitle.setVisibility(View.GONE);
mLogo.setVisibility(View.VISIBLE);
super.onBackPressed();
}
else {
if (snackbar != null) {
snackbar.dismiss();
}
long currentTimestamp = System.currentTimeMillis();
if (currentTimestamp < mBackPressTimestamp + BACK_PRESS_DELAY) {
super.onBackPressed();
} else {
mBackPressTimestamp = currentTimestamp;
Toast.makeText(this,"press again",Toast.LENGTH_LONG).show();
}
}
}
}
}
Transport fragment :
mBtnSearch.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
FragmentManager fragmentManager = getFragmentManager();
TransportListFragment fragment1 = new TransportListFragment();
fragmentManager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
fragmentManager.beginTransaction().replace(R.id.mycontainer, fragment1).addToBackStack("G").commit();
}
});
Whats going wrong here please help.Thank you.
Just remove the following lines when you add new Fragment to the BackStack:
fragmentManager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
i have an actionbar with two tabs and an tablistener which handles the fragments. Now i want with a ViewPager the possibility to Swipe to also switch the tabs.
I tried the solution stated here:
Android, How to mix ActionBar.Tab + View Pager + ListFragment
But it gives an conflicts with Android.app.Fragments and the Support Package Fragments.
The App is for >4.0 so i dont need the support fragments.
public class MainActivity extends Activity{
....
actionBar = getActionBar();
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
//viewpager
mViewPager = new ViewPager(this);
setContentView(mViewPager);
Doesnt work:
//TabsAdapter tabsAdapter = new TabsAdapter(this,mViewPager);
// Tab tab1 = actionBar.newTab().setText("Tab1");
// Tab tab2 = actionBar.newTab().setText("Tab2");
// tabsAdapter.addTab(tab1, Tab1Fragment.class, null);
// tabsAdapter.addTab(tab2, Tab2Fragment.class, null);
Alternative:
//viewpager
mViewPager = new ViewPager(this);
setContentView(mViewPager);
mViewPager.setOnPageChangeListener(
new ViewPager.SimpleOnPageChangeListener() {
#Override
public void onPageSelected(int position) {
// When swiping between pages, select the
// corresponding tab.
super.onPageSelected(position);
getActionBar().setSelectedNavigationItem(position);
}
});
tab_1 = actionBar.newTab().setText("Tab1");
tab_1
.setTabListener(new TabListener<Tab1Fragment>(
this, "Tab1", Tab1Fragment.class,mViewPager));
actionBar.addTab(tab_1);
tab_2 = actionBar.newTab().setText("Tab2");
tab_2
.setTabListener(new TabListener<Tab2Fragment>(
this, "Tab2", Tab2Fragment.class,mViewPager));
actionBar.addTab(tab_2);
/**
* TabListener
* #param <T>
*/
private static class TabListener<T extends Fragment> implements ActionBar.TabListener
{
private Fragment mFragment;
private Activity mActivity;
private final String mTag;
private final Class<T> mClass;
private ViewPager vp;
public TabListener(Activity activity, String tag, Class<T> clz, ViewPager vp) {
mActivity = activity;
mTag = tag;
mClass = clz;
mFragment = mActivity.getFragmentManager().findFragmentByTag(mTag);
this.vp = vp;
}
public void onTabSelected(Tab tab, FragmentTransaction ft) {
if (mFragment == null) {
mFragment = Fragment.instantiate(mActivity, mClass.getName());
ft.replace(android.R.id.content, mFragment, mTag);
} else {
if (mFragment.isDetached()) {
ft.attach(mFragment);
}
}
vp.setCurrentItem(tab.getPosition());
}
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
if (mFragment != null) {
ft.detach(mFragment);
}
}
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
}
In the Fragments classes i have statements like:
Bundle args = new Bundle();
args.putString("name", atn.getName());
args.putLong("aid", atn.getId());
AFragment f = new AFragment();
f.setArguments(args);
f.show(getFragmentManager(), "tag");
FragmentManager fragmentManager =mActivity.getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
Fragment f = fragmentManager.findFragmentByTag("Tab1");
if (f != null) {
fragmentTransaction.detach(f);
fragmentTransaction.attach(f);
}
fragmentTransaction.replace(android.R.id.content, f);
fragmentTransaction. commitAllowingStateLoss();
which are not compatible with the other Fragment type (import android.support.v4.app.Fragment;). How can i easily add the swipe gesture? Selection on Tabs works perfectly.
EDIT:
With support package 13 the view pager worked but the content isnt refreshed. I use FragmentStatePagerAdapter so the Fragments should be removed and added again but instead it takes the same fragment without new creation. Also Viewpage named teh Fragments like
android:switcher... can i name them on my own?
import android.support.v13.app.FragmentStatePagerAdapter;
public class TabsAdapter extends FragmentStatePagerAdapter
implements ActionBar.TabListener, ViewPager.OnPageChangeListener {
private final Context mContext;
private final ActionBar mActionBar;
private final ViewPager mViewPager;
private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>();
private Fragment mFragment;
private Activity mActivity;
private String mTag;
private final List<Fragment> fragments = new ArrayList<Fragment>();
static final class TabInfo {
private final Class<?> clss;
private final Bundle args;
private final String name;
TabInfo(Class<?> _class, Bundle _args,String name) {
clss = _class;
args = _args;
this.name=name;
}
}
public TabsAdapter(Activity activity, ViewPager pager) {
super(activity.getFragmentManager());
mContext = activity;
mActionBar = activity.getActionBar();
mViewPager = pager;
mViewPager.setAdapter(this);
mViewPager.setOnPageChangeListener(this);
mActivity = activity;
}
public void addTab(ActionBar.Tab tab, Class<?> clss, Bundle args) {
TabInfo info = new TabInfo(clss, args,tab.getText()+"");
tab.setTag(info.name);
tab.setTabListener(this);
mTabs.add(info);
mActionBar.addTab(tab);
notifyDataSetChanged();
}
#Override
public int getCount() {
return mTabs.size();
}
#Override
public int getItemPosition(Object object) {
return POSITION_NONE;
// return super.getItemPosition(object);
}
#Override
public Fragment getItem(int position) {
TabInfo info = mTabs.get(position);
Fragment fr = Fragment.instantiate(mContext, info.clss.getName(), info.args);
//
// //addFragment (fr, position);
return fr;
}
public void addFragment(Fragment f, int location) {
if (fragments.size() == 0)
fragments.add(f);
else
fragments.add(location, f);
}
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
mActionBar.setSelectedNavigationItem(position);
}
#Override
public void onPageScrollStateChanged(int state) {
}
#Override
public void onTabSelected(ActionBar.Tab tab, android.app.FragmentTransaction ft) {
Object tag = tab.getTag();
for (int i=0; i<mTabs.size(); i++) {
if (mTabs.get(i).name == tag) {
updateDataSet(i);
mViewPager.setCurrentItem(i);
}
}
}
#Override
public void onTabReselected(ActionBar.Tab tab, android.app.FragmentTransaction ft) {
// TODO Auto-generated method stub
}
#Override
public void onTabUnselected(ActionBar.Tab tab, android.app.FragmentTransaction ft) {
// TODO Auto-generated method stub
}
public void updateDataSet(int pos)
{
//Let's update the dataset for the selected genre
Fragment fragment =
(mActivity.getFragmentManager().findFragmentByTag(
"android:switcher:"+ mViewPager.getId()+":"+pos));
//TabFragment fragment = (TabFragment) getItem(pos);
if(fragment != null) // could be null if not instantiated yet
{
if(fragment.getView() != null)
{
// no need to call if fragment's onDestroyView()
//has since been called.
if(fragment instanceof Tab1Fragment){
((Tab1Fragment) fragment).refresh();
}
else if(fragment instanceof Tab2Fragment){
( (Tab2Fragment) fragment).refresh();
}
}
}
}
Try using the build it Tab Activity template, create an example and extract the code you want from there. I created a tabs based app, but I used the default options and customized those to fit my needs.
Check this open source proyect https://github.com/dkim0419/SoundRecorder
they use one activity with a viewPager and some fragments.
Also the use PagerSlidingTabStrip for page indicator.
Take a look it's very easy how they implemented it.