最近我的工作我的应用程序,使其加载速度更快,工作得更好,我使用的导航抽屉里我的MainActivity:防止抽屉式导航片段
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_camara) {
LoadJson asyncTask = (LoadJson) new LoadJson(new LoadJson.AsyncResponse() {
@Override
public void processFinish(JSONArray output) {
//Here you will receive the result fired from async class
//of onPostExecute(result) method.
//Set the fragment initially
MainFragment fragment = new MainFragment(output);
FragmentTransaction fragmentTransaction =
getSupportFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.fragment_container, fragment);
fragmentTransaction.commit();
// Handle the camera action
}
}).execute();
} else if (id == R.id.nav_gallery) {
//Set the fragment initially
GalleryFragment fragment = new GalleryFragment();
FragmentTransaction fragmentTransaction =
getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, fragment);
fragmentTransaction.commit();
} else if (id == R.id.nav_search) {
//Set the fragment initially
FetchResualt fragment = new FetchResualt();
FragmentTransaction fragmentTransaction =
getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, fragment);
fragmentTransaction.commit();
// Handle the camera action
} else if (id == R.id.nav_manage) {//
Bundle bundle = new Bundle();//
bundle.putInt("someStr",ID_OF_BEACH);//
//Set the fragment initially//
FragmentBeach fragment = new FragmentBeach();//
fragment.setArguments(bundle);//
FragmentTransaction fragmentTransaction =//
getSupportFragmentManager().beginTransaction();//
fragmentTransaction.replace(R.id.fragment_container, fragment);//
fragmentTransaction.commit();//
// Handle the camera action
} else if (id == R.id.nav_share) {
} else if (id == R.id.nav_send) {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
正如你可以看到,如果我们推第一个菜单if (id == R.id.nav_camara)
它会通过一个JSONArray
到片段类,它需要4-5秒加载。因此,如果用户每次进入nav_camara
时都在菜单之间导航,则会使应用程序停留4秒,并且每次我们选择菜单项时都会重新创建片段的副本,因此即使我制作:setRetainInstance(true);
在我的片段类中它不会工作。
您建议什么解决方案,以防止应用程序每次我们选择一个菜单项时重新创建一个新的片段?
也许加载是问题?而不是片段本身?尝试实现一些预取并在早些时候执行LoadJson任务。 – Christopher
嘿刚刚访问[此链接](http://stackoverflow.com/questions/14839152/fragment-as-a-singleton-in-android)。希望在这里你可以找到有用的东西。 –
我不认为它有什么区别,因为有一些计算,使culester标记和迭代json文件,并将其放在地图上,这需要很长时间,每次应用程序做一个片段的新副本这些计算将发生它认为该解决方案可能无法获得片段 – Blacksword