2017-04-25 72 views
0

我想使用一個獨特的活動,其中包含導航視圖,他們的選項將打開另一個片段。一切都可以,除非MainFragment從另一個片段(從導航視圖打開)返回時被重新創建。我想知道如何避免這種情況,因爲它再次加載列表。Android - 如何避免按下按鈕時重新創建一個片段

MainActivity:

import android.content.Intent; 
import android.os.Bundle; 
import android.support.annotation.NonNull; 
import android.support.design.widget.NavigationView; 
import android.support.v4.view.GravityCompat; 
import android.support.v4.widget.DrawerLayout; 
import android.support.v7.app.ActionBarDrawerToggle; 
import android.support.v7.app.AppCompatActivity; 
import android.support.v7.widget.Toolbar; 
import android.view.Menu; 
import android.view.MenuItem; 

public class MainActivity extends AppCompatActivity 
    implements NavigationView.OnNavigationItemSelectedListener { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     toolbar.setTitle(R.string.home); 
     setSupportActionBar(toolbar); 

     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
      this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); 
     drawer.addDrawerListener(toggle); 
     toggle.syncState(); 

     NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); 
     navigationView.setNavigationItemSelectedListener(this); 

     getSupportFragmentManager().beginTransaction() 
      .add(R.id.layout, new MainFragment()) 
      .commit(); 
    } 

    @Override 
    public void onBackPressed() { 
     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     if (drawer.isDrawerOpen(GravityCompat.START)) { 
      drawer.closeDrawer(GravityCompat.START); 
     } else { 
      if (getSupportFragmentManager().getBackStackEntryCount() > 0) { 
       NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); 
       navigationView.getMenu().findItem(R.id.nav_home).setChecked(true); 
       getSupportFragmentManager().popBackStack(); 
      } else { 
       super.onBackPressed(); 
      } 
     } 
    } 

    @SuppressWarnings("StatementWithEmptyBody") 
    @Override 
    public boolean onNavigationItemSelected(@NonNull MenuItem item) { 

     if (getSupportFragmentManager().getBackStackEntryCount() > 0) { 
      getSupportFragmentManager().popBackStack(); 
     } 

     switch (item.getItemId()) { 
      case R.id.nav_history: 
       getSupportFragmentManager().beginTransaction() 
        .replace(R.id.layout, new Fragment2()) 
        .addToBackStack(null) 
        .commit(); 
       break; 
      case R.id.nav_settings: 
       break; 
      case R.id.nav_logout: 
       break; 
     } 

     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     drawer.closeDrawer(GravityCompat.START); 
     return true; 
    } 
} 

主要片段:

import android.os.AsyncTask; 
import android.os.Bundle; 
import android.support.annotation.Nullable; 
import android.support.v4.app.Fragment; 
import android.support.v7.widget.DividerItemDecoration; 
import android.support.v7.widget.LinearLayoutManager; 
import android.support.v7.widget.RecyclerView; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ProgressBar; 
import android.widget.TextView; 

import java.util.ArrayList; 
import java.util.Date; 
import java.util.List; 

import br.com.example.adapters.ScheduledServicesAdapter; 
import br.com.example.entities.ScheduledService; 

public class MainFragment extends Fragment { 


    private final class ScheduleServiceTask extends AsyncTask<Void, Void, List<ScheduledService>> { 

     @Override 
     protected List<ScheduledService> doInBackground(Void... params) { 
      mList = new ArrayList<>(); 
      mList.add(new ScheduledService(new Date(), 250.50, 0)); 
      mList.add(new ScheduledService(new Date(), 50.75, 0)); 

      return mList; 
     } 

     @Override 
     protected void onPostExecute(List<ScheduledService> scheduledServices) { 
      super.onPostExecute(scheduledServices); 
      setData(scheduledServices); 
     } 
    } 

    private RecyclerView mRecyclerView; 
    private ScheduledServicesAdapter mAdapter; 
    private List<ScheduledService> mList; 


    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) { 
     super.onCreateView(inflater, parent, savedInstanceState); 
     return inflater.inflate(R.layout.content_main, parent, false); 
    } 

    @Override 
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { 
     super.onViewCreated(view, savedInstanceState); 


     LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity()); 
     DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(getActivity(), 
      linearLayoutManager.getOrientation()); 

     mRecyclerView = (RecyclerView) view.findViewById(R.id.recycler_view); 
     mRecyclerView.setHasFixedSize(true); 
     mRecyclerView.setLayoutManager(linearLayoutManager); 
     mRecyclerView.addItemDecoration(dividerItemDecoration); 

     new ScheduleServiceTask().execute(); 
    } 

    private void setData(List<ScheduledService> list) { 
     ProgressBar progressBar = (ProgressBar) getActivity().findViewById(R.id.progress_bar); 
     progressBar.setVisibility(View.GONE); 

     if (!list.isEmpty()) { 
      mAdapter = new ScheduledServicesAdapter(list); 
      mRecyclerView.setAdapter(mAdapter); 
     } else { 
      TextView emptyText = (TextView) getActivity().findViewById(R.id.empty_text); 
      emptyText.setVisibility(View.VISIBLE); 
     } 
    } 
} 

片段2具有用於測試目的的世界你好文本圖。

回答

0
  1. 當你把你MainFragmentBackStack(指鑑於這fragment的 不visible因爲Fragment2採取 屏),所有這些都屬於它的意見將被銷燬。
  2. 然後,當您從另一個片段返回到MainFragment時,將會重新創建MainFragment的視圖 。這意味着onCreateViewonViewCreated將再次調用 。
  3. 在你的情況,我建議:

    • 移動new ScheduleServiceTask().execute();onCreate或 另一種方法(例如:requestGetData()
    • 初始化mAdapteronViewCreated()
    • 設置adaptermRecyclerViewsetData()方法,您只需要爲mAdapter重新設置數據並調用notifyDatasetChanged()函數。
+0

感謝您的解釋。即使片段被重新創建,mList變量也不爲空,整個數據都在那裏。在調用新的ScheduledTask()。execute之前,我已經嘗試了mList!= null。這樣我就避免再次查詢。 –

0

@Douglas Fomaro,更好的是你可以將你的數據(mList)移動到片段之外。維護保存所有列表數據值的DataManager類。

相關問題