Sindri Þór Sindri Þór - 4 months ago 25
Android Question

Trying to launch fragment inside actionBarActivity

I have checked numbers of solutions on how to launch my fragment but nothing has worked so far. I appreciate any help I can get

What I want is to get the content inside the fragment to show.

I have 2 tabs that works but fragment inside each tab doesn´t show it content because the fragment is never lunched. Which is weird becouse fragment extende actionBarActivity. But if I make the mainActivity extende fragmentActivity works but then my tabs don't.

Tab.setCurrentItem(tab.getPosition()); Seems to not do the trick

Theme in androidManifest
android:theme="@style/Theme.AppCompat.Light.DarkActionBar"

MainActivity

import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import is.at.work.lottostokkur.R;
import is.at.work.lottostokkur.adapter.TabPagerAdapter;

public class MainActivity extends ActionBarActivity {

ViewPager Tab;
TabPagerAdapter TabAdapter;
ActionBar actionBar;

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

TabAdapter = new TabPagerAdapter(getSupportFragmentManager());
actionBar = getSupportActionBar();

Tab = (ViewPager)findViewById(R.id.pager);
Tab.addOnPageChangeListener(
new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
actionBar = getSupportActionBar();
actionBar.setSelectedNavigationItem(position);
}
});

//Enable Tabs on Action Bar
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.TabListener tabListener = new ActionBar.TabListener() {

@Override
public void onTabSelected(ActionBar.Tab tab, android.support.v4.app.FragmentTransaction fragmentTransaction) {
Tab.setCurrentItem(tab.getPosition());

}

@Override
public void onTabUnselected(ActionBar.Tab tab, android.support.v4.app.FragmentTransaction fragmentTransaction) {

}

@Override
public void onTabReselected(ActionBar.Tab tab, android.support.v4.app.FragmentTransaction fragmentTransaction) {

}
};
//Add New Tab
actionBar.addTab(actionBar.newTab().setText("Lottó").setTabListener(tabListener));
actionBar.addTab(actionBar.newTab().setText("About").setTabListener(tabListener));
}
}


And my activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:id="@+id/lottoId"
android:orientation="vertical" >

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="50sp"/>

</LinearLayout>


This is the fragment I'm trying to launch

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import is.at.work.lottostokkur.R;

public class LottoFragment extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

View view = inflater.
inflate(R.layout.lotto, container, false);
((TextView)view.findViewById(R.id.textView)).setText("iOS");

return view;
}
}


My adapter

public class TabPagerAdapter extends FragmentStatePagerAdapter {
public TabPagerAdapter(FragmentManager fm) {
super(fm);
// TODO Auto-generated constructor stub
}

@Override
public Fragment getItem(int i) {
switch (i) {
case 0:
//Fragement for Lotto Tab
return new LottoFragment();
case 1:
//Fragment for About Tab
return new AboutFragment();
}
return null;
}

@Override
public int getCount() {
// TODO Auto-generated method stub
return 2; //No of Tabs
}
}

Answer

TLDR;

I can't see the line that you set adapter to ViewPager

in your case you should add following:

Tab.setAdapter(TabAdapter);

First of all, I would recommend using AppCompatActivity since the others are depreciated.

And also I recommend you not to use actionbar.setNavigationMode() method because it is depreciated too.

you have a lot of other options to achive the tab navigation.