每次通过startActivity访问视图时,Android都会运行onChildAdded方法

时间:2017-06-05 15:08:31

标签: android

我想知道为什么每次通过启动活动访问视图时,我的android应用程序都在运行fireBase onChildAdded。我只想从fireBase数据库读取一次,从那时起我使用onResume用数据库中的最后一个条目填充列表(如果有的话)。

但是每次使用startActitivity访问视图时它都在运行onChildAdded。所以这意味着当我按下后退按钮到这个视图时就不会发生这种情况。

每次用户从主屏幕返回此页面时,不得不从数据库中重新填充列表,从而减慢了我的应用程序。

这是一段视频,展示了当我从主屏幕访问并按下“夜总会”卡时,按回访问屏幕的速度有多快:https://www.youtube.com/watch?v=VJxsHT14PU8&feature=youtu.be

我的onCreate方法:

@Override
protected void onCreate(Bundle savedInstanceState) {
    // start tracing to "/sdcard/calc.trace"
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_nightclub_search);

    // Map and camera Positions
    // Retrieve location and camera position from saved instance state.
    if (savedInstanceState != null) {
        mLastKnownLocation = savedInstanceState.getParcelable(KEY_LOCATION);
        mCameraPosition = savedInstanceState.getParcelable(KEY_CAMERA_POSITION);
    }

    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);

    // Build the Play services client for use by the Fused Location Provider and the Places API.
    // Use the addApi() method to request the Google Places API and the Fused Location Provider.
    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .enableAutoManage(this /* FragmentActivity */,
                    this /* OnConnectionFailedListener */)
            .addConnectionCallbacks(this)
            .addApi(LocationServices.API)
            .build();
    mGoogleApiClient.connect();

    // List View
    listView = (ListView) findViewById(R.id.venueList);
    listView.setOnItemClickListener(this);
    venueList = new ArrayList<>();
    arrayAdapter = new ArrayAdapter<>(nightclub_search.this, R.layout.list_layout, venueList);

    // Fill the list view from the fireBase
    fillListView();

    // Button
    Button addClub = (Button) findViewById(R.id.addClub);
    addClub.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i = new Intent(nightclub_search.this, venue_add.class);
            startActivity(i);
        }
    });
}

这就是我从主屏幕调用视图的方式:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home_screen);

    CardView cardBar = (CardView) findViewById(R.id.cardBar);
    CardView cardNightclub = (CardView) findViewById(R.id.cardNightClub);

    cardBar.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(home_screen.this, bar_search.class);
            startActivity(intent);
        }
    });
    cardNightclub.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(home_screen.this, nightclub_search.class);
            startActivity(intent);
        }
    });
}}

最后,这就是我在onCreate(以及其他地方)内调用firebase填充列表的方式:

// Fills the list for the initial load of the application
protected void fillListView(){

    // Loading bar
    dialog = new ProgressDialog(nightclub_search.this);
    dialog.setMessage("Loading Venues...");
    dialog.show();


    // Separate this from the main thread so that it runs faster
    Runnable run = new Runnable() {
        @Override
        public void run() {
            myRef.addChildEventListener(new ChildEventListener() {
                @Override
                public void onChildAdded(DataSnapshot dataSnapshot, String s) {
                    // Loading dialog for getting fireBase entries
                    count++;
                    Log.d("Firebase", "onChildAddedFROMFILLLISTVIEW:" + dataSnapshot.getKey());
                    // A new venue has been added, add it to the displayed list
                    Nightclub nightclub = dataSnapshot.getValue(Nightclub.class);
                    clubList.add(nightclub);
                    venueList.add(nightclub.name);
                    // Read in location and add marker on map
                    addMarkers(nightclub, days);
                    // Add to listView
                    listView.setAdapter(arrayAdapter);
                    // Remove the loading bar when we've reached this point
                    if (count >= (dataSnapshot.getChildrenCount() - 1)){
                        count = 0;
                        dialog.dismiss();
                    }
                }

                @Override
                public void onChildChanged(DataSnapshot dataSnapshot, String s) {

                }

                @Override
                public void onChildRemoved(DataSnapshot dataSnapshot) {

                }

                @Override
                public void onChildMoved(DataSnapshot dataSnapshot, String s) {

                }

                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            });
        }
    };

    Thread fillListThread = new Thread(run);
    fillListThread.start();

}

2 个答案:

答案 0 :(得分:0)

尝试设置启动模式&#39; singleTask&#39;在manifest的活动中,以便onCreate在初始化活动时只被调用一次,并且在所有调用之后将被调用到onNewIntent&#39;方法,直到在活动后台堆栈中没有创建新实例。

答案 1 :(得分:0)

除非您为其设置一些标志,否则startActivity(Intent intent)方法会创建一个新的活动实例。要通过intent恢复现有活动实例,请尝试使用:

Intent i = new Intent(nightclub_search.this, venue_add.class);
i.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(i);

或将其添加到清单上的Activity标记:

<activity
    android:name="com.company.ActivityName"
    android:launchMode="singleTask">
</activity>