I'm having a strange problem using an ActionView in my Actionbar (specifically ActionbarSherlock). This issue is only happening in devices below SDK 3.0 (basically Gingerbread devices is all I'm testing on).
My app has a ViewPager and I am animating one of my menu items while an AsyncTask is running. If I touch the ActionView before swiping everything is ok, if I swipe to any of the views, then go back to the first view, which is the only view to have the ActionView, and touch the ActionView the icon is doubling up on itself, like so:
I had this same issue previously, before I implemented a ViewPager, and I fixed it by stopping and starting the animation in onCreateOptionsMenu and calling invalidateMenuOptions, which is why I have ActivityCompat.invalidateOptionsMenu(this); in onResume of the Activity. I was hoping, by calling that, the menu would refresh itself and fix the duplicating, but it isn't.
UPDATE Using getSherlock().dispatchInvalidateOptionsMenu(); seems to, at least, get the menu to refresh itself, but it causing the ActionView to go nuts.
ViewPager:
public class Main extends SherlockFragmentActivity
{
private static List<Integer> mIds;
private static SparseArray<Fragment> mPageReferenceMap = new SparseArray<Fragment>();
#Override
public void onCreate(final Bundle icicle)
{
super.onCreate(icicle);
setContentView(R.layout.main);
mViewPager = (ViewPager)findViewById(R.id.viewpager);
mMyFragmentPagerAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager());
mViewPager.setAdapter(mMyFragmentPagerAdapter);
mViewPager.setOnPageChangeListener(new OnPageChangeListener() {
#Override
public void onPageSelected(int position) {
GetListingFragment().StopAnimation(); //needed to add this because the ActionView was showing on the other views when swiping
}
#Override
public void onPageScrolled(int position, float offset, int offsetPixel) {
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
mIds = new ArrayList<Integer>();
mIds.add(0);
mIds.add(1);
mIds.add(2);
}
#Override
public void onResume()
{
super.onResume();
ActivityCompat.invalidateOptionsMenu(this);
}
private ListingFragment GetKeywordsFragment()
{
ListingFragment lf = (ListingFragment)getSupportFragmentManager().findFragmentById(R.id.fragmentListing);
if (lf == null)
{
final MyFragmentPagerAdapter fpa = (MyFragmentPagerAdapter)mViewPager.getAdapter();
lf = (ListingFragment)fpa.getFragment(0);
}
return lf;
}
private static class MyFragmentPagerAdapter extends FragmentStatePagerAdapter {
public MyFragmentPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int index) {
if (index == 0)
{
final ListingFragment lf = ListingFragment.newInstance();
mPageReferenceMap.put(index, lf);
return lf;
}
else
{
final DetailFragment df = DetailFragment.newInstance(mIds.get(index));
mPageReferenceMap.put(index, df);
return df;
}
}
public Fragment getFragment(int key) {
return mPageReferenceMap.get(key);
}
#Override
public int getCount() {
return 3;
}
}
}
Fragment:
public class ListFragment extends SherlockListFragment
{
private int mId;
private MenuItem refreshItem;
private AsyncTask<Void, String, Void> gi;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
public static ListingFragment newInstance(int id) {
ListingFragment lf = new ListingFragment();
Bundle bundle = new Bundle();
bundle.putInt("id", id);
lf.setArguments(bundle);
return lf;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (getArguments() != null)
mId = getArguments().getInt("id");
return inflater.inflate(R.layout.listing, container, false);
}
private class GetItems extends AsyncTask<Void, String, Void> {
#Override
protected void onPreExecute()
{
StartAnimation();
}
#Override
protected Void doInBackground(Void... unused)
{
//background process to get items
}
protected void onPostExecute(final Void unused)
{
StopAnimation();
}
}
#Override
public void onCreateOptionsMenu(final Menu menu, final MenuInflater inflater) {
inflater.inflate(R.menu.keyword_menu, menu);
StopAnimation();
refreshItem = menu.findItem(R.id.refresh);
if (fi != null && fi.getStatus() == AsyncTask.Status.RUNNING)
StartAnimation();
super.onCreateOptionsMenu(menu, inflater);
}
private void StartAnimation() {
if (refreshItem != null && refreshItem.getActionView() == null)
{
final LayoutInflater inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final ImageView ivRefresh = (ImageView)inflater.inflate(R.layout.refresh_view, null);
final Animation rotation = AnimationUtils.loadAnimation(activity, R.anim.refresh);
ivRefresh.startAnimation(rotation);
refreshItem.setActionView(ivRefresh);
}
}
public void StopAnimation()
{
if (refreshItem != null && refreshItem.getActionView() != null)
{
refreshItem.getActionView().clearAnimation();
refreshItem.setActionView(null);
}
}
#Override
public boolean onOptionsItemSelected(final MenuItem item)
{
if (item.getItemId() == R.id.getitems) {
gi = new GetItems(getActivity(), null);
return true;
} else {
return super.onOptionsItemSelected(item);
}
}
}
Looks like this is a known bug in ABS:
https://github.com/JakeWharton/ActionBarSherlock/issues/331
Related
Hi i am working in an app, in which:
View pager is there on an activity.
On that view pager i am showing 2 fragments(frag1 and frag2).
on button click on frag1 we have added one more fragment(lets say frag3).
and on back press on frag3 i come back on frag1.
Issue:
the issue is when i come back to frag1 from frag3 on back press, sometimes frag1 is not attached to the activity.
i am not able to figure out how this is happening.
if this is happening then what is the solution so i can stop activity to detach the frag1 or re-initialize the frag1 again.
Please help.
This code may help you
public class PageAdapter extends FragmentPagerAdapter implements IconPagerAdapter {
protected static final int[] PAGER = new int[] {
R.drawable.image1,
R.drawable.image2,
R.drawable.image3,
R.drawable.image4
};
private int mCount = PAGER.length;
public PagerFragmentAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
return new PagerFragment(PAGER[position]);
}
#Override
public int getIconResId(int index) {
return ICONS[index % PAGER.length];
}
#Override
public int getCount() {
return mCount;
}
public void setCount(int count) {
if (count > 0 && count <= 10) {
mCount = count;
notifyDataSetChanged();
}
}
}
Defining Fragments
public final class PagerFragment extends Fragment {
private static final String KEY_CONTENT = "PagerFragment:Content";
int imageSource;
public PagerFragment(int imageSource) {
this.imageSource = imageSource;
}
public PagerFragment() {
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if ((savedInstanceState != null) && savedInstanceState.containsKey(KEY_CONTENT)) {
imageSource = savedInstanceState.getInt(KEY_CONTENT);
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
ViewGroup root = (ViewGroup) inflater.inflate(R.layout.indicatorpage, null);
ImageView image = (ImageView) root.findViewById(R.id.pagerImage);
image.setImageResource(imageSource);
setRetainInstance(true);
return root;
}
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt(KEY_CONTENT, imageSource);
}
}
MainActivity Class
public class MainActivity extends FragmentActivity {
PagerFragmentAdapter mAdapter;
ViewPager mPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mAdapter = new PagerFragmentAdapter(getSupportFragmentManager());
mPager = (ViewPager) findViewById(R.id.pager);
mPager.setAdapter(mAdapter);
mPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
System.out.println("selected page is :" + position);
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
CirclePageIndicator mIndicator = (CirclePageIndicator) findViewById(R.id.indicator);
mIndicator.setViewPager(mPager);
final float density = getResources().getDisplayMetrics().density;
mIndicator.setRadius(7 * density);
mIndicator.setPageColor(0x00000000);
mIndicator.setFillColor(0xFFFFFFFF);
mIndicator.setStrokeColor(0xFFFFFFFF);
mIndicator.setStrokeWidth(1 * density);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
I am trying to use FragmentStatePagerAdapter with ViewPager to achieve a sequential slide scroll view (similar to most pdf readers). However, while running the code, the following is happening:
When the activity becomes visible, the first page is instantiated with a different (2nd) page's value. However, when scrolled to some other page and then back to page 1, the page displays default text from layout file.
Only 2nd and 2nd to last pages instantiate with the passed value (that too with values from other pages, not their own). Rest of the pages display default text from layout file.
On debugging, I noticed that the index/currentItemNumber changes when ViewPager.populate() calls ViewPager.addNewItem(). Ever more strange is the fact that setText() is called on the TextView (part of fragment layout), but text does not change from the default text.
Am I missing something?
Here is the code below:
MainActivity.java
public class MainActivity extends ActionBarActivity implements View.OnClickListener {
private ViewPager mPager;
private Button mButtonFirst;
private Button mButtonPrev;
private Button mButtonGoTo;
private Button mButtonNext;
private Button mButtonLast;
private TextView mPageCount;
private EditText mPageNumber;
private TextView mError;
private int mNumScreens;
private int mCurrScreen;
private MyPagerAdapter mMyPagerAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setUpViews();
setUpListeners();
setUpPager();
}
private void setUpViews() {
mPager = (ViewPager) findViewById(R.id.my_pager);
// Get other view handles...
mError = (TextView) findViewById(R.id.error_details);
}
private void setUpListeners() {
// Set this class as click handler for all buttons
}
private void setUpPager() {
String[] strings = new String[] {
"1",
"2",
"3",
"4",
"5",
"6"
};
// Success!
// Set adapter and update views
mMyPagerAdapter = new MyPagerAdapter(getSupportFragmentManager(), strings);
mPager.setAdapter(mMyPagerAdapter);
mNumScreens = strings.length;
mPageCount.setText("/" + Integer.toString(mNumScreens));
mCurrScreen = -1;
GoToScreen(1);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
// Handle options
return super.onOptionsItemSelected(item);
}
#Override
public void onClick(View v) {
switch(v.getId()) {
case R.id.button_first:
GoToScreen(1);
break;
case R.id.button_prev:
GoToScreen(mCurrScreen - 1);
break;
case R.id.button_goto:
{
int screen;
boolean reset = false;
try {
screen = Integer.parseInt(mPageNumber.getText().toString());
reset = !SetToScreen(screen);
} catch (NumberFormatException e) {
e.printStackTrace();
reset = true;
}
if(reset) {
mPageNumber.setText(Integer.toString(mCurrScreen));
}
}
break;
case R.id.button_next:
GoToScreen(mCurrScreen + 1);
break;
case R.id.button_last:
GoToScreen(mNumScreens);
break;
}
}
private void GoToScreen(int screen) {
if(SetToScreen(screen)) {
mPageNumber.setText(Integer.toString(mCurrScreen));
}
}
private boolean SetToScreen(int screen) {
// Switch to a valid screen
if(screen >= 1 && screen <= mNumScreens && mCurrScreen != screen) {
mPager.setCurrentItem(screen - 1, false);
// Handle button visibility
// Update current screen
mCurrScreen = screen;
return true;
}
return false;
}
MyPagerAdapter.java
public class MyPagerAdapter extends FragmentStatePagerAdapter {
private final String[] mStrings;
public MyPagerAdapter(FragmentManager fm, String[] strings) {
super(fm);
mStrings = strings;
}
#Override
public int getCount() {
return mStrings.length;
}
#Override
public Fragment getItem(int position) {
return ScreenFragment.newInstance(mStrings[position]);
}
}
ScreenFragment.java
public class ScreenFragment extends Fragment {
private static final String ARG_SCREEN_STRING= "screen_string";
private String mScreenInfo;
private TextView mStatementLabel;
public static ScreenFragment newInstance(String screenString) {
ScreenFragment fragment = new ScreenFragment();
Bundle args = new Bundle();
args.putString(ARG_SCREEN_STRING, screenString);
fragment.setArguments(args);
return fragment;
}
public ScreenFragment() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mScreenInfo = getArguments().getString(ARG_SCREEN_STRING);
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_screen, container, false);
}
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setUpViews();
setUpFields();
}
private void setUpFields() {
mStatementLabel.setText(mScreenInfo);
}
private void setUpViews() {
mStatementLabel = (TextView) getActivity().findViewById(R.id.qnr_screen_statement);
}
}
The problem was solved when I moved calls to setUpViews() and setUpFields() to onCreateView() from onActivityCreated(), with little modifications. This is how the new onCreateView() looks like (I moved the content of the above mentioned functions here)
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_screen, container, false);
mStatementLabel = (TextView) v.findViewById(R.id.qnr_screen_statement);
mStatementLabel.setText(mScreenInfo);
return v;
}
I am yet to figure out why that was causing the problem. Will update if I find anything.
I have one SherlockFragmentActivity with 3 fragments. Everything works fine but when I swipe from fragment to fragment, wrong menus are shown. Fragment1 is showing menu from fragment2, SearchView on Fragment1 is filtering data on Fragment2. Here is the code for SherlockFragmentActivity
public class MainHolder extends SherlockFragmentActivity {
private static final String[] CONTENT = new String[] { "FRAGMENT1", "FRAGMENT2", "FRAGMENT3" };
FragmentAdapter mAdapter;
ViewPager mPager;
TabPageIndicator mIndicator;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_holder);
mAdapter = new FragmentAdapter(getSupportFragmentManager());
mPager = (ViewPager)findViewById(R.id.pager);
mPager.setAdapter(mAdapter);
mIndicator = (TabPageIndicator)findViewById(R.id.indicator);
mIndicator.setViewPager(mPager);
}
class FragmentAdapter extends FragmentPagerAdapter {
public FragmentAdapter(FragmentManager fm) {
super(fm);
}
public Fragment getItem(int position) {
switch(position)
{
case 0:
return Fragment1.newInstance(CONTENT[position % CONTENT.length]);
case 1:
return Fragment2.newInstance(CONTENT[position % CONTENT.length]);
case 2:
return Fragment3.newInstance(CONTENT[position % CONTENT.length]);
default:
return null;
}
}
#Override
public CharSequence getPageTitle(int position) {
return CONTENT[position % CONTENT.length].toUpperCase();
}
#Override
public int getCount() {
return CONTENT.length;
}
}
#Override
public void onBackPressed() {
Fragment fragment = (Fragment) getSupportFragmentManager().findFragmentByTag("android:switcher:" + R.id.pager + ":"+mPager.getCurrentItem());
if (fragment != null) // could be null if not instantiated yet
{
if (fragment.getView() != null) {
// Pop the backstack on the ChildManager if there is any. If not, close this activity as normal.
if (!fragment.getChildFragmentManager().popBackStackImmediate()) {
finish();
}
}
}
}
}
And here is for the fragment
public class Fragment1 extends SherlockFragment {
private static final String KEY_CONTENT = "Fragment1:Content";
LazyAdapter fragment1adapter;
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
menu.clear();
inflater.inflate(R.menu.fragment1, menu);
MenuItem searchItem = menu.findItem(R.id.menu_search);
final SearchView searchView = (SearchView) searchItem.getActionView();
searchView.setQueryHint("Pretraga ...");
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextChange(String newText) {
if (newText.length() != 0) {
fragment1adapter.getFilter().filter(newText);
return true;
}
return false;
}
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
});
searchView.setOnCloseListener(new SearchView.OnCloseListener() {
#Override
public boolean onClose() {
fragment1adapter.getFilter().filter("");
return false;
}
});
}
public static Fragment1 newInstance(String content) {
Fragment1 fragment = new Fragment1();
StringBuilder builder = new StringBuilder();
for (int i = 0; i < 20; i++) {
builder.append(content).append(" ");
}
builder.deleteCharAt(builder.length() - 1);
fragment.mContent = builder.toString();
return fragment;
}
private String mContent = "???";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if ((savedInstanceState != null) && savedInstanceState.containsKey(KEY_CONTENT)) {
mContent = savedInstanceState.getString(KEY_CONTENT);
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment1, container, false);
setRetainInstance(true);
setHasOptionsMenu(true);
setMenuVisibility(true);
}
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString(KEY_CONTENT, mContent);
}
}
Other fragments are the same, with their separate menus and layouts. Can anyone give me some hints why is this happening?
The solution is to remove setMenuVisibility(true); from fragments onCreateView.
I am using the following library for my sliding menu (https://github.com/bk138/LibSlideMenu) in my app.
In my app the sliding menu works. I can slide from right to left and the menu will appear. But the problem is that when I am in the menu I can't slide back to the fragment were I came from.
The only way to get back is using back button. Also when you are in the menu I don't have the padding on the right where you see the previous fragment on the background.
I am searching for it like days. I have searched the example for the problem but couldn't find the essential thing that i am forgetting.
My main activity:
public class MainActivity extends SlidingFragmentActivity {
private Fragment rFrag;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setBehindContentView(R.layout.menu_frame);
if (savedInstanceState != null)
rFrag = getSupportFragmentManager().getFragment(savedInstanceState, "mContent");
if (rFrag == null)
rFrag = new RecentGridFragment();
FragmentTransaction fragment = getSupportFragmentManager().beginTransaction();
fragment.replace(R.id.content_frame, rFrag);
MenuFragment mFrag = new MenuFragment();
fragment.replace(R.id.menu_frame, mFrag);
fragment.commit();
//Sliding menu
SlidingMenu sMenu = new SlidingMenu(this);
sMenu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
sMenu.setShadowWidthRes(R.dimen.shadow_width);
sMenu.setShadowDrawable(R.drawable.shadow);
sMenu.setBehindScrollScale(0.25f);
sMenu.setFadeDegree(0.25f);
sMenu.setSlidingEnabled(true);
sMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
return super.onCreateOptionsMenu(menu);
}
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
getSupportFragmentManager().putFragment(outState, "mContent", rFrag);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
toggle();
}
return super.onOptionsItemSelected(item);
}
public void switchContent(final Fragment inputFrag) {
rFrag = inputFrag;
FragmentTransaction fragment = getSupportFragmentManager().beginTransaction();
fragment.replace(R.id.content_frame, inputFrag);
fragment.commit();
Handler h = new Handler();
h.postDelayed(new Runnable() {
public void run() {
getSlidingMenu().showContent();
}
}, 50);
}}
Menu:
public class MenuFragment extends ListFragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.list, null);
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
String[] birds = getResources().getStringArray(R.array.birds);
ArrayAdapter<String> colorAdapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, android.R.id.text1, birds);
setListAdapter(colorAdapter);
}}
The mainfragment that has the content
public class RecentGridFragment extends Fragment {
private int mImgRes;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
mImgRes = R.drawable.peacock;
GridView gv = (GridView) inflater.inflate(R.layout.list_grid, null);
gv.setBackgroundResource(android.R.color.black);
gv.setAdapter(new GridAdapter());
return gv;
}
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
}
private class GridAdapter extends BaseAdapter {
#Override
public int getCount() {
return 30;
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = getActivity().getLayoutInflater().inflate(R.layout.grid_item, null);
}
ImageView img = (ImageView) convertView.findViewById(R.id.grid_item_img);
img.setImageResource(mImgRes);
return convertView;
}
}}
I have found my mistake:
I need to get the slidingactivity instead of making a new one.
SlidingMenu sMenu = this.getSlidingMenu();
My app uses a ViewPager in portrait mode and a dual-fragment layout in landscape.
I am trying to kick off an AsyncTask, that is in the Fragment, from Activity. The AsyncTask is normally started from a menu item in the Actionbar, but I have a need to start it programatically from the Activity.
The menu item is an ImageView and I'm animating it while the AsyncTask is running. The code I have works fine in the dual-fragment landscape view, but I'm getting a NullPointerException on the menu item when in portrait mode.
Activity
public class Main extends SherlockFragmentActivity
{
private static List<Integer> mIds;
private static SparseArray<Fragment> mPageReferenceMap = new SparseArray<Fragment>();
#Override
public void onCreate(final Bundle icicle)
{
super.onCreate(icicle);
setContentView(R.layout.main);
mViewPager = (ViewPager)findViewById(R.id.viewpager); //view pager exists, so we are using the portait layout
if (mViewPager != null)
{
mIds = new ArrayList<Integer>();
mIds.add(0);
mIds.add(1);
mIds.add(2);
}
else //in landscape
{
ListFragment lf = (ListFragment)getSupportFragmentManager().findFragmentById(R.id.fragmentList);
if (lf == null)
lf = new ListFragment();
DetailFragment df = (DetailFragment)getSupportFragmentManager().findFragmentById(R.id.fragmentDetail);
if (df == null)
{
df = new DetailFragment();
df.setArguments(getIntent().getExtras());
}
getSupportFragmentManager().beginTransaction().add(R.id.fragmentList, lf).commit();
getSupportFragmentManager().beginTransaction().add(R.id.fragmentDetail, df).commit();
}
final MyFragmentPagerAdapter fpa = (MyFragmentPagerAdapter)mViewPager.getAdapter();
ListFragment lf2 = (ListFragment)fpa.getFragment(0);
//this works if I use:
//(ListFragment)getSupportFragmentManager().findFragmentById(R.id.fragmentList);
lf2.RunTask();
}
private static class MyFragmentPagerAdapter extends FragmentStatePagerAdapter {
public MyFragmentPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int index) {
if (index == 0)
{
ListFragment lf = ListFragment.newInstance();
mPageReferenceMap.put(index, lf);
return lf;
}
else
{
DetailFragment df = DetailFragment.newInstance(mIds.get(index-1));
mPageReferenceMap.put(index, df);
return df;
}
public Fragment getFragment(int key) {
return mPageReferenceMap.get(key);
}
public void destroyItem(View container, int position, Object object) {
super.destroyItem(container, position, object);
mPageReferenceMap.remove(position);
}
#Override
public int getCount() {
return 4;
}
}
}
Fragment
public class ListingFragment extends SherlockListFragment
{
private MenuItem refreshItem;
public static ListingFragment newInstance() {
ListingFragment lf = new ListingFragment();
return lf;
}
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.listing_layout, container, false);
}
private void StartAnimation() {
final LayoutInflater inflater = (LayoutInflater)getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final ImageView ivRefresh = (ImageView)inflater.inflate(R.layout.refresh_view, null);
final Animation rotation = AnimationUtils.loadAnimation(getActivity(), R.anim.refresh);
ivRefresh.startAnimation(rotation);
//this is null
refreshItem.setActionView(ivRefresh);
}
public void StopAnimation()
{
if (refreshItem != null && refreshItem.getActionView() != null)
{
refreshItem.getActionView().clearAnimation();
refreshItem.setActionView(null);
}
}
public void RunTask()
{
new GetItems().execute();
}
private class GetItems extends AsyncTask<Void, Void, Void>
{
#Override
protected void onPreExecute()
{
StartAnimation();
}
#Override
protected Void doInBackground(Void... unused)
{
...
}
protected void onPostExecute(final Void unused)
{
StopAnimation();
}
}
#Override
public void onCreateOptionsMenu(final Menu menu, final MenuInflater inflater) {
inflater.inflate(R.menu.keyword_menu, menu);
refreshItem = menu.findItem(R.id.get);
super.onCreateOptionsMenu(menu, inflater);
}
#Override
public boolean onOptionsItemSelected(final MenuItem item)
{
if (item.getItemId() == R.id.get) {
gi = new GetItems(getActivity(), null);
gi.execute();
return true;
} else {
return super.onOptionsItemSelected(item);
}
}
}
I think following link provide you with your answer on how to avoid the nullpointer exception.
Fragment's instances may be recreated by the system at any time, that's why it's not easy to "hold a reference to them". You have to use the fragmentmanager.
adapter instance gone in fragment with listview