8

我已經使用Navigation Drawer Fragment和MainActivity的模板實現設置了一個新項目。導航抽屜onNavigationDrawerItemSelected在MainActivity onCreate之前調用?

它爲我提供了以下相關的方法:

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    Intent intent = getIntent(); 
    token = intent.getStringExtra(EXTRA_TOKEN); 

    mNavigationDrawerFragment = (NavigationDrawerFragment) 
      getSupportFragmentManager().findFragmentById(R.id.navigation_drawer); 
    mNavigationDrawerFragment.activityMain = this; 

    mTitle = getTitle(); 

    // Set up the drawer. 
    mNavigationDrawerFragment.setUp(
      R.id.navigation_drawer, 
      (DrawerLayout) findViewById(R.id.drawer_layout)); 
} 

我的MainActivity被飛濺的活動它得到通過EXTRA_TOKEN保存的訪問令牌啓動。

這是MainAcitivity導航抽屜項目選擇收聽者的控制裝置:

@Override 
public void onNavigationDrawerItemSelected(int position) { 
    // update the main content by replacing fragments 
    FragmentManager fragmentManager = getSupportFragmentManager(); 
    onSectionAttached(position + 1); 

    switch(position) { 
     case 0: 
      fragmentManager.beginTransaction() 
        .replace(R.id.container, FeedFragment.newInstance(token, "")) 
        .commit(); 
      break; 

     case 1: 
      fragmentManager.beginTransaction() 
        .replace(R.id.container, PeopleFragment.newInstance("", "")) 
        .commit(); 
      break; 

     case 2: 
      if(qbloggedin) { 
       fragmentManager.beginTransaction() 
         .replace(R.id.container, MessagesFragment.newInstance(token, "")) 
         .commit(); 
      } 
      break; 

     default: 
      break; 
    } 
} 

它開始根據三個不同的碎片上,其在NavDrawer被選擇的項目。在實例化新片段時,token字符串被傳遞到它的構造函數中,該構造函數保存在片段的類中以供進一步使用。

然而,在應用程序的第一次啓動時,似乎onNavigationDrawerItemSelectedonCreate之前被調用!這導致我傳遞一個空值標記到片段中,導致它們全部混亂。

這怎麼可能?據我瞭解,NavigationDrawerFragment應該還沒有安裝!

我在onCreateonNavigationDrawerItemSelected switch position = 0上設置了斷點。 onNavigationDrawerItemSelected確實在onCreate之前被擊中。

如何確保在嘗試處理onNavigationDrawerItemSelected之前先獲取令牌?

任何幫助,將不勝感激。

回答

1

你可以的意圖轉移到一個構造函數和有保存令牌就像這樣:

Intent i; 

...... 

public FragmentConstructor() { 

    i = getIntent(); 
    token = intent.getStringExtra(EXTRA_TOKEN); 

} 
+0

請幫忙,例如intent.method()有一個Context參數。當有Context參數時,intent.method(context)會獲得一個空Context。 –

10

我相信我想通了這一點,因爲它是發生在我身上的人誰搜索這一點,並不能找到回答。

如果您使用Android Studio DrawerActivity,那麼它們會爲您創建樣板代碼。在activity_main.xml中的這段代碼或DrawerActivity設置爲其內容視圖的XML中,都有一個標籤。

當在onCreate()中調用setContentView()時,會自動創建該片段,因此技術上onCreate()仍然被首先調用,但是在創建其他任何東西之前調用onNavigationDrawerItemSelected()方法。由於setContentView通常保持最佳狀態,因此嘗試將碎片狀態存儲在抽屜中時會導致問題。

只需移動任何檢查上面setContentView()的savedInstanceBundle的代碼,它就能解決問題。

與評論舉例:

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    // THIS IS WHERE YOU CHECK FOR SAVED INSTANCE 
    // Check for frag 
    if (savedInstanceState != null) { 
     Log.i(TAG, "Get QuestionDayFragment"); 
     mQuestionDaysFragment = (QuestionDaysFragment) getSupportFragmentManager().getFragment(savedInstanceState, QUESTION_DAY_FRAGMENT); 
    } 

    // View injection 
    setContentView(R.layout.activity_main); 
    ButterKnife.inject(this); 

    // THIS IS WHERE THE CODE WAS BEFORE 
    // THIS WOULD BE CALLED AFTER onNavigationDrawerItemSelected() 

    // Singleton injection 
    LifeboxApplication.graph().inject(this); 

    // Toolbar 
    setSupportActionBar(mToolbar); 

    // FB 
    uiHelper = new UiLifecycleHelper(this, callback); 
    uiHelper.onCreate(savedInstanceState); 

    // Drawer 
    mNavigationDrawerFragment = (NavigationDrawerFragment) getSupportFragmentManager().findFragmentById(R.id.navigation_drawer); 
    mTitle = getTitle(); 
    mNavigationDrawerFragment.setUp(R.id.navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout)); 

} 
0

我該怎麼做才能使其正常工作是檢查頁面是否已經執行onNavigationDrawerItemSelected

private Boolean loaded=false; 

    protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState);   

      // Your code here 
      this.loaded=true; 
    } 

    public void onNavigationDrawerItemSelected(int position) { 
     if (!this.loaded){ 
      return; 
    } 
0

我也同意使用一個布爾值,之前加載檢查onCreate()是否已完成加載。我唯一的建議是,爲了快速修復,你可以使用onSectionAttached(int number)來處理選中的每個項目,而不是onNavigationDrawerItemSelected。