2017-02-22 18 views
0

我需要幫助。我正在開發一個應用程序。onItemClick如果轉到FirstFragment否則轉到SecondFragment

這是我的導航欄。 我有三個選項菜單(彩色的)。

1)FirstFragment
2)MapFragment
3)第三塊碎片

Image Here

這是我的抽屜式導航欄包括 「建立類型列表」 中。

點擊後會打開另一個片段(名爲ListFragment),其中包含該場所的列表。

list

場所必須的功能轉到FirstFragment要不然到MapFragment。

場景:

1)如果我在 我FirstFragment->點擊導航抽屜>點擊ListFragment->銀行

在這種情況下,當用戶點擊該銀行將檢查用戶在FirstFragment中,然後進入FirstFragment。否則它會轉到MapFragment ........結果它會轉到FirstFragment。

2)如果我在MapFragment-我>點擊導航抽屜>點擊ListFragment->銀行

它檢查,如果用戶是在FirstFragment否則它會去MapFragment ..... ......結果它將轉到MapFragment。

問題:所以這個問題我不知道如何實現它在我的代碼...

這裏的代碼在我的MainActivity:

@Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 
     android.app.FragmentManager fragmentManager = getFragmentManager(); 

     if (id == R.id.action_ar) { 
      fragmentManager.beginTransaction() 
        .replace(R.id.content_frame, new FirstFragment()) 
        .commit(); 
      Toast.makeText(this, "First Selected", Toast.LENGTH_SHORT).show(); 
     } 
     else if (id == R.id.action_map) { 
      fragmentManager.beginTransaction() 
        .replace(R.id.content_frame, new MapFragment()) 
        .commit(); 
      Toast.makeText(this, "MAP Selected", Toast.LENGTH_SHORT).show(); 
     } 
     else if (id == R.id.action_direction) { 
      fragmentManager.beginTransaction() 
        .replace(R.id.content_frame, new ThirdFragment()) 
        .commit(); 
      Toast.makeText(this, "Third Selected", Toast.LENGTH_SHORT).show(); 
     } 
     return super.onOptionsItemSelected(item); 
    } 
+0

你的問題,所以你需要證明代碼... –

+0

問題是我不知道如何。這就是爲什麼我需要幫助。 – Problematic

+0

我問你用更多的代碼請[編輯]你的問題......雖然,你的問題似乎還不清楚。嘗試創建一個[mcve] –

回答

-1

可以傳遞一個變量「from_where_did_icome」(例如)每次你去一個片段或另一個來檢查你到達那裏。對於使用此代碼示例做:

//Passing data to fragment: 

Bundle bun = new Bundle(); 
bun.putString("pathimage", path); 
fragment_setting.setArguments(bun); 

//Getting data in fragment: 

Bundle b = getArguments(); 
String path = b.getString("pathimage"); 

瞭解更多在這個兩個環節:在`FirstFragment`存在 https://developer.android.com/training/basics/fragments/communicating.htmlhttps://developer.android.com/guide/components/fragments.html#CommunicatingWithActivity

+0

爲什麼你需要一個額外的變量來告訴你你在哪個片段?爲什麼你會使用像php這樣的下劃線變量名? –

+0

@Nick它不是你所在的位置,而是你從哪個片段/活動中得到的信息,通過這些信息,我們可以決定他用簡單的if或者之後的切換來問什麼。也編輯了答案。 –

相關問題