为什么在旋转手机时会调用onActivityCreated两次?

时间:2015-02-05 12:58:07

标签: android android-layout android-activity android-fragments

我有一个片段类,它通过Activity类调用一个函数,它在片段中为我的自定义ListView返回一个适配器,但看起来在eventList完成之前返回了适配器。所以我的自定义列表视图不会显示在片段中。

Tha是我的Fragment clas:

package newcomer.fragment;

import java.util.ArrayList;

import com.fb.newcomersapp.CustomListAdapter;
import com.fb.newcomersapp.Event;
import com.fb.newcomersapp.R;

import android.app.Activity;
import android.app.Fragment;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;

public class FeedFragment extends Fragment{
    private static ListView listView; 
    private static TextView textView;
    EventListListener mCallback;
    private ArrayList<Event> eventList;
    private CustomListAdapter adapter;  

public interface EventListListener{

    public String getLocal();
    public CustomListAdapter parseQueryEvents();
    public CustomListAdapter adaptEventList();
    public void openEvent(int position);
    public ArrayList<Event> getEventList();


}



/* (non-Javadoc)
 * @see android.app.Fragment#onActivityCreated(android.os.Bundle)
 */
@Override
public void onActivityCreated(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onActivityCreated(savedInstanceState);
    listView = (ListView) getView().findViewById(R.id.event_list_view);
    adapter = mCallback.parseQueryEvents();
    Log.d("FeedFragment", "Adapter já foi chamado");
    adapter.notifyDataSetChanged();
    listView.setAdapter(adapter);


    listView.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            // TODO Auto-generated method stub
            mCallback.openEvent(position);

        }
    });
}

/* (non-Javadoc)
 * @see android.app.Fragment#onCreateView(android.view.LayoutInflater, android.view.ViewGroup, android.os.Bundle)
 */
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    Log.d("FeedFragment", "onCreateView");
    View view = inflater.inflate(R.layout.fragment_feed, container, false);
    textView = (TextView) view.findViewById(R.id.tv_header);
    textView.setText(mCallback.getLocal()); 
    return view;
}

/* (non-Javadoc)
 * @see android.app.Fragment#onAttach(android.app.Activity)
 */
@Override
public void onAttach(Activity activity) {
    // TODO Auto-generated method stub
    super.onAttach(activity);

    // This makes sure that the container activity has implemented
    // the callback interface. If not, it throws an exception
    try {
        mCallback = (EventListListener) activity;
    } catch (ClassCastException e) {
        throw new ClassCastException(activity.toString()
                + " must implement EventListListener");
        }
    }
}

函数调用:

public synchronized CustomListAdapter parseQueryEvents () {
    eventList = new ArrayList<Event>();
    ParseQuery<ParseObject> query = ParseQuery.getQuery("Event");
    query.orderByAscending("startDate") ;
    query.whereGreaterThanOrEqualTo("endDate", new Date()) ;
    query.findInBackground(new FindCallback<ParseObject>() {
        public void done(List<ParseObject> parseEventList, ParseException e) {
            if (e == null) {
                ParseObject temp ;
                Iterator<ParseObject> i = parseEventList.iterator() ;
                while(i.hasNext()) {
                    temp = i.next();
                    eventList.add(new Event(temp)) ;
                }

                Log.d("FeedActivity", "Eventos: "+eventList.size());
            } else {
                Log.d("FeedActivity", e.getMessage());
            }
        }
    });
    return adaptEventList();
}

public synchronized CustomListAdapter adaptEventList() {
    CustomListAdapter adapter = new CustomListAdapter(this, eventList);
    Log.d("FeedActivity", "Adapter Criado");
    return adapter;

}

日志:

02-05 09:43:17.065: D/FeedFragment(27880): onCreateView
02-05 09:43:17.113: D/Network(27880): Network
02-05 09:43:17.122: D/Network(27880): Network
02-05 09:43:17.772: D/FeedActivity(27880): Adapter Criado
02-05 09:43:17.772: D/FeedFragment(27880): Adapter já foi chamado
02-05 09:43:17.790: I/Choreographer(27880): Skipped 46 frames!  The application may be doing too much work on its main thread.
02-05 09:43:17.832: I/Adreno-EGL(27880): <qeglDrvAPI_eglInitialize:410>: EGL 1.4 QUALCOMM build: AU_LINUX_ANDROID_LNX.LA.3.5.1_RB1.04.04.02.048.018_msm8610_LNX.LA.3.5.1_RB1_ release_AU ()
02-05 09:43:17.832: I/Adreno-EGL(27880): OpenGL ES Shader Compiler Version: E031.24.00.08
02-05 09:43:17.832: I/Adreno-EGL(27880): Build Date: 03/07/14 Fri
02-05 09:43:17.832: I/Adreno-EGL(27880): Local Branch: 
02-05 09:43:17.832: I/Adreno-EGL(27880): Remote Branch: quic/LNX.LA.3.5.1_RB1.1
02-05 09:43:17.832: I/Adreno-EGL(27880): Local Patches: NONE
02-05 09:43:17.832: I/Adreno-EGL(27880): Reconstruct Branch: AU_LINUX_ANDROID_LNX.LA.3.5.1_RB1.04.04.02.048.018 + f2fd134 +  NOTHING
02-05 09:43:17.858: D/OpenGLRenderer(27880): Enabling debug mode 0
02-05 09:43:18.377: D/FeedActivity(27880): Eventos: 3
02-05 09:43:37.652: D/FeedFragment(27880): onCreateView
02-05 09:43:37.708: D/Network(27880): Network
02-05 09:43:37.721: D/Network(27880): Network
02-05 09:43:38.398: D/FeedActivity(27880): Adapter Criado
02-05 09:43:38.398: D/FeedFragment(27880): Adapter já foi chamado
02-05 09:43:38.399: D/FeedFragment(27880): onCreateView
02-05 09:43:38.428: D/Network(27880): Network
02-05 09:43:38.431: D/Network(27880): Network
02-05 09:43:39.133: D/FeedActivity(27880): Adapter Criado
02-05 09:43:39.133: D/FeedFragment(27880): Adapter já foi chamado
02-05 09:43:39.146: I/Choreographer(27880): Skipped 95 frames!  The application may be doing too much work on its main thread.
02-05 09:43:39.190: W/View(27880): requestLayout() improperly called by android.widget.ListView{41d65188 IFED.VC. ......ID -360,0-0,442 #7f050041app:id/list_slidermenu} during layout: running second layout pass
02-05 09:43:39.193: D/FeedActivity(27880): Eventos: 3
02-05 09:43:39.559: D/FeedActivity(27880): Eventos: 6

0 个答案:

没有答案