我目前有一個導航抽屜,在我的活動中有幾個片段(Home,Help,About)。在啓動時打開Home。我遇到的問題是,當我轉到另一個片段(例如「幫助」),然後繼續讓手機進入睡眠狀態並隨後打開手機時,它將始終返回到主頁而不是幫助。Android - 恢復顯示片段
我對生命週期很陌生,但希望得到一些關於如何從不同片段恢復的反饋。
編輯:提供相關代碼 更新:意識到這是因爲我重新審視簡歷上的意見。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initializeUI();
}
private void initializeUI() {
fragAbout = new About();
fragHelp = new Help();
fragHome = new MyViewPager();
// Adding fragments to activity
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.add(R.id.main_activity_fraglayout, fragHome);
transaction.commit();
...
}
private void addDrawerItems() {
...
DrawerItemAdapter drawerAdapter = new DrawerItemAdapter(this, R.layout.nav_list_row, drawerItems);
mDrawerList.setAdapter(drawerAdapter);
mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
switch (position) {
case 0:
...
newFragOnClick(fragHome, "Home");
break;
case 1:
...
newFragOnClick(fragSettings, "Help");
break;
case 2:
...
newFragOnClick(fragAbout, "About");
break;
default:
break;
}
}
});
}
private void newFragOnClick(Fragment frag, String actionBarTitle){
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.main_activity_fraglayout, frag);
transaction.commit();
}
請上傳您的活動的完整代碼 –
您可以通過託管的活動處理此導航,在託管活動的onResume()中替換所需的片段 – Farhan
@Farhan是的,這似乎工作。謝謝! – WonderfulWonder