Starting new Activity vs. setContentPane(newView)

Put your problem here if it does not fit any of the other categories.

Starting new Activity vs. setContentPane(newView)

Postby Danuubz » Thu Oct 23, 2008 5:20 pm

The Google tutorials say:

"An activity is usually a single screen in your application. Each activity is implemented as a single class that extends the Activity base class. Your class will display a user interface composed of Views and respond to events. Most applications consist of multiple screens. For example, a text messaging application might have one screen that shows a list of contacts to send messages to, a second screen to write the message to the chosen contact, and other screens to review old messages or change settings. Each of these screens would be implemented as an activity."

My question: Is there a performance advantage by starting new Activity if you want to set a new View on the screen compared to setContentView(...)?
Last edited by Danuubz on Mon Oct 27, 2008 3:08 pm, edited 1 time in total.
User avatar
Danuubz
Experienced Developer
Experienced Developer
 
Posts: 78
Joined: Wed Dec 19, 2007 10:44 pm
Location: Germany

Top

Postby Danuubz » Fri Oct 24, 2008 4:27 pm

In other words:

Why is it better to start a new Activity for each 'screen' instead of packing each 'screen' into one Activity?

(I only see disadvantages by starting these new activities, especially if I make the views programmatically I cannot see any necessity for that)
User avatar
Danuubz
Experienced Developer
Experienced Developer
 
Posts: 78
Joined: Wed Dec 19, 2007 10:44 pm
Location: Germany

Postby Danuubz » Mon Oct 27, 2008 3:20 pm

Here is a comparison application I wrote:

A.) You can switch screen by starting new Activity, if you do 'TAP SCREEN' or by pressing DPAD_TOP.

B.) With DPAD_CENTER you switch screen using setContentView(view).

Performance: setContentView(view) needs 2-5 milliseconds to switch screen. The 'new Activity' approach needs 40-90 milliseconds for this example.

Source:

values>strings.xml
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="utf-8" ?>
  2. - <resources>
  3.   <string name="hello">Hello World, Intents</string>
  4.   <string name="app_name">Intents</string>
  5.   <string name="activity_second">Second</string>
  6.   </resources>
  7.  
Parsed in 0.002 seconds, using GeSHi 1.0.8.4



AndroidManifest.xml
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="utf-8" ?>
  2. - <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="de.intents" android:versionCode="1" android:versionName="1.0.0">
  3. - <application android:icon="@drawable/icon" android:label="@string/app_name">
  4. - <activity android:name=".Intents" android:label="@string/app_name">
  5. - <intent-filter>
  6.   <action android:name="android.intent.action.MAIN" />
  7.   <category android:name="android.intent.category.LAUNCHER" />
  8.   </intent-filter>
  9.   </activity>
  10. - <activity android:name=".Second" android:label="@string/activity_second">
  11. - <intent-filter>
  12.   <action android:name="android.intent.action.VIEW" />
  13.   </intent-filter>
  14.   </activity>
  15.   </application>
  16.   </manifest>
Parsed in 0.003 seconds, using GeSHi 1.0.8.4



Intents.java:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package de.intents;
  2.  
  3. import android.app.Activity;
  4. import android.os.Bundle;
  5. import android.view.KeyEvent;
  6. import android.view.MotionEvent;
  7. import android.content.Intent;
  8. import android.graphics.Color;
  9. import android.view.*;
  10. import android.widget.*;
  11.  
  12. public class Intents extends Activity implements GestureDetector.OnGestureListener
  13. {
  14.     GestureDetector detect;
  15.     LinearLayout main;
  16.     TextView tv;
  17.    
  18.     long start, stop, period;
  19.    
  20.     boolean flag = true;
  21.        
  22.     @Override
  23.     public void onCreate(Bundle savedInstanceState)
  24.     {
  25.         super.onCreate(savedInstanceState);
  26.         detect = new GestureDetector(this);  
  27.        
  28.         main = new LinearLayout(this);
  29.                 main.setLayoutParams(new LinearLayout.LayoutParams(320,480));
  30.                 main.setOrientation(LinearLayout.VERTICAL);
  31.                 main.setBackgroundColor(Color.GRAY);
  32.                
  33.                 tv = new TextView(this);
  34.                 tv.setLayoutParams(new LinearLayout.LayoutParams(320,50));
  35.                 tv.setBackgroundColor(Color.WHITE);
  36.                 main.addView(tv);
  37.                
  38.                 tv.setText("DPAD_CENTER -> setContentPane(view)n"
  39.                                 + "DPAD_TOP or SCREEN-TAP -> new Activityn"
  40.                                 + "LONG-PRESS -> QUIT");
  41.        
  42.         setContentView(main);
  43.     }
  44.    
  45.     @Override
  46.     public boolean onTouchEvent(MotionEvent me)
  47.     {
  48.         return detect.onTouchEvent(me);
  49.     }
  50.    
  51.     @Override
  52.     public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY)
  53.     {  
  54.         return true;  
  55.     }
  56.    
  57.     @Override
  58.     public boolean onDown(MotionEvent e)    
  59.     {
  60.         return true;
  61.     }
  62.    
  63.     @Override
  64.     public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
  65.     {
  66.         return true;  
  67.     }
  68.    
  69.     @Override
  70.     public void onLongPress(MotionEvent e)
  71.     {  
  72.         finish();
  73.     }
  74.    
  75.     @Override
  76.     public void onShowPress(MotionEvent e) {   }    
  77.    
  78.     @Override
  79.     public boolean onSingleTapUp(MotionEvent e)    
  80.     {    
  81.         // SWITCH SCREEN BY ACTIVITY
  82.         //
  83.         start = System.currentTimeMillis();
  84.        
  85.         Intent i = new Intent(Intent.ACTION_VIEW);
  86.         i.setClass(Intents.this, Second.class);
  87.         i.putExtra("start", start);
  88.         this.startActivity(i);
  89.        
  90.         return true;
  91.     }
  92.    
  93.    
  94.     public boolean onKeyDown(int keyCode, KeyEvent event)
  95.     {    
  96.         if(keyCode == KeyEvent.KEYCODE_DPAD_UP)
  97.         {
  98.                 // SWITCH BY ACTIVITY
  99.                 //
  100.                 start = System.currentTimeMillis();
  101.                
  102.                 Intent i = new Intent(Intent.ACTION_VIEW);
  103.             i.setClass(Intents.this, Second.class);
  104.             i.putExtra("start", start);
  105.             this.startActivity(i);
  106.         }
  107.        
  108.         if(keyCode == KeyEvent.KEYCODE_DPAD_CENTER)
  109.         {
  110.                 if(flag == true)
  111.                 {
  112.                         // SWITCH BY setContentView(view)
  113.                 //
  114.                         start = System.currentTimeMillis();
  115.                        
  116.                         main = new LinearLayout(this);
  117.                         main.setLayoutParams(new LinearLayout.LayoutParams(320,480));
  118.                         main.setOrientation(LinearLayout.VERTICAL);
  119.                         main.setBackgroundColor(Color.RED);
  120.                        
  121.                         tv = new TextView(this);
  122.                         tv.setLayoutParams(new LinearLayout.LayoutParams(320,30));
  123.                         tv.setBackgroundColor(Color.WHITE);
  124.                         main.addView(tv);
  125.                        
  126.                         stop = System.currentTimeMillis();
  127.                         period = stop - start;
  128.                        
  129.                         tv.setText((new Long(period)).toString() + " ms");
  130.                        
  131.                         setContentView(main);
  132.                        
  133.                         flag = false;
  134.                 }
  135.                 else
  136.                 {
  137.                         // SWITCH BY setContentView(view)
  138.                 //
  139.                         start = System.currentTimeMillis();
  140.                        
  141.                         main = new LinearLayout(this);
  142.                         main.setLayoutParams(new LinearLayout.LayoutParams(320,480));
  143.                         main.setBackgroundColor(Color.YELLOW);
  144.                        
  145.                         tv = new TextView(this);
  146.                         tv.setLayoutParams(new LinearLayout.LayoutParams(320,30));
  147.                         tv.setBackgroundColor(Color.WHITE);
  148.                         main.addView(tv);
  149.                        
  150.                         stop = System.currentTimeMillis();
  151.                         period = stop - start;
  152.                        
  153.                         tv.setText((new Long(period)).toString() + " ms");
  154.                        
  155.                         setContentView(main);
  156.                        
  157.                         flag = true;
  158.                 }              
  159.         }
  160.        
  161.         return true;
  162.     }
  163.    
  164. }
  165.  
Parsed in 0.050 seconds, using GeSHi 1.0.8.4


Second.java:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package de.intents;
  2.  
  3. import android.app.Activity;
  4. import android.os.Bundle;
  5. import android.content.Intent;
  6. import android.graphics.Color;
  7. import android.view.GestureDetector;
  8. import android.view.MotionEvent;
  9. import android.widget.LinearLayout;
  10. import android.widget.TextView;
  11. import android.view.*;
  12.  
  13. public class Second extends Activity implements GestureDetector.OnGestureListener
  14. {
  15.     LinearLayout main;
  16.     TextView tv;
  17.     GestureDetector detect;
  18.     Intent intent;
  19.    
  20.     long start, stop, period;
  21.        
  22.     @Override
  23.     public void onCreate(Bundle savedInstanceState)
  24.     {
  25.        
  26.         super.onCreate(savedInstanceState);
  27.        
  28.         intent = this.getIntent();
  29.         start = intent.getLongExtra("start", -1L);
  30.        
  31.         detect = new GestureDetector(this);
  32.        
  33.         main = new LinearLayout(this);
  34.         main.setLayoutParams(new LinearLayout.LayoutParams(320,480));
  35.         main.setBackgroundColor(Color.WHITE);
  36.        
  37.         tv = new TextView(this);
  38.                 tv.setLayoutParams(new LinearLayout.LayoutParams(320,50));
  39.                 tv.setBackgroundColor(Color.WHITE);
  40.                 main.addView(tv);
  41.                
  42.                 stop = System.currentTimeMillis();
  43.                 period = stop - start;
  44.                
  45.                 tv.setText((new Long(period)).toString() + " msnn"
  46.                                 + "BACK -> return");
  47.        
  48.         setContentView(main);        
  49.     }
  50.    
  51.     @Override
  52.     public boolean onTouchEvent(MotionEvent me)
  53.     {
  54.         return detect.onTouchEvent(me);
  55.     }
  56.    
  57.     @Override
  58.     public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY)
  59.     {  
  60.         return true;  
  61.     }
  62.    
  63.     @Override
  64.     public boolean onDown(MotionEvent e)    
  65.     {
  66.         return true;
  67.     }
  68.    
  69.     @Override
  70.     public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
  71.     {
  72.         return true;  
  73.     }
  74.    
  75.     @Override
  76.     public void onLongPress(MotionEvent e)
  77.     {  
  78.         finish();
  79.     }
  80.    
  81.     @Override
  82.     public void onShowPress(MotionEvent e) {   }    
  83.    
  84.     @Override
  85.     public boolean onSingleTapUp(MotionEvent e)    
  86.     {          
  87.         return true;
  88.     }
  89. }
  90.  
Parsed in 0.040 seconds, using GeSHi 1.0.8.4
User avatar
Danuubz
Experienced Developer
Experienced Developer
 
Posts: 78
Joined: Wed Dec 19, 2007 10:44 pm
Location: Germany

Postby ErnestoGuevara » Wed Jan 14, 2009 4:32 pm

This is old, but never got enough love.

Thanks for this example Nuubz. I think the advantage to starting a new activity is that you will allow android to better manage your app lifetime (can kill off inactive stuff if it needs to free memory for the active view) and to integrate it into your back button.

If you don't care about either, switching the content view sounds fine to me.

Che
ErnestoGuevara
Experienced Developer
Experienced Developer
 
Posts: 75
Joined: Thu Jan 08, 2009 2:02 am

Postby Danuubz » Wed Jan 14, 2009 7:02 pm

This is a post from 'former times' :)

For some cases (like displaying simple information), switching content view is ok and useful. However there are lots of problems with Event handling/ listeners if you want to switch from one complex view to another.
Sticky Note Cork&Orc [Android Market] : http://www.youtube.com/watch?v=ewWaquAwZQY
User avatar
Danuubz
Experienced Developer
Experienced Developer
 
Posts: 78
Joined: Wed Dec 19, 2007 10:44 pm
Location: Germany

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 20 guests