android 关闭所有窗口

  • 内容
  • 评论
  • 相关

本小例子讲的是比较麻烦的一种,

package com.example.closeall;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;

public class MainActivity extends Activity {
	private static List<Activity> activities = new ArrayList<Activity>();
	private static int count = 0;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		if(count == 0 ){
			setTitle("主窗口");
			count++;
		}else{
			setTitle("窗口"+count);
			count++;
		}
		activities.add(this);
	}
	
	public void onClick_ShowNewActivity(View view){
		Intent intent = new Intent(this,MainActivity.class);
		startActivity(intent);
	}
	
	public void onClick_CloseAllActivity(View view){
		for(int i = 1; i < activities.size(); i++){
			if(activities.get(i) != null){
				activities.get(i).finish();
			}
		}
		
		count = 1;
	}

	@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, menu);
		return true;
	}

}

评论

0条评论

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注