android launchmodel的使用

我们知道android中activity的启动模式有四种,分别为standard,singleTop,singleTask,singleTop。关于这四种启动模式的详细介绍请看<a target=_blank href="http://blog.csdn.net/congqingbin/article/details/7441789">http://blog.csdn.net/congqingbin/article/details/7441789</a>该博文介绍的非常详细,我就不在这里多说了。下面我们列举singleTop和singleTask的一个应用。就是我们应用程序的退出。

一、singleTop应用

我们在启动应用程序的时候,可以先用一个空的Activity来作为入口,并且在manifest文件中加入android:launchMode="singleTop" 
<activity
            android:name=".FlashActivity"
            android:label="@string/app_name"
            android:launchMode="singleTop" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>


源代码如下:

HomeActivity:

public class FlashActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		Log.d("mm", "FlashActivity onCreate"+getTaskId());
		Intent intent = new Intent(this,Main.class);
		startActivity(intent);
		
		super.onCreate(savedInstanceState);
	}
	
	
	@Override
	protected void onNewIntent(Intent intent) {
		
		Log.d("mm", "FlashActivity onNewIntent");
		//当flag为<span style="font-family: 宋体, SimSun;">FLAG_ACTIVITY_CLEAR_TOP的时候,本Activity也要finish掉</span>
<span style="white-space:pre">		</span>if(intent.getFlags() == Intent.FLAG_ACTIVITY_CLEAR_TOP){
			finish();
		}
		
		super.onNewIntent(intent);
		
	}



	@Override
	protected void onDestroy() {
		
		Log.d("mm", "FlashActivity ondes");
		super.onDestroy();
		
	}
	
	
	
}
MainActivity代码

public class Main extends Activity {
	//微信申请时的ID
	boolean isRun = true;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.view_stub);
        final ArrayList<MyObject> list = new ArrayList<MyObject>();
        final ArrayList<MyObject.InnerClassObj> list2 = new ArrayList<MyObject.InnerClassObj>();
        if ((((int) (Math.random() * 100)) & 0x01) == 0) {  
            // to show text  
            // all you have to do is inflate the ViewStub for textview  
            ViewStub stub = (ViewStub) findViewById(R.id.viewstub_demo_text);  
            stub.inflate();  
            TextView text = (TextView) findViewById(R.id.viewstub_demo_textview);  
            text.setText("The tree of liberty must be refreshed from time to time" +  
                    " with the blood of patroits and tyrants! Freedom is nothing but " +  
                    "a chance to be better!");  
                    text.setOnClickListener(new OnClickListener() {
						
						@Override
						public void onClick(View v) {	
					
					           startActivity();
						}
					});
        } else {    
            // to show image  
            // all you have to do is inflate the ViewStub for imageview  
            ViewStub stub = (ViewStub) findViewById(R.id.viewstub_demo_image);  
            stub.inflate();  
            final ImageView image = (ImageView) findViewById(R.id.viewstub_demo_imageview);  
            image.setImageResource(R.drawable.icon);  
            image.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
	
					startActivity();
				}
			});
        } 
         
       
    
    public void startActivity(){
    	Intent intent = new Intent();
    	intent.setClass(this, MyActivity.class);
    	startActivity(intent);
    }
    
    @Override
	protected void onDestroy() {
		
		Log.d("mm", "MainActivity ondes");
		super.onDestroy();
		
	}
}
MyActivity的代码

public class MyActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		ImageView image = new ImageView(this);
		image.setBackgroundResource(android.R.color.darker_gray);
		setContentView(image);
		
		Intent intent = new Intent(this, MMService.class);
		startService(intent);
		Log.d("mm", "MyActivity onCreate"+getTaskId());
	}

	@Override
	public void onBackPressed() {
		Log.d("mm", "MyActivity onBackPressed");
		//监听返回键,这时候我们要退出程序的话,要返回到FlashActivity
		Intent intent = new Intent(this,FlashActivity.class);
		//如果想通过把Task栈中的Activity全部给finish掉的话,就必须设置这个flag,
		//不然的话FlashActivity就会又创建一个新的FlashActivity
		intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		startActivity(intent);
		super.onBackPressed();
		
	}
	
	@Override
	protected void onDestroy() {
		
		Log.d("mm", "MyActivity ondes");
		super.onDestroy();
		
	}
	
}

这样就可以把Task中所有的Activity都能够finish掉了

二、singleTask

singleTask就是设置为singleTask的Activity,在栈中只有一个实例,无论该Activity是在栈顶还是栈底,只要有被召唤就从栈中提到栈顶,当然它以上的Activity都会被finish掉,所以我们可以把FlashActivity的launchmode改为singleTask的话也是可以的。



但是MyActivity中flag就不用在设置了

public class MyActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		ImageView image = new ImageView(this);
		image.setBackgroundResource(android.R.color.darker_gray);
		setContentView(image);
		
		Intent intent = new Intent(this, MMService.class);
		startService(intent);
		Log.d("mm", "MyActivity onCreate"+getTaskId());
	}

	@Override
	public void onBackPressed() {
		Log.d("mm", "MyActivity onBackPressed");
		//监听返回键,这时候我们要退出程序的话,要返回到FlashActivity
		Intent intent = new Intent(this,FlashActivity.class);
		//如果想通过把Task栈中的Activity全部给finish掉的话,就必须设置这个flag,
		//不然的话FlashActivity就会又创建一个新的FlashActivity
		//intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		startActivity(intent);
		super.onBackPressed();
		
	}
	
	@Override
	protected void onDestroy() {
		
		Log.d("mm", "MyActivity ondes");
		super.onDestroy();
		
	}
	
}

关于Activity的启动模式的应用还有很多,根据自己的业务的需要,来设置。

你可能感兴趣的:(android launchmodel的使用)