我开始使用minSdkVersion =“14”和targetSdkVersion =“17”的新应用程序.它包含一个6页的viewpager.有3个网页浏览和3个其他的观点.
当我将应用程序推回到背景,点击后退或主页按钮,它在“缓存的后台进程”中使用大约40MB,我不明白为什么.
这是我的一个Webview的一个例子:
import android.os.Bundle; import android.support.v4.app.Fragment; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.webkit.WebView; import android.webkit.WebViewClient; import android.widget.RelativeLayout; public class Presentation extends Fragment { boolean isOption = false; RelativeLayout main = null; WebView web_main = null; public Presentation () { } @Override public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) { super.onCreate(savedInstanceState); main = (RelativeLayout) inflater.inflate(R.layout.webview,container,false); return main; } @Override public void onActivityCreated(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onActivityCreated(savedInstanceState); web_main = new WebView(getActivity().getApplicationContext()); web_main.setWebViewClient(new WebViewClient()); web_main.getSettings().setAppCacheEnabled(false); web_main.loadUrl("file:///android_asset/main.html"); main.removeAllViews(); main.addView(web_main); } @Override public void onDestroy() { super.onDestroy(); Log.i(getClass().getName(),"[OnDestroy]"); main.removeAllViews(); web_main.destroy(); main = null; web_main = null; System.gc(); } }
我遵循几个教程和答案,但对缓存后台进程没有任何影响
这是我的主要活动:
public class AppTest extends FragmentActivity { /** * The {@link android.support.v4.view.PagerAdapter} that will provide * fragments for each of the sections. We use a * {@link android.support.v4.app.FragmentPagerAdapter} derivative,which * will keep every loaded fragment in memory. If this becomes too memory * intensive,it may be best to switch to a * {@link android.support.v4.app.FragmentStatePagerAdapter}. */ SectionsPagerAdapter mSectionsPagerAdapter; /** * The {@link ViewPager} that will host the section contents. */ ViewPager mViewPager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main_content); // Create the adapter that will return a fragment for each of the three // primary sections of the app. mSectionsPagerAdapter = new SectionsPagerAdapter( getSupportFragmentManager()); // Set up the ViewPager with the sections adapter. mViewPager = (ViewPager) findViewById(R.id.pager); mViewPager.setAdapter(mSectionsPagerAdapter); } @Override protected void onStop() { super.onStop(); System.gc(); Log.i(getClass().getName(),"[OnStop]"); android.os.Debug.stopMethodTracing(); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main_content,menu); return true; } @Override protected void onDestroy() { super.onDestroy(); mViewPager.removeAllViews(); Log.i(getClass().getName(),"[OnDestroy]"); } /** * A {@link FragmentPagerAdapter} that returns a fragment corresponding to * one of the sections/tabs/pages. */ public class SectionsPagerAdapter extends FragmentStatePagerAdapter { public SectionsPagerAdapter(FragmentManager fm) { super(fm); } @Override public Fragment getItem(int position) { // getItem is called to instantiate the fragment for the given page. // Return a DummySectionFragment (defined as a static inner class // below) with the page number as its lone argument. Fragment fragment = null; switch (position) { case 0: fragment = new Presentation(); break; /* case 1: fragment = new Edition(); break; case 2: fragment = new Programme(); break; case 3: fragment = new Twitter(); break; case 4: fragment = new Partenaire(); break; case 5: fragment = new Information(); break;*/ default: fragment = new Presentation(); break; } return fragment; } @Override public int getCount() { // Show 6 total pages. return 6; } @Override public CharSequence getPageTitle(int position) { switch (position) { case 0: return "Presentation"; case 1: return "Edition"; case 2: return "Program"; case 3: return "Tweets"; case 4: return "Partners"; case 5: return "Information"; } return null; } } }
谁能看到有什么错误?
编辑
我试图把webview放在布局中,但是它仍然是一样的
其实我想知道当应用程序处于后台状态时缓存中放置什么?
解决方法
“缓存后台进程”通常是指没有前台活动且没有正在运行的服务的进程.这些进程保存在内存中,因为我们有足够的内存,我们可以让用户快速切换回来.如果Android开始运行RAM,这些进程将是第一个要被销毁以释放RAM的进程.有时,当相同的应用程序切换到新进程时,可能会保留一个旧的应用程序进程.
据我所知,在“缓存的后台进程”状态中占用的空间将由您的应用程序正在使用的任何位置决定.例如,如果应用程序在前台使用20MB,则如果RAM可用,则占用相同的空间.
如果您的应用程序有3个ImageViews和3个WebViews,它可以很好地占用40MB的RAM空间,这取决于那些ImageViews和WebViews中存储的内容.您可以使用Profiling tools来查看应用程序使用的内存量以及它的组件成分.如果在前台使用的内存与背景状态下的内存类似,那么一切都是应该的.
注意:制造商可能会混淆设置应用程序,并重新定义“缓存后台进程”的含义.在这种情况下,您必须与他们联系,以了解它们如何定义它以及它由何种组成.