简体   繁体   English

Android按钮的onClickLIstener()仅在Activity开始时起作用一次,而不是从Fragment事务返回时起作用

[英]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. 但是,如果我按了其中一个按钮,然后按了返回按钮,则home片段及其按钮会重新出现,但所有按钮回调均不起作用。 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: 回调在home片段的onCreateView方法中分配,如下所示:

 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: 这是我使用按钮启动的Fragments的超类:

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. 该Activity可能会在某个时候被重新创建(取决于您的流程),但是您应该在onAttach (Activity activity)重新分配引用,或者只是调用getActivity()来获取您当前附加的Activity。

So mActivity.getSupportFragmentManager()... 所以mActivity.getSupportFragmentManager()...

should instead be 相反应该是

getActivity().getSupportFragmentManager()...

EDIT: Wait, maybe that's not right. 编辑:等等,也许那是不对的。 What is v in onCreateView() ? onCreateView() v是什么? You're not creating any UI in this Fragment ? 您没有在此Fragment创建任何UI吗? You should be using onViewCreated() to get a reference to the views in the created hierarchy. 您应该使用onViewCreated()获取对所创建层次结构中视图的引用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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