简体   繁体   中英

Android buttons' onClickLIstener() only works once when the Activity begins, not when returning from Fragment transaction

Any time the main Activity is started or restarted (ie by pressing the home button on my action bar), all of the buttons (which reside in a fragment in this main activity) work correctly. However, if I press one of the buttons, then press the back button, the home fragment and its buttons reappears but none of the button callbacks work. The button images continue to change appearance when pressed, as always.

The callbacks are assigned in the onCreateView method of the home fragment, as follows:

 public class HomeFragment extends SherlockFragment {
   public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
     Button button = (Button) v.findViewById(R.id.button);
     button.setOnClickListener(new OnClickListener() {
       @Override
       public void onClick(View arg0) {
         Log.d(TAG, "Button!");
         mActivity.getSupportFragmentManager().beginTransaction()
                  .replace(R.id.contFragMain, new SomeFragment())
                  .addToBackStack(null)
                  .commit();
        }    
     });
 }

Here is the superclass of Fragments I launch with the buttons:

public abstract class RouteListFragment extends SherlockFragment {
  private static final String TAG = RouteListFragment.class.getName();
  private Activity mActivity;                 // parent activity
  RouteListAdapter mAdapter;
  ListView mListViewRouteList;

  public RouteListFragment() {
    super();
  }

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mActivity = getActivity();
  }

   @Override
   public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
     Bundle bndl = getArguments();  // data passed should contain search term
     View v = inflater.inflate(R.layout.fragment_route_list, container, false);
     mListViewRouteList = (ListView) v.findViewById(R.id.listViewRouteList);
     addRoutesToList(); //Populates the list with data via an async DB call
     return v;
}

You shouldn't be stashing the Activity reference; the Activity is probably getting recreated at some point (depends on how your flow is), but you should either reassign the reference in onAttach (Activity activity) , or just call getActivity() to get the currently Activity you're attached to.

So mActivity.getSupportFragmentManager()...

should instead be

getActivity().getSupportFragmentManager()...

EDIT: Wait, maybe that's not right. What is v in onCreateView() ? You're not creating any UI in this Fragment ? You should be using onViewCreated() to get a reference to the views in the created hierarchy.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM