A practice of android:process combining activity initiation mode

Source: Internet
Author: User

There is a scenario where an app crashes, and the app crashes because the app consumes more memory than the system allocates to its maximum heap size. The allocation of objects, which occurs above the heap (heap), is fixed for the maximum heap size that the system assigns to each application.

Assuming this happens, your app starts another activity, causing your app to crash, and your app is using more memory than the system allocates the largest heap size. Well, this time, you can take the approach is, optimization algorithms and the like, but, assuming that you optimize, however, the problem still occurs.

Then, you can do this by letting the activated activity run in a new, independent process, so that the activated activity instance does not occupy the heap size of your app. This can be achieved by android:process. This is a crash issue that resolves the application because the memory consumed exceeds the maximum heap size of the system.

In addition, setting up an activity's startup mode, setting its startup mode to Android:singleinstance, ensures that, even if the user invokes the activity multiple times, there is only one instance in the system, but not multiple times. This is also a way to save memory.

Here are the practices:

The requirements of practice

1. Create an app that launches helpactivity by the app.

The 2.HelpActivity setting runs in a separate process.

3.helpActivity is limited to running in a separate task 4. Start and helpactivity to ensure that it runs in the system with only one instance, even if the user calls it multiple times.       5. Use DDMS to verify that the actual helpactivity process is not the same as the process that initiated its application. -----------Concrete Practice Content Configuration aspects:1.android:taskaffinity can ensure that the activity runs in a separate task 2.android:launchmode = "SingleInstance", which ensures that, at any moment, The activity instance has only one, even if the user starts it multiple times.
<manifest xmlns:android= "http://schemas.android.com/apk/res/android" package= "Com.processtest" Android: Versioncode= "1" android:versionname= "1.0" > <uses-sdk android:minsdkversion= "8" ANDROID:TARGETSD        kversion= "/> <application android:allowbackup=" true "android:icon=" @drawable/ic_launcher " Android:label= "@string/app_name" android:theme= "@style/apptheme" > <activity android:name                = "Com.processtest.MainActivity" android:label= "@string/app_name" > <intent-filter> <action android:name= "Android.intent.action.MAIN"/> <category android:name= "android.intent . category. LAUNCHER "/> </intent-filter> </activity> <activity android:name=". H Elpactivity "android:launchmode=" SingleInstance "android:process=": A_separate_process "an Droid:taskaffinity= "com.Processtest.myhelpactivity "/> </application></manifest> 

Code aspects:

1. To set the boot mode identification Intent.flag_activity_new_task, the purpose is to implement single-instance ACTIVITY.

 Public classMainactivityextendsActivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main);    Inital (); }        Private voidinital () {Button start=(Button) Findviewbyid (R.ID.STARTHELP); Start.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View v) {//TODO auto-generated Method StubIntent Intent =NewIntent (mainactivity. This, Helpactivity.class);                Intent.setflags (Intent.flag_activity_new_task);            StartActivity (Intent);            }        }); } @Override Public BooleanOncreateoptionsmenu (Menu menu) {//inflate the menu; This adds items to the action bar if it is present.getmenuinflater (). Inflate (R.menu.main, menu); return true; }}

actual results:DDMS, display: Long Press the home key: The above information, can be explained: 1. The two are running in different processes.  2. The two have become two different applications---can be seen by the results shown by the home key. Reference: http://android-developers.blogspot.com/2011/03/memory-analysis-for-android.htmlhttp:// developer.android.com/tools/debugging/ddms.htmlhttp://stackoverflow.com/questions/7142921/ Usage-of-androidprocess
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.