Android Fragment Content Lost - android

i have a problem on android eclipse with fragments in a viewpager. When an home activity starts i show an actionbar tabs and first fragment on the screen. When i swipe to next fragment and then back to first fragment, first fragment contents are still on the screen. But on the other hand if a swipe 2 times to right and go back first fragment, its contents are lost and blank screen is shown. My codes are below. How to retain each fragment content either it is shown or is visible ? any help is appreciated. Thanx in advance.
HomeActivity.java
public class HomeActivity extends FragmentActivity implements TabListener {
private String[] menuItems = null;
private ActionBar bar = null;
private ViewPager viewPager = null;
private TabsAdapter tabsAdapter = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
viewPager = (ViewPager) findViewById(R.id.viewPager);
viewPager.setPageMargin(10);
menuItems = getResources().getStringArray(R.array.menuTitles);
bar = getActionBar();
bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
tabsAdapter = new TabsAdapter(this, viewPager);
for (int i = 0; i < menuItems.length; i++) {
switch (i) {
case 0:
tabsAdapter.addTab(bar.newTab().setText(menuItems[i]),
NewsFeedActivity.class, null);
break;
case 1:
tabsAdapter.addTab(bar.newTab().setText(menuItems[i]),
NotificationActivity.class, null);
break;
case 2:
tabsAdapter.addTab(bar.newTab().setText(menuItems[i]),
NewsFeedActivity.class, null);
break;
case 3:
tabsAdapter.addTab(bar.newTab().setText(menuItems[i]),
NotificationActivity.class, null);
break;
case 4:
tabsAdapter.addTab(bar.newTab().setText(menuItems[i]),
NewsFeedActivity.class, null);
break;
default:
break;
}
}
if (savedInstanceState != null) {
bar.setSelectedNavigationItem(savedInstanceState.getInt("tab", 0));
}
}
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("tab", getActionBar().getSelectedNavigationIndex());
}
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState)
{
super.onRestoreInstanceState(savedInstanceState);
// Restore selected tab
int saved = savedInstanceState.getInt("tab", 0);
if (saved != getActionBar().getSelectedNavigationIndex())
getActionBar().setSelectedNavigationItem(saved);
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
public static class TabsAdapter extends FragmentPagerAdapter 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>();
static final class TabInfo {
private final Class<?> clss;
private final Bundle args;
TabInfo(Class<?> _class, Bundle _args) {
clss = _class;
args = _args;
}
}
public TabsAdapter(FragmentActivity activity, ViewPager pager) {
super(activity.getSupportFragmentManager());
mContext = activity;
mActionBar = activity.getActionBar();
mViewPager = pager;
mViewPager.setAdapter(this);
mViewPager.setOnPageChangeListener(this);
}
public void addTab(ActionBar.Tab tab, Class<?> clss, Bundle args) {
TabInfo info = new TabInfo(clss, args);
tab.setTag(info);
tab.setTabListener(this);
mTabs.add(info);
mActionBar.addTab(tab);
notifyDataSetChanged();
}
#Override
public void onPageScrollStateChanged(int state) {
}
#Override
public void onPageScrolled(int position, float positionOffset,
int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
mActionBar.setSelectedNavigationItem(position);
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
Toast.makeText(mContext, "Reselected!", Toast.LENGTH_SHORT).show();
Object tag = tab.getTag();
for (int i = 0; i < mTabs.size(); i++) {
if (mTabs.get(i) == tag) {
mViewPager.setCurrentItem(i);
}
}
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
Toast.makeText(mContext, "gokay!", Toast.LENGTH_SHORT).show();
Object tag = tab.getTag();
for (int i = 0; i < mTabs.size(); i++) {
if (mTabs.get(i) == tag) {
mViewPager.setCurrentItem(i);
}
}
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}
#Override
public Fragment getItem(int position) {
TabInfo info = mTabs.get(position);
return Fragment.instantiate(mContext, info.clss.getName(),
info.args);
}
#Override
public int getCount() {
return mTabs.size();
}
}
}
NewsFeedActivity.java
public class NewsFeedActivity extends Fragment {
static final String URL = "http://api.androidhive.info/pizza/?format=xml";
// XML node keys
static final String KEY_ITEM = "item"; // parent node
static final String KEY_ID = "id";
static final String KEY_NAME = "name";
static final String KEY_COST = "cost";
static final String KEY_DESC = "description";
private ArrayList<String> xmlList = null;
private XMLParser parser = null;
private String xml = "";
private Document doc = null;
private NodeList nl = null;
private View view = null;
private Element e = null;
private ListView listNewsFeed = null;
private static Bundle args = null;
private static NewsFeedActivity newsFeed = null;
private ArrayAdapter<String> adapter = null;
public static NewsFeedActivity newInstance(int page, String title) {
newsFeed = new NewsFeedActivity();
args = new Bundle();
args.putInt("Page", page);
args.putString("newsFeed", title);
newsFeed.setArguments(args);
return newsFeed;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.activity_news_feed, container, false);
if (getActivity().getActionBar().getSelectedTab().getPosition() == 0) {
new NewsFeeder().execute("");
}
return view;
}
private class NewsFeeder extends AsyncTask<String, String, String> {
private ProgressDialog mProgressDialog = null;
#Override
protected void onPreExecute() {
super.onPreExecute();
mProgressDialog = new ProgressDialog(getActivity());
mProgressDialog.setMessage("Haber kaynağı yenileniyor..");
mProgressDialog.setCancelable(false);
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
mProgressDialog.show();
}
#Override
protected String doInBackground(String... params) {
xmlList = new ArrayList<String>();
try {
parser = new XMLParser();
xml = parser.getXmlFromUrl(URL); // getting XML
Log.i("gokay", xml);
doc = parser.getDomElement(xml); // getting DOM element
nl = doc.getElementsByTagName(KEY_ITEM);
return null;
} catch (Exception e) {
e.printStackTrace();
System.out.println(e.getMessage());
Log.e("gokay", e.getMessage());
return null;
}
}
#Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
mProgressDialog.dismiss();
for (int i = 0; i < nl.getLength(); i++) {
e = (Element) nl.item(i);
xmlList.add(parser.getValue(e, KEY_ID));
xmlList.add(parser.getValue(e, KEY_NAME));
xmlList.add("Rs." + parser.getValue(e, KEY_COST));
xmlList.add(parser.getValue(e, KEY_DESC));
}
listNewsFeed = (ListView) view.findViewById(R.id.listView1);
adapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, xmlList);
listNewsFeed.setAdapter(adapter);
// onFoodMenuItemsDBAddFinished();
}
}
}

try using
setRetainInstance(true);
in oncreate() fragment

Related

Android - Issue with Fragment's SavedInstance

I've checked the document for a change and a possible fix, but now I'm getting activity ComponentInfo error with the new code that is provided by the link you've given. Edited code below.
MainActivity.java
public class TabMainActivity extends Activity {
ActionBar.Tab Tab1, Tab2, Tab3;
FragmentTab1 fragmentTab1 = new FragmentTab1();
FragmentTab2 fragmentTab2 = new FragmentTab2();
FragmentTab3 fragmentTab3 = new FragmentTab3();
ArrayList<ListData> dataArray;
ListView listView;
ArrayList<DATA> data = new ArrayList<DATA>();
private static String url = "http://80.240.142.93/nokta/casestudy.json";
private static final String TAG_VIDEO = "videos";
private static final String TAG_TITLE = "title";
private static final String TAG_THUMB = "thumbnail";
private static final String TAG_LINK = "video_url";
private ArrayList<HashMap<String, String>> videosList;
Bundle bund = new Bundle();
private Intent intent;
private JSONArray listArray;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
bund.putInt("love", 5);
fragmentTab1.setArguments(bund);
setContentView(R.layout.tabmainactivity);
videosList = new ArrayList<HashMap<String,String>>();
if (isNetworkAvailable()){
new JSONParse().execute();
}else{
showMessage("There is no internet connection");
}
android.app.FragmentManager fm = getFragmentManager();
android.app.FragmentTransaction ft = fm.beginTransaction();
//ft.replace(R.id.fragment_container, fragmentTab1);
//ft.addToBackStack(null);
//ft.commit();
ActionBar actionBar = getActionBar();
// Hide Actionbar Icon
actionBar.setDisplayShowHomeEnabled(false);
// Hide Actionbar Title
actionBar.setDisplayShowTitleEnabled(false);
// Create Actionbar Tabs
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
Tab1 = actionBar.newTab().setText("TAB1").setTabListener(new TabListener<FragmentTab1>(this,"tab1",FragmentTab1.class));
actionBar.addTab(Tab1);
Tab2 = actionBar.newTab().setText("TAB2").setTabListener(new TabListener<FragmentTab2>(this,"tab2",FragmentTab2.class));
actionBar.addTab(Tab2);
Tab3 = actionBar.newTab().setText("TAB3").setTabListener(new TabListener<FragmentTab3>(this,"tab3",FragmentTab3.class));
actionBar.addTab(Tab3);
//ListView listV = fragmentTab1.getListView();
}
private class JSONParse extends AsyncTask<String, String, JSONObject> {
#Override
protected void onPreExecute() {
super.onPreExecute();
}
#Override
protected JSONObject doInBackground(String... args) {
JSONParser jParser = new JSONParser();
// Getting JSON from URL
JSONObject json = jParser.getJSONfromURL(url);
return json;
}
#Override
protected void onPostExecute(JSONObject json) {
try {
listArray = json.getJSONArray(TAG_VIDEO);
{
prepareData(json);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
private boolean isNetworkAvailable() {
ConnectivityManager connectivityManager
= (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}
private void showMessage(String txt){
Toast.makeText(this, txt, Toast.LENGTH_LONG).show();
}
private void prepareData(JSONObject json){
try {
listArray = json.getJSONArray(TAG_VIDEO);
dataArray = new ArrayList<ListData>();
for (int i = 0; i < listArray.length(); i++){
// tmp hashmap for single contact
HashMap<String, String> video = new HashMap<String, String>();
JSONObject c = listArray.getJSONObject(i);
String jpeg_s = c.getString(TAG_THUMB);
String title_s = c.getString(TAG_TITLE);
String video_s = c.getString(TAG_LINK);
video.put(TAG_THUMB, jpeg_s);
video.put(TAG_TITLE, title_s);
video.put(TAG_LINK, video_s);
videosList.add(video);
try {
URL newurl = new URL(jpeg_s);
Bitmap mIcon_val;
mIcon_val = BitmapFactory.decodeStream(newurl.openConnection().getInputStream());
ListData prepare_data = new ListData();
prepare_data.setImage(mIcon_val);
prepare_data.setName(title_s);
dataArray.add(prepare_data);
Log.e("BitMap", mIcon_val.toString());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//android.app.FragmentManager fm = getFragmentManager();
//android.app.FragmentTransaction ft = fm.beginTransaction();
//ft.replace(R.id.fragment_container, fragmentTab1);
//ft.addToBackStack(null);
//ft.commit();
ListView lw = fragmentTab1.getListView();
lw.setAdapter(new CustomAdapter(this, dataArray));
} catch (JSONException e) {
e.printStackTrace();
}
/*
listView.setAdapter(new CustomAdapter(this, dataArray));
Log.e("LW", listView.getItemAtPosition(1).toString());*/
Log.e("returnJSON", videosList.toString());
}
}
TabListener.java
public class TabListener<T extends Fragment> implements ActionBar.TabListener {
private Fragment mFragment;
private final Activity mActivity;
private final String mTag;
private final Class<T> mClass;
public TabListener(Activity activity, String tag, Class<T> clz) {
mActivity = activity;
mTag = tag;
mClass = clz;
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
String backStateName = mFragment.getTag();
if (mFragment == null) {
// If not, instantiate and add it to the activity
mFragment = Fragment.instantiate(mActivity, mClass.getName());
ft.add(android.R.id.content, mFragment, mTag);
} else {
// If it exists, simply attach it in order to show it
ft.attach(mFragment);
}
Log.e("BAKSTAE", backStateName);
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
if (mFragment != null) {
// Detach the fragment, because another one is being attached
ft.detach(mFragment);
}
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
}
Anyone sees the problem?
Thanks.
The problem is that you are replacing the Fragment when you switch tabs, and that is telling Android that you don't need the previous fragment any more. Thus when you switch back to it, the data it had is gone.
The docs here have a good example of how to implement your TabListener:
http://developer.android.com/guide/topics/ui/actionbar.html#Tabs
In their example, they delay instantiating the Fragment instance (rather that creating all the Fragments in field initializers like you are doing). Then they call ft.add if the fragment is new, and ft.attach if it is not. Also, when the tab is unselected, they call ft.detach.
try the following code
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle state) {
if (mMyView == null) {
mMyView = new MyView(getActivity());
} else {
((ViewGroup) mMyView.getParent()).removeView(mMyView);
}
return mPuzzleView;
}

Android json viewpager action bar fragmentpageadapter

I have created action bar and viewpager, I have three fragment. Each of the fragment I parsed json and can show each listview . My problem is : If I click second fragment and then I go back(click firs fragmet) when I go to the next fragment and then if I go to a front fragment at this situation Loading information from the server occurs twice, I use AsyncTask class to load info from server.
Below is a my code :
public class TabsPagerAdapter extends FragmentPagerAdapter {
public TabsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int index) {
switch (index) {
case 0:
return new SendItemsFragment();
case 1:
return new RecivedItemsFragment();
case 2:
return new FavoriteItemsFragment();
}
return null;
}
#Override
public int getCount() {
return 3;
}
public class MainActivity extends FragmentActivity implements TabListener {
private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private ActionBar actionBar;
private String[] tabs = { "test1", "test2", "test3" };
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
actionBar = getActionBar();
actionBar.setHomeButtonEnabled(false);
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setIcon(R.color.white);
actionBar.setDisplayShowTitleEnabled(true);
Drawable d = getResources().getDrawable(R.drawable.acttitle);
getActionBar().setBackgroundDrawable(d);
mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM
| ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_SHOW_TITLE);
viewPager = (ViewPager) findViewById(R.id.vp_main);
viewPager.setAdapter(mAdapter);
getActionBar().setCustomView(R.layout.menu_example);
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM
| ActionBar.DISPLAY_SHOW_HOME);
for (String tab_name : tabs) {
actionBar.addTab(actionBar.newTab().setText(tab_name)
.setTabListener(this));
}
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
#Override
public void onPageScrollStateChanged(int arg0) {
}
});
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// actionBar.setStackedBackgroundDrawable(getResources().getDrawable(
// R.drawable.background)); background viewpager
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}
}
This is - one of the fragment's code. Similar to all three of them. The only difference is that the load of data from the server, And also use the AsyncTask class, each of which had a different.
public class SendItemsFragment extends Fragment {
private String URL = "*******************************************";
public static String KEY_title = "title";
public static String KEY_description = "description";
public static String KEY_image = "image";
public static String KEY_journal = "journal";
public static String KEY_JournalID = "JournalID";
public static String KEY_pubDate = "pubDate";
public static String KEY_statID = "statID";
public JSONArray jsonarray;
public ListView list;
public TransparentProgressDialog pd;
public JSONParser jsonparser;
static DealBoxAdapter adapter;
ProgressDialog pDialog, pDialog1;
static String fontPath2 = "font.ttf";
public static Typeface tf2;
ArrayList<HashMap<String, String>> itemList = new ArrayList<HashMap<String, String>>();
public ImageLoader imageLoader;
static final int DIALOG_ERROR_CONNECTION = 1;
private int screenSize;
private LoadDataAllChanelsToServer loader;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.send_items, container, false);
list = (ListView) rootView.findViewById(R.id.listView1);
pd = new TransparentProgressDialog(getActivity(), R.drawable.loader);
screenSize = getResources().getConfiguration().screenLayout
& Configuration.SCREENLAYOUT_SIZE_MASK;
loader=new LoadDataAllChanelsToServer();
loader.execute();
return rootView;
}
private class LoadDataAllChanelsToServer extends
AsyncTask<String, Integer, String> {
#Override
protected void onPreExecute() {
pd.show();
}
#Override
protected String doInBackground(String... urls) {
jsonparser = new JSONParser();
JSONObject jsonobject = jsonparser.getJSONfromURL(URL);
try {
jsonarray = jsonobject.getJSONArray("data");
for (int i = 0; i < jsonarray.length(); i++) {
jsonobject = jsonarray.getJSONObject(i);
HashMap<String, String> map = new HashMap<String, String>();
map.put("journal", jsonobject.getString(KEY_journal));
map.put("image", jsonobject.getString(KEY_image));
map.put("title", jsonobject.getString(KEY_title));
map.put("description",
jsonobject.getString(KEY_description));
map.put("JournalID", jsonobject.getString(KEY_JournalID));
map.put("pubDate", jsonobject.getString(KEY_pubDate));
map.put("statID", jsonobject.getString(KEY_statID));
itemList.add(map);
}
} catch (JSONException e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return itemList.toString();
}
#Override
protected void onPostExecute(String result) {
try {
if (pd != null) {
pd.dismiss();
}
} catch (Exception e) {
}
try {
adapter = new DealBoxAdapter(getActivity(), itemList,
screenSize);
list.setAdapter(adapter);
} catch (NullPointerException e) {
e.printStackTrace();
}
}
}
}
that's all :)
if anyone knows solution please help me
thank you
You should cancel async task when your Fragment is destroyed.
private LoadDataAllChanelsToServer mLoader;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
...
mLoader = new LoadDataAllChanelsToServer();
mLoader.execute();
...
}
#Overide
public onDestroy() {
super.onDestroy();
if(mLoader != null) {
mLoader.cancel(true);
mLoader = null;
}
this way when fragment is destroyed AsyncTask will be canceled.
You can also set ViewPager.setOffscreenPageLimit(2). This way, since you have only 3 Fragments, no Fragments will be destroy and therefore your onViewCreated will only be called once.

Reload the data in fragment when every time I click the tab in android

I have create an app the layout have three tab in Android 2.3. Each tab has one fragment, and in each fragment, there has a list view. All the data show in list view is from internet, the data always change, so I want to reload the data every time when I watch that fragment. For example, I am watching the "tab1" now, when I click "tab2" the data in "tab2" will reload, and when I click back to "tab1" the data in "tab1" will also reload again. I tried
ft.detach(frg);
ft.attach(frg);
ft.commit();
but always have errors.
Here is my codes.
MainActivity
public class MainActivity extends ActionBarActivity {
private ViewPager pager;
private TabsAdapter mTabsAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pager = new ViewPager(this);
pager.setId(R.id.pager);
setContentView(pager);
final ActionBar bar = getSupportActionBar();
bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
mTabsAdapter = new TabsAdapter(this, getSupportActionBar(), pager);
mTabsAdapter.addTab(bar.newTab().setText("tab1"), Tab1.class, null);
mTabsAdapter.addTab(bar.newTab().setText("tab2"), Tab2.class, null);
mTabsAdapter.addTab(bar.newTab().setText("tab3"), Tab3.class, null);
}
}
Tab1
public class Tab1 extends ListFragment{
private String result;
protected ArrayAdapter listAdapter;
private ArrayList<String> items = new ArrayList<String>();
final String uri = "http://localhost/sample.php";
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View rootView = inflater.inflate(R.layout.list, container, false);
items.clear();
Thread t = new Thread(new sendPostRunnable());
t.start();
listAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, items);
setListAdapter(listAdapter);
return rootView;
}
class sendPostRunnable implements Runnable{
#Override
public void run(){
result = sendPostDataToInternet();
String temp = "Not result.";
try {
JSONTokener jsonTokener = new JSONTokener(result);
JSONObject jsonObject = (JSONObject) jsonTokener.nextValue();
JSONArray jarray = jsonObject.getJSONArray("response");
for (int i = 0; i < jarray.length(); i++) {
temp = "";
JSONObject jobject = jarray.getJSONObject(i);
temp += "Name: "+jobject.getString("name")+"\n";
temp += "Email: "+jobject.getString("email");
items.add(temp);
temp = "";
}
if(jarray.length() < 1){
items.add(temp);
}
} catch (JSONException e) {
e.printStackTrace();
}
getActivity().runOnUiThread(new Runnable() {
public void run() {
listAdapter.notifyDataSetChanged();
}
});
}
}
private String sendPostDataToInternet(){
HttpPost httpRequest = new HttpPost(uri);
List<NameValuePair> params = new ArrayList<NameValuePair>();
try{
httpRequest.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
HttpResponse httpResponse = new DefaultHttpClient().execute(httpRequest);
if (httpResponse.getStatusLine().getStatusCode() == 200){
String strResult = EntityUtils.toString(httpResponse.getEntity());
return strResult;
}
} catch (Exception e){
e.printStackTrace();
}
return null;
}
}
TabsAdapter
public class TabsAdapter extends FragmentPagerAdapter implements TabListener, OnPageChangeListener{
private final Context mContext;
private final ActionBar mActionBar;
private final ViewPager mViewPager;
private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>();
private final String TAG = "";
static final class TabInfo{
private final Class<?> clss;
private final Bundle args;
TabInfo(Class<?> _class, Bundle _args){
clss = _class;
args = _args;
}
}
public TabsAdapter(FragmentActivity activity, ActionBar bar, ViewPager pager) {
super(activity.getSupportFragmentManager());
mContext = activity;
mActionBar = bar;
mViewPager = pager;
mViewPager.setAdapter(this);
mViewPager.setOnPageChangeListener(this);
}
public void addTab(Tab tab, Class<?> clss, Bundle args){
TabInfo info = new TabInfo(clss, args);
tab.setTag(info);
tab.setTabListener(this);
mTabs.add(info);
mActionBar.addTab(tab);
notifyDataSetChanged();
}
#Override
public void onPageSelected(int position) {
mActionBar.setSelectedNavigationItem(position);
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
mViewPager.setCurrentItem(tab.getPosition());
Log.v(TAG, "clicked");
Object tag = tab.getTag();
for (int i = 0; i<mTabs.size(); i++){
if (mTabs.get(i) == tag){
mViewPager.setCurrentItem(i);
}
}
}
#Override
public Fragment getItem(int position) {
TabInfo info = mTabs.get(position);
return Fragment.instantiate(mContext, info.clss.getName(), info.args);
}
#Override
public int getCount() {
return mTabs.size();
}
}
Impelement some interface like OnMyRefreshListener in your fragment and then:
public void onPageSelected(int position) {
((OnMyRefreshListener)pagerViewAdapter.getItem()).onMyRefresh();
}
I am facing same problem in my previous app.
No more changes just add one line in your MainActivity.java
viewPager.setOffscreenPageLimit();
if you have a three tab then put viewPager.setOffscreenPageLimit(3);.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.item_list_tabs);
viewPager = (ViewPager) findViewById(R.id.viewpager);
viewPager.setOffscreenPageLimit(4);
setupViewPager(viewPager);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
setupTabIcons();
}
Document

Viewpager and Sherlock Fragments. How to do somethng inside fragment from FragmentActivity?

I have a parent SherlockFragmentActivity class that contains ViewPager with 4 Fragments inside.
One of them extends from SherlockListFragment and I want to scroll it to top by click on it's tab.
MainActivity class:
public class MainActivity extends SherlockFragmentActivity {
ViewPager viewPager;
TabAdapter tabAdapter;
#Override
public void onCreate(Bundle savedInstanceState) {
setTheme(Constants.THEME);
viewPager = new ViewPager(this);
viewPager.setId(R.id.pager);
viewPager.setBackgroundResource(R.color.background_color);
tabAdapter = new TabAdapter(this,viewPager);
super.onCreate(savedInstanceState);
setContentView(viewPager);
// Action bar setup
setupTabs(savedInstanceState);
}
private void setupTabs(Bundle savedInstanceState) {
final ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
for (int i = 1; i <= 4; i++) {
ActionBar.Tab tab = getSupportActionBar().newTab();
switch (i) {
case 1:
tab.setText(R.string.feed).setTabListener(tabAdapter);
tabAdapter.addTab(EventListFragment.class);
break;
case 2:
tab.setText(R.string.downloads).setTabListener(tabAdapter);
tabAdapter.addTab(LocalEventListFragment.class);
break;
case 3:
tab.setText(R.string.tags).setTabListener(tabAdapter);
tabAdapter.addTab(TagsFragment.class);
break;
case 4:
tab.setText(R.string.settings).setTabListener(tabAdapter);
tabAdapter.addTab(SettingsFragment.class);
break;
}
getSupportActionBar().addTab(tab);
}
}
TabAdapter class:
public static class TabAdapter extends FragmentPagerAdapter implements ViewPager.OnPageChangeListener, ActionBar.TabListener {
private final Context mContext;
private final ArrayList<Class<?>> classes = new ArrayList<Class<?>>();
private final ArrayList<Fragment> mFragments = new ArrayList<Fragment>();
private final ActionBar mActionBar;
private final ViewPager mViewPager;
public TabAdapter(SherlockFragmentActivity activity, ViewPager pager){
super(activity.getSupportFragmentManager());
mContext = activity;
mActionBar = activity.getSupportActionBar();
mViewPager = pager;
mViewPager.setAdapter(this);
mViewPager.setOnPageChangeListener(this);
}
public void addTab(Class<?> clss){
classes.add(clss);
mFragments.add(Fragment.instantiate(mContext, clss.getName(),
null));
}
#Override
public Fragment getItem(int i) {
return mFragments.get(i);
}
public int getId(int index){
return mFragments.get(index).getId();
}
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
// fixed double call onTabReselected
if (mActionBar.getSelectedNavigationIndex() != position)
mActionBar.setSelectedNavigationItem(position);
}
#Override
public void onPageScrollStateChanged(int i) {
}
#Override
public int getCount() {
return 4;
}
#Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
mViewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
}
#Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
final int position = tab.getPosition();
final Fragment fragment = mFragments.get(position);
if (fragment != null) {
switch (position) {
case 0:
// call tab's ListFragment scroll to top item
((EventListFragment) fragment).scrollToTop();
break;
case 1:
// do something
break;
}
}
}
onTabReselected called, then user press current tab, so I try to do EventListFragment scrolling:
EventListFragment class:
public class EventListFragment extends SherlockListFragment {
#Override
public void onCreate(Bundle state) {
super.onCreate(state);
mAdapter = new EventListAdapter(getSherlockActivity());
setListAdapter(mAdapter);
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setRetainInstance(true);
}
#Override
public final View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View eventListLayout = inflater.inflate(R.layout.eventlist_fragment,null);
ListView lv = (ListView) eventListLayout.findViewById(android.R.id.list);
ViewGroup parent = (ViewGroup) lv.getParent();
//Remove ListView and add PullToRefreshListView in its place
int lvIndex = parent.indexOfChild(lv);
parent.removeViewAt(lvIndex);
mPullToRefreshListView = onCreatePullToRefreshListView(inflater, savedInstanceState);
parent.addView(mPullToRefreshListView, lvIndex, lv.getLayoutParams());
return eventListLayout;
}
public void scrollToTop() {
if (getSherlockActivity() != null) { // null after screen rotation
final ListView listView = getListView();
listView.post(new Runnable(){
public void run() {
listView.smoothScrollToPosition(0);
}});
}
}
It works on application start, but after screen rotation scrollToTop - getSherlockActivity() returns null.
If remove this condition check, have an exception:
java.lang.IllegalStateException: Content view not yet created
at android.support.v4.app.ListFragment.ensureList(ListFragment.java:328)
at android.support.v4.app.ListFragment.getListView(ListFragment.java:222)
at com.project.fragment.EventListFragment.scrollToTop(EventListFragment.java:337)
at com.project.MainActivity$TabAdapter.onTabReselected(MainActivity.java:411)
at com.actionbarsherlock.internal.app.ActionBarWrapper$TabWrapper.onTabReselected(ActionBarWrapper.java:327)
I haven't totally understatinding how to ViewPager and its FragmentPagerAdapter works. So can't find a problem, that occurs.
Use this FragmentPageAdapter:
public class TestFragmentAdapter extends FragmentPagerAdapter implements IconPagerAdapter {
protected static final String[] CONTENT = new String[] { "CATEGORIAS", "PRINCIPAL", "AS MELHORES", };
protected static final int[] ICONS = new int[] {
R.drawable.perm_group_calendar,
R.drawable.perm_group_camera,
R.drawable.perm_group_device_alarms,
};
private int mCount = CONTENT.length;
public TestFragmentAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {Fragment f = null;
switch(position){
case 0:
{
f = new ArrayListFragment();//YourFragment
// set arguments here, if required
Bundle args = new Bundle();
f.setArguments(args);
break;
}
case 1:
{
f = new HomeFragment();//YourFragment
// set arguments here, if required
Bundle args = new Bundle();
f.setArguments(args);
break;
}
case 2:
{
f = new EndlessCustomView();//YourFragment
// set arguments here, if required
Bundle args = new Bundle();
f.setArguments(args);
break;
}
default:
throw new IllegalArgumentException("not this many fragments: " + position);
}
return f;
}
#Override
public int getCount() {
return mCount;
}
#Override
public CharSequence getPageTitle(int position) {
return TestFragmentAdapter.CONTENT[position % CONTENT.length];
}
#Override
public int getIconResId(int index) {
return ICONS[index % ICONS.length];
}
public void setCount(int count) {
if (count > 0 && count <= 10) {
mCount = count;
notifyDataSetChanged();
}
}
}
This Activity:
public class BaseSampleActivity extends SlidingFragmentActivity {
private static final Random RANDOM = new Random();
TestFragmentAdapter mAdapter;
ViewPager mPager;
PageIndicator mIndicator;
protected ListFragment mFrag;
#Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.themed_titles);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
mAdapter = new TestFragmentAdapter(getSupportFragmentManager());
mPager = (ViewPager)findViewById(R.id.pager);
mPager.setAdapter(mAdapter);
mPager.setCurrentItem(1);
mIndicator = (TitlePageIndicator)findViewById(R.id.indicator);
mIndicator.setViewPager(mPager);
}
Applied solution according to this post:
MainActivity class:
//...
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
FragmentManager manager = getSupportFragmentManager();
for (int i = 0; i < tabAdapter.getClasses().size(); i++) {
String className = tabAdapter.getClasses().get(i).getName();
if (manager.findFragmentByTag(className) != null) {
manager.putFragment(outState, className, manager.findFragmentByTag(className));
}
}
}
//...
TabAdapter class:
//...
public void addTab(Class<?> clss, FragmentManager manager, Bundle savedInstanceState) {
classes.add(clss);
String className = clss.getName();
tags.add(className);
Fragment fragment;
if (savedInstanceState != null) {
fragment = manager.getFragment(savedInstanceState, className);
if (fragment == null) {
fragment = createFragment(className);
manager.beginTransaction().add(fragment, className);
}
} else {
fragment = createFragment(className);
manager.beginTransaction().add(fragment, className);
}
mFragments.add(fragment);
}
//...
where createFragment is a simple method, that creates needed fragment by passed class name.

Save the fragment state in tabs switch

it is one week I try to solve this problem whitout success. Please help me.
I use the tabs navigation with viewpager. This is the class where I put the tabs and the FragmentPagerAdapter class:
public class Detail extends SherlockFragmentActivity {
ViewPager mViewPager;
TabsAdapter mTabsAdapter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
...
ActionBar bar = getSupportActionBar();
bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
bar.setDisplayOptions(0, ActionBar.DISPLAY_SHOW_TITLE);
mViewPager = (ViewPager)findViewById(R.id.pager);
// Add the tabs
mTabsAdapter = new TabsAdapter(this, bar, mViewPager);
mTabsAdapter.addTab(bar.newTab().setText(R.string.filmtab),
FragmentFilm.class, null);
mTabsAdapter.addTab(bar.newTab().setText(R.string.cinematab),
FragmentCinema.class, null);
mTabsAdapter.addTab(bar.newTab().setText(R.string.dintornitab),
FragmentPdi.class, null);
if (savedInstanceState != null) {
bar.setSelectedNavigationItem(savedInstanceState.getInt("tab"));
}
}
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("tab", getSupportActionBar().getSelectedNavigationIndex());
}
public static class TabsAdapter extends FragmentPagerAdapter
implements ViewPager.OnPageChangeListener, ActionBar.TabListener {
private final Context mContext;
private final ActionBar mBar;
private final ViewPager mViewPager;
private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>();
static final class TabInfo {
private final Class<?> clss;
private final Bundle args;
TabInfo(Class<?> _class, Bundle _args) {
clss = _class;
args = _args;
}
}
public TabsAdapter(Detail activity, ActionBar bar, ViewPager pager) {
super(activity.getSupportFragmentManager());
mContext = activity;
mBar = bar;
mViewPager = pager;
mViewPager.setAdapter(this);
mViewPager.setOnPageChangeListener(this);
}
public void addTab(ActionBar.Tab tab, Class<? extends Fragment> clss, Bundle args) {
TabInfo info = new TabInfo(clss, args);
tab.setTag(info);
tab.setTabListener(this);
mTabs.add(info);
mBar.addTab(tab);
notifyDataSetChanged();
}
#Override
public int getCount() {
return mTabs.size();
}
#Override
public Fragment getItem(int position) {
TabInfo info = mTabs.get(position);
return Fragment.instantiate(mContext, info.clss.getName(), info.args);
}
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
mBar.setSelectedNavigationItem(position);
}
#Override
public void onPageScrollStateChanged(int state) {
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
Object tag = tab.getTag();
for (int i=0; i<mTabs.size(); i++) {
if (mTabs.get(i) == tag) {
mViewPager.setCurrentItem(i);
}
}
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
}
}
The 3 fragment classes are all the same I copy here just one;
In the fragment class I use async task for download the data I need to put in the view, I do this in the onActivityCreated method:
public class FragmentFilm extends SherlockFragment
{
private Detail act;
private DetailedRec detail_film;
private View view;
private String a;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
setRetainInstance(true);
view = inflater.inflate(R.layout.tab_film_info, container, false);
return view;
}
/*
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
}*/
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
act = (Detail) getActivity();
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
new DownloadFilmDetailAsyncTask().execute();
}
private class DownloadFilmDetailAsyncTask extends AsyncTask<Void, DetailedRec, Void>
{
#Override
protected void onPreExecute() {
super.onPreExecute();
}
#Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
ProgressBar prBar = (ProgressBar)getView().findViewById(R.id.progressbar_film);
prBar.setVisibility(View.GONE);
ScrollView lay = (ScrollView)getView().findViewById(R.id.tab_filmsummary);
lay.setVisibility(View.VISIBLE);
}
#Override
protected Void doInBackground(Void... params)
{
try
{
String locale = getResources().getConfiguration().locale.getDisplayName();
JSONObject objSend = new JSONObject();
objSend.put("idFilm", act.getIdFilm());
objSend.put("cinemaId",act.getIdCinema());
int ind = locale.indexOf("(");
String locale_send = locale.substring(0, ind-1);
objSend.put("locale", locale_send);
ArrayList<String> otherCin = new ArrayList<String>(Arrays.asList(act.getOtherCinemas()));
JSONArray othCin = new JSONArray(otherCin);
objSend.put("otherCinemas", othCin );
JSONObject jsonObject = sendAndGetJSONObject(JSON_SERVER+"JsonServer?op=getFilmbyId",objSend);
DetailedRec detail_rec = new DetailedRec();
//FILM
detail_rec.setFilmId(jsonObject.getString("filmId"));
detail_rec.setName(jsonObject.getString("name"));
detail_rec.setImageUrl(jsonObject.getString("imageUrl").replace("640", "80"));
detail_rec.setActors(jsonObject.getString("actors"));
detail_rec.setGenre(jsonObject.getString("genre"));
detail_rec.setDirector(jsonObject.getString("director"));
detail_rec.setPlot(jsonObject.getString("plot"));
detail_rec.setYear(jsonObject.getString("year"));
detail_rec.setDuration(jsonObject.getString("duration"));
detail_rec.setTrailer(jsonObject.getString("trailer"));
detail_rec.setRating(jsonObject.getString("rating"));
detail_film = detail_rec;
publishProgress(detail_rec);
}
catch (IOException ignored)
{
}
catch (JSONException ignored)
{
}
return null;
}
#Override
protected void onProgressUpdate(DetailedRec... values)
{
for (final DetailedRec detail_rec : values)
{
updateViews(detail_rec);
}
}
private JSONObject getJSONObject(String url) throws IOException, MalformedURLException, JSONException
{
HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
InputStream in = conn.getInputStream();
try
{
StringBuilder sb = new StringBuilder();
BufferedReader r = new BufferedReader(new InputStreamReader(new DoneHandlerInputStream(in),"WINDOWS_1252"));
for (String line = r.readLine(); line != null; line = r.readLine())
{
sb.append(line);
}
return new JSONObject(sb.toString());
}
finally
{
in.close();
}
}
private JSONObject sendAndGetJSONObject(String url,JSONObject request) throws IOException, MalformedURLException, JSONException
{
HttpClient client = new DefaultHttpClient();
HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000); //Timeout Limit
InputStream in=null;
try{
HttpPost post = new HttpPost(url);
StringEntity se = new StringEntity(request.toString());
se.setContentType((Header) new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
post.setEntity(se);
HttpResponse response = client.execute(post);
HttpEntity entity = response.getEntity();
in = entity.getContent();
StringBuilder sb = new StringBuilder();
BufferedReader r = new BufferedReader(new InputStreamReader(new DoneHandlerInputStream(in),"WINDOWS_1252"));
for (String line = r.readLine(); line != null; line = r.readLine())
{
sb.append(line);
}
return new JSONObject(sb.toString());
}catch(Exception e){
}
finally
{
in.close();
}
return null;
}
}
private void updateViews(final DetailedRec detail_rec){
//FILM
TextView filmName = (TextView) getView().findViewById(R.id.movieTitle);
filmName.setText(detail_rec.getName().trim());
TextView actors = (TextView) getView().findViewById(R.id.movieActor);
actors.setText(detail_rec.getActors().trim());
TextView genre = (TextView) getView().findViewById(R.id.movieGenre);
genre.setText(detail_rec.getGenre().trim());
TextView director = (TextView) getView().findViewById(R.id.movieDirector);
director.setText(detail_rec.getDirector().trim());
TextView plot = (TextView) getView().findViewById(R.id.moviePlot);
plot.setText(detail_rec.getPlot().trim());
TextView year = (TextView) getView().findViewById(R.id.movieYear);
year.setText(detail_rec.getYear().trim());
TextView duration = (TextView) getView().findViewById(R.id.movieDuration);
duration.setText(detail_rec.getDuration().trim());
ImageView image = (ImageView) getView().findViewById(R.id.moviePoster);
new DownloadImagesTask(detail_rec.getImageUrl().trim().replace("80", "100")).execute(image);
//image.setImageBitmap(downloadBitmap(detail_rec.getImageUrl().trim().replace("80", "100")));
//Rating
if(detail_rec.getRating().compareTo("N/A")!=0){
RatingBar rateBar = (RatingBar)getView().findViewById(R.id.MovieRatingBar);
rateBar.setRating(Float.parseFloat(detail_rec.getRating()));
}
//Trailer
Button trailer = (Button)getView().findViewById(R.id.trailer);
if(detail_rec.getTrailer().compareTo("")!=0){
trailer.setVisibility(View.VISIBLE);
trailer.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
int index = detail_rec.getTrailer().indexOf("v=");
String videoId="";
if(index!=-1){
videoId = detail_rec.getTrailer().substring(index+2); //"Fee5vbFLYM4";
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("vnd.youtube:"+videoId));
intent.putExtra("VIDEO_ID", videoId);
startActivity(intent);
}
}
});
}
}
}
Ok, in my application I want the fragment is load one time. I explain so good:
When I start the Detail class the fragment classes are istantiate and the AsyncTask in all the 3 fragment start, now when the user switch from one tab to other I want the fragment in tab unselected don't lost the data and the view because now when I switch from one tab to another and then return the first tab this is recreate and the onActivityCreated method is called again!
Please help me, I search in all place but I dont find solution!!
P.S. I use the SherlockActionBar, I don't know id it is relevant. Sorry for my bad English
Thank you
Just after instantiating the ViewPager, add this
mViewPager = (ViewPager)findViewById(R.id.pager);
mViewPager.setOffscreenPageLimit(3);
That should do it!

Categories

Resources