2015年8月28日 星期五

【Android】Bundle/Intent 返回正確的上一頁


<方法一>
<page 1>
Intent intent = new Intent(this, xxx.class);
intent.putExtra("back_class", this.getClass().getName());
startActivity(intent);
======================================================================================
<page2>
String clazz = getIntent().getStringExtra("back_class");

<方法二>
<page 1>
Intent intent = new Intent();
Bundle bundle = new Bundle();
bundle.putString("header_title", getString(R.string.transaction));
bundle.putInt("header_background_color", getResources().getColor(R.color.main_func));
bundle.putString("header_back_to", "Submenu_pageA");
intentMenuItem.setClass(Submenu_pageA.this,Submenu_pageB.class);
intentMenuItem.putExtras(bundle);
startActivity(intentMenuItem);
======================================================================================
<page 2>
Bundle bundle = null;
public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
bundle = this.getIntent().getExtras();
if(bundle.containsKey("header_back_to") && bundle.getString("header_back_to").equals("Submenu_pageA")){
  setReturnClass(Submenu_pageA.class);
}else{
  setReturnClass(HomeActivity.class);
}
}

沒有留言:

張貼留言