2015-07-10 58 views
-1

我想傳遞一個對象片段 - >活動 - >片段。麻煩傳遞對象片段到片段

我的第二個片段說讓爭論時,我的目標是零:

片段A:(發送對象,溝通是接口主要活動)

meetsListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) { 
      Meet meet = new Meet(); 
      meet = meetList.get(position); // meet is not null here 
      communicator.changeToolbarTitle(meet.getName()); 

      ****** Sends Meet object to main activity ******** 
      communicator.sendMeetToMeetProfile(meet); 
     } 
    }); 

主要活動:

@Override 
public void sendMeetToMeetProfile(Meet meet) { 
    MeetAthletesListContainer meetAthletesListContainer = new MeetAthletesListContainer(); 

    ***** Fragment is not replaced just called ******* 
    MeetAthletesMales meetAthletesMales = new MeetAthletesMales(); 
    MeetAthletesFemales meetAthletesFemales = new MeetAthletesFemales(); 
    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); 

    Bundle bundleMales = new Bundle(); 
    bundleMales.putParcelable("meetAthletesMales", meet); 
    meetAthletesMales.setArguments(bundleMales); 
    Bundle bundleFemales = new Bundle(); 
    bundleFemales.putParcelable("meetAthletesFemales", meet); 
    meetAthletesFemales.setArguments(bundleFemales); 

    ******* Replaces current fragment with container that contains above (2) fragments ******* 
    transaction.replace(R.id.frameLayout, meetAthletesListContainer, "meetAthletesListContainer"); 
    transaction.addToBackStack(null); 
    transaction.commit(); 
} 

片段B :(片段包含在另一片段內)

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 

    view = inflater.inflate(R.layout.fragment_meet_athletes_males, container, false); 
    Log.e("", "Fragment onCreateView Called!"); 

    initializeVar(); 
    *****Says this meet object is null***** 
    meet = getArguments().getParcelable("meetAthletesMales"); 
} 

我正在用另一個替換片段,但將對象傳遞給另一片段(meet_container_fragment)內的片段。所以我的對象被直接發送到片段。我在另一種情況下這個工作,我實際上取代片段,但我想更換片段的容器,而不是實際的B片段

+0

transaction.replace將刪除所有片段,並添加新的片段。也許使用transaction.add? – zzas11

回答

0

試試這個

MeetAthletesListContainer meetAthletesListContainer = new MeetAthletesListContainer(); 

     ***** Fragment is not replaced just called ******* 
     MeetAthletesMales meetAthletesMales = new MeetAthletesMales(); 
     MeetAthletesFemales meetAthletesFemales = new MeetAthletesFemales(); 
     FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); 

     Bundle bundle = new Bundle(); 
     bundle.putParcelable("meetAthletesMales", meet); 
     bundle.putParcelable("meetAthletesFemales", meet); 
     meetAthletesFemales.setArguments(bundle); 

     ******* Replaces current fragment with container that contains above (2) fragments ******* 
     transaction.replace(R.id.frameLayout, meetAthletesListContainer, "meetAthletesListContainer"); 
     transaction.addToBackStack(null); 
     transaction.commit(); 
+0

我不認爲你真的在這裏改變任何東西 –

0

試試這個,

主要活動

 FragmentB fragmentb = FragmentB.newInstance(appContext,meet); 
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 
    fragmentTransaction.add(R.id.contentContainer, fragmentb , tag); 

片段B

public static FragmentB newInstance(AppContext appContext, Meet meet) { 
    FragmentB fragment = new FragmentB(); 
    Bundle args = new Bundle(); 
    args.putSerializable(ARG_APP_CONTEXT, appContext); 
    args.putString(ARG_MEET_NAME, meet.name); 

}

@Override 
public void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
    if (getArguments() != null) { 
     appContext = (AppContext) getArguments().get(ARG_APP_CONTEXT); 
     name= getArguments().getString(ARG_MEET_NAME); 
     } 
} 

也使滿足實現Serializable

+0

我會嘗試這個,但想知道是否添加/替換不同片段而不是片段B將對片段B獲得捆綁包產生影響。在我的程序中,片段B包含在另一個片段中,我將替換 –

+0

嘗試使用replace.It正在爲我工​​作.. –