Activity的跳转顺序为FirstActivity—>SecondActivity—>ThirdActivity。
1、在FirstActivity中双击返回键退出程序;
2、在SecondActivity中按返回键回到FirstAcitivity;
3、在ThirdActivity中按返回键回到SecondActivity。
1、在FirstActivity中双击返回键退出程序;
2、在SecondActivity中按返回键回到FirstAcitivity;
3、在ThirdActivity中按返回键回到FirstActivity。
问题一可以理解为浏览网页返回上一个页面,问题二则是回到首页。
1、退出程序;2、双击返回键退出;3、Activity生命周期。
重点是Activity的生命周期。
参考http://hi.baidu.com/a420478805/item/7937000b8f7f3ae3ff240db3
MyApplication类用于储存每一个Activity,并实现关闭所有Activity的操作;在每个Activity的onCreate方法中使用MyApplication.getInstance().addActivity(this)将该Activity添加到MyApplication的对象实例容器中;当需要关闭所有Activity时调用MyApplication.getInstance().exit()即可。
1 |
/**管理Activity的类,主要用于Activity的退出*/ |
1 |
/**双击返回键退出程序*/ |
图 Activity生命周期
(1) Activity有四个状态:
活动状态:当前Activity位于前台,用户可见,可以获得焦点。
暂停状态:其他Activity位于前台,该Activity依然可见,只是不能获得焦点。
停止状态:不可见,失去焦点。
销毁状态:该Activity结束,或Activity所在Dalvik进程被结束。
(2)对照生命周期图,掌握Activity处在某个状态或正向某个状态过渡时回调的函数。
通过以上分析,问题一的解决只需在FirstActivity中添加双击返回键退出程序的代码即可。在这种情况下,只要没退出程序,Activity并没有被销毁(当然是在不考虑内存使用的情况下),只是暂停或停止了。
可采用这种方法,当SecondActivity跳转到ThirdActivity后就销毁它(即在SecondActivity的Intent后添加finish()),那么由Activity栈可知,在ThirdActivity中按返回键时会返回到FirstActivity。
代码很简单,主要是FirstActivity跳到SecondActivity,SeconActivity跳到ThirdActivity,
每个布局文件中只有一个按钮。
FirstActivity.javapackage com.lucus.multi_activity_jump; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.KeyEvent; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Toast; public class FirstActivity extends Activity { private long exitTime = 0; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //将当前Activity添加到MyApplication对象实例中 MyApplication.getInstance().addActivity(this); Button firstBtn = (Button)findViewById(R.id.firstBtn); firstBtn.setOnClickListener(new FirstBtnOnClickListener()); }//end method onCreate class FirstBtnOnClickListener implements OnClickListener { @Override public void onClick(View v) { // TODO Auto-generated method stub Intent intent = new Intent(); intent.setClass( FirstActivity.this , SecondActivity.class ); FirstActivity.this.startActivity( intent ); } }//end class FirstBtnOnClickListener /**双击返回键退出程序*/ public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK) { if ((System.currentTimeMillis() - exitTime) > 2000) { Toast.makeText(this, "再按一次退出程序", Toast.LENGTH_SHORT).show(); exitTime = System.currentTimeMillis(); } else { MyApplication.getInstance().exit(); } return true; } return super.onKeyDown(keyCode, event); }//end onKeyDown /**双击返回键退出程序*/ }//end FirstActivity
SecondActivity.javapackage com.lucus.multi_activity_jump; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class SecondActivity extends Activity{ public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.second); MyApplication.getInstance().addActivity(this); Button secondBtn = (Button)findViewById(R.id.secondBtn); secondBtn.setOnClickListener(new SecondBtnOnClickListener()); }//end method onCreate class SecondBtnOnClickListener implements OnClickListener { @Override public void onClick(View v) { // TODO Auto-generated method stub Intent intent = new Intent(); intent.setClass( SecondActivity.this , ThirdActivity.class ); SecondActivity.this.startActivity( intent ); finish();//销毁该Activity } }//end class SecondBtnOnClickListener }//end SecondActivity
ThirdActivity.javapackage com.lucus.multi_activity_jump; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class ThirdActivity extends Activity { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.third); MyApplication.getInstance().addActivity(this); Button thirdBtn = (Button)findViewById(R.id.thirdBtn); thirdBtn.setOnClickListener(new ThirdBtnOnClickListener()); }//end method onCreate class ThirdBtnOnClickListener implements OnClickListener { @Override public void onClick(View v) { // TODO Auto-generated method stub // Intent intent = new Intent(); // intent.setClass( ThirdActivity.this , FirstActivity.class ); // ThirdActivity.this.startActivity( intent ); } }//end class ThirdBtnOnClickListener }//end ThirdActivity
MyApplication.javapackage com.lucus.multi_activity_jump; import java.util.LinkedList; import java.util.List; import android.app.Activity; import android.app.Application; /**管理Activity的类,主要用于Activity的退出*/ public class MyApplication extends Application { private List<Activity> activityList = new LinkedList<Activity>(); private static MyApplication instance; private MyApplication() { } /**单例模式中获取唯一的MyApplication实例*/ public static MyApplication getInstance() { if(null == instance) { instance = new MyApplication(); } return instance; } /**添加Activity到容器中*/ public void addActivity(Activity activity) { activityList.add(activity); } /**遍历所有Activity并finish*/ public void exit() { for(Activity activity:activityList) { activity.finish(); } System.exit(0); } }
http://blog.csdn.net/hpoi/article/details/4629065
http://hi.baidu.com/a420478805/item/7937000b8f7f3ae3ff240db3