下面我的代碼是基於這樣的:http://web.archive.org/web/20100816175634/http://blog.henriklarsentoft.com/2010/07/android-tabactivity-nested-activities/無法使用自己的tabhost嵌套活動的setContentView
我的問題是與應該存儲的活動,所以我可以適當地利用後退按鈕歷史的ArrayList,但是我應用程序崩潰時,我回擊。我認爲這是因爲歷史並不完全存儲視圖。
我說:
View view = getLocalActivityManager().startActivity("ViewPagerActivity", new
Intent(this,ViewPagerActivity.class)
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
.getDecorView();
我的背法,看它是否會加載它和它的作品。
public class FeaturedTabGroup extends ActivityGroup {
// Keep this in a static variable to make it accessible for all the nesten activities, lets them manipulate the view
public static FeaturedTabGroup group;
// Need to keep track of the history if you want the back-button to work properly, don't use this if your activities requires a lot of memory.
private ArrayList history;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.history = new ArrayList();
group = this;
// Start the root activity withing the group and get its view
View view = getLocalActivityManager().startActivity("ViewPagerActivity", new
Intent(this,ViewPagerActivity.class)
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
.getDecorView();
// Replace the view of this ActivityGroup
replaceView(view);
}
public void replaceView(View v) {
// Adds the old one to history
history.add(v);
// Changes this Groups View to the new View.
setContentView(v);
System.out.println("view set successful");
}
public void back() {
if(history.size() > 0) {
history.remove(history.size()-1);
setContentView((Integer) history.get(history.size()-1));
}else {
finish();
}
}
@Override
public void onBackPressed() {
FeaturedTabGroup.group.back();
return;
}
}
編輯:
爲了簡便起見,我會用另外一個問題解決這個問題:爲什麼的setContentView(V)工作,但不是當視圖是店在一個ArrayList?將視圖存儲在數組列表中時會發生什麼?
僅供參考,您可以基於亨裏克·拉爾森托夫特的博客文章在這裏得到一個完整的工作示例:http://richipal.com/post/2624844577 – Theo