Problem Description
Application which I'm writing has 3 Fragments. Main Fragment Activity has a Search Box with Search button when I press on a Search button the function below is called:
Fragments.values()[tabControl.getCurrentItem()].getFragment().search(tv.getText().toString(), MainActivity.this.getApplicationContext());
Everything works Okay until I press HOME button wait for 30 minutes and relaunch application, after I press Search button in FragmentYellowPages
Fragment application crashes on the lines
lvYellowPages.setVisibility(View.VISIBLE);
as lvYellowPages
in this case is null, but In a logs I can see that onCreateView
function is called. Can you please help me, I can share my code with you.
FragmentPagerAdapter
public class FragmentAdapter extends FragmentPagerAdapter {
Fragments[] mFragments;
public FragmentAdapter(FragmentManager fm) {
super(fm);
mFragments = Fragments.values();
}
public enum Fragments {
Favorites(App.getStringByResId(R.string.favorites), new FragmentFavorites()),
Categories(App.getStringByResId(R.string.categories), new FragmentCategories()),
YellowPages(App.getStringByResId(R.string.yellow_pages), new FragmentYellowPages());
Fragments(String title, BaseListFragment fragment) {
this.mTitle = title;
this.mFragment = fragment;
}
String mTitle;
BaseListFragment mFragment;
public String getTitle() {
return mTitle;
}
public BaseListFragment getFragment() {
return mFragment;
}
};
@Override
public CharSequence getPageTitle(int position) {
return mFragments[position].getTitle();
}
@Override
public Fragment getItem(int position) {
return mFragments[position].getFragment();
}
@Override
public int getCount() {
return mFragments.length;
}
};
List Fragment
public class FragmentYellowPages extends BaseListFragment {
final static String TAG = FragmentYellowPages.class.getSimpleName();
ListView lvYellowPages;
TextView tvInfo;
SimpleCursorAdapter scAdapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View yellowPagesView = inflater.inflate(R.layout.fragment_yellow_pages, container, false);
Log.i(TAG, "onCreateView( )");
tvInfo = (TextView) yellowPagesView.findViewById(R.id.tvYellowPagesInfo);
lvYellowPages = (ListView) yellowPagesView.findViewById(android.R.id.list);
return yellowPagesView;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i(TAG, "onCreate( )");
setRetainInstance(true);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Log.i(TAG, "onActivityCreated( )");
}
@Override
public void onResume() {
super.onResume();
Log.i(TAG, "onResume( )");
}
void updateAdapterOnSearch(final String tts, final String lang) {
Log.i(TAG, String.format("updateAdapterOnSearch(%s, %s)", tts, lang));
/* Show "Yellow Pages" list view and hide Info text view. */
lvYellowPages.setVisibility(View.VISIBLE);
tvInfo.setVisibility(View.GONE);
/* Some code .... */
}
@Override
public void search(final String tts, final Context context) {
Log.i(TAG, String.format("search(%s)", tts));
/* If user search for the text. */
if (tts != null && tts.length() != 0) {
updateAdapterOnSearch(tts, lang);
}
}
};
Fragment Layout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffc8"
android:tag="FRAGMENT_YELLOW_PAGES_TAG" >
<ListView android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone"
android:background="#ffffc8"/>
<TextView android:id="@+id/tvYellowPagesInfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:gravity="center"
android:padding="5dp"
android:text="@string/no_companies_were_found"
android:visibility="gone"/>
</RelativeLayout>
mViewPager.setSaveEnabled(false);
in youronCreate
function. Oh and you do not need to wait 30 minutes to reproduce, get a task killer and kill the app then open it. – Rager